图像旋转

原创
2020/10/14 20:00
阅读数 1.2K

效果

Matlab代码

clc
clear all
close all

I=imread('lena.jpg');
[W,H,chanel]=size(I);
dst_data=zeros([W,H,chanel]);
degree=20;
swirldegree = degree/1000.0;
MidX = W/2;
MidY = H/2;
%根据极坐标公式new[rho , theta] = old[rho , theta + rho /K] .
% 本代码直接根据极坐标转角坐标
for y=1:H
    dst_Imgindex = y;
    for x=1:W
        Yoffset = y - MidY;
        Xoffset = x - MidX;
        radian = atan2(Yoffset,Xoffset);
        radius = sqrt(Xoffset*Xoffset+Yoffset*Yoffset);
        
        X = int32(radius*cos(radian+radius*swirldegree)+MidX);
        Y = int32(radius*sin(radian+radius*swirldegree)+MidY);
        
        if (X >= W)
            X = W-1;
        end
        if (X < 1)
            X = 1;
        end
        if (Y >= H)
            Y = H-1;
        end
        if (Y < 1)
            Y = 1;
        end
        dst_data(x,y,:) = I(X,Y,:);
    end
end
dst_data=uint8(dst_data);
subplot(121);
imshow(dst_data);
subplot(122);
imshow(I);

C代码(对BMP位图文件)

int Swirl(IMAGE_TYPE *BMP24_img,int degree)
{
	DWORD width,height,x,y,bfsize;
	WORD  biBitCount = 3;
	T_U8 *dst_data,*bmp,*img_data;
	T_U32 line_byte,src_Imgindex,dst_Imgindex;
	int Xoffset,Yoffset,MidX,MidY,X,Y;
	double radius,radian,swirldegree;
	
	BITMAPFILEHEADER bf;
	BITMAPINFOHEADER bi;
	
	FILE *Swirl_fp = fopen("swirl.bmp","wb");
	
	if(NULL == Swirl_fp)
	{
		printf("Can't open Swirl_fp.bmp\n");
		return -1;
	}
	
	if (degree < 1) degree = 1;
	if (degree > 100) degree = 100;
	swirldegree = degree/1000.0;
 
	memset(&bf, 0, sizeof(bf));
	memset(&bi, 0, sizeof(bi));
	
	bmp = BMP24_img;
	memcpy(&bf,bmp,14);
	memcpy(&bi,&bmp[14],40);
	
	fwrite(&bf,sizeof(BITMAPFILEHEADER),1,Swirl_fp);
	fwrite(&bi,sizeof(BITMAPINFOHEADER),1,Swirl_fp);
	
	height = bi.biHeight;
	width  = bi.biWidth;
	bfsize = bf.bfSize;
	biBitCount = bi.biBitCount;
	line_byte =  (width*3+3) & ~0x3;
	img_data = bmp+54;
	
	dst_data = (T_U8 *)malloc(line_byte*height);
	if (NULL == dst_data)
	{
		printf("Can't malloc for dst image!\n");
		return -1;
	}
	memset(dst_data,0,line_byte*height);
	
	MidX = width/2;
	MidY = height/2;
	
	for(y = 0; y < height; y+= 1)
	{
		dst_Imgindex = y*line_byte;
		for(x=0; x <width; x+= 1)
		{
			Yoffset = y - MidY;
			Xoffset = x - MidX;
			
			radian = atan2((double)Yoffset,(double)Xoffset);
			radius = sqrt(Xoffset*Xoffset+Yoffset*Yoffset);
			
			X = (int)(radius*cos(radian+radius*swirldegree)+MidX);
			Y = (int)(radius*sin(radian+radius*swirldegree)+MidY);
			
			if (X >= width) X = width-1;
			if (X < 0)	   X = 0;
			if (Y >= height)Y = height-1;
			if (Y < 0)	   Y = 0;
			
			src_Imgindex = Y*line_byte+3*X;
			
			dst_data[dst_Imgindex] = img_data[src_Imgindex];
			dst_data[dst_Imgindex+1] = img_data[src_Imgindex+1];
			dst_data[dst_Imgindex+2] = img_data[src_Imgindex+2];
			
			dst_Imgindex +=3;
		}
	}
	
	
	fwrite(dst_data,line_byte*height,1,Swirl_fp);
	fclose(Swirl_fp);
	Swirl_fp = NULL;
	free(dst_data);
	return 0;
}

参考引用

  1. https://blog.csdn.net/lz0499/article/details/78265391
  2. https://smallsats.org/2014/01/03/swirlmatlab-code/
展开阅读全文
打赏
0
0 收藏
分享
加载中
更多评论
打赏
0 评论
0 收藏
0
分享
返回顶部
顶部