从平面坐标转球面坐标加旋转

clear
filename = '1.jpg';
lena = imread(filename); % 读入一幅彩色图像

I=imresize(lena,0.125,'bicubic');
height= size(I, 1);
width = size(I, 2);
M_PI     =  3.14159265358979323846; 
M_PI_2   =  1.57079632679489661923;

for i=1:height
    for j=1:width
     x_pos = j / width * (2 * M_PI) - M_PI;
     y_pos = i / height * (M_PI)-M_PI_2;
              
	 Nx = cos(y_pos) * cos(x_pos);
	 Ny = cos(y_pos) * sin(x_pos);
	 Nz = sin(y_pos);
	
	 angle_x = 0 * M_PI / 180.0;
	 angle_y = 0 * M_PI / 180.0;

	 x = Nx;
	 y = Ny;
	 z = Nz;

	 x_ = x;
	 y_ = y * cos(angle_x) - z * sin(angle_x);
	 z_ = y * sin(angle_x) + z * cos(angle_x);

	Nx = z_ * sin(angle_y) + x_ * cos(angle_y);
	Ny = y_;
	Nz = z_ * cos(angle_y) - x_ * sin(angle_y);

	temp0 =Nx;
    temp1 = (sqrt(Nx*Nx + Ny*Ny));

	if (abs(temp0) < 0.0000001)
		temp0 = 0.0000001;
    end
    
	if (abs(temp1) < 0.0000001)
		temp1 = 0.0000001;	
    end

	 f_i = atan(Ny / temp0);
	 f_j = atan(Nz / temp1);

  	if (Nx < 0 && Ny < 0)
  		f_i = f_i - M_PI;
      end
      
  	if (Nx < 0 && Ny > 0)
  		f_i = f_i + M_PI;	
     end
     
	 f_ii = (f_i + M_PI) / (2 * M_PI) * width;
	 f_jj = (f_j + M_PI_2) / (M_PI)* height;
     
	 jj = round(f_ii);
	 ii = round(f_jj);
    
     if(ii < 1)
         ii = 1;
     end
     
     if(jj < 1)
         jj = 1;
     end
     
     if(ii > height)
         ii = height;
     end
     
     if(jj > width)
         jj = width;
     end
        r = I(ii,jj,1);
        g = I(ii,jj,2);
        b = I(ii,jj,3);
        J(i,j,1) = r;
        J(i,j,2) = g;
        J(i,j,3) = b;      
    end 
end
imshow(J); % 显示彩色图像

你可能感兴趣的:(图像处理)