用三角函数旋转图像

Rotating image with trigonometric functions

您好,我想仅使用三角函数对图像进行评级。

假设“q”为“theta”

y'=x*sin(q) + y(-tan(q/2))*sin(q)+y

x'=x+2y(-tan(q/2)) + xsin(q)( -tan^2(q/2)) + y

x' 和 y' 将成为我们新的 x 和 y 值。

我在图像处理方面很新,但我写了一个我认为有点正确的代码。

在此示例中,“theta”被视为“30 度”,但我将开发代码,它适用于任何其他度数...

基本上,我想从你那里找出我的错误,或者如果代码完全错误,请告诉我正确的方法。

im1=zeros(64*64);

subplot(1,2,1);
imshow(im1);

[x,y]=size(im1);
    
%Let theta=30 degrees

for i=1:64
    for j=1:64
        
        x2(i,j)=x+2*y*(-tand(15))+ x*sind(30)*(-tand(15).^2)+y;
        
        y2(i,j)=x*sind(30)+y*(-tand(15))*sind(30)+y;

 % i'm not sure about where to put i and j, but i prefer like this.
        
    end
   
end

im2=[x2,y2];

subplot(1,2,2);
imshow(im2);

使用三角函数旋转图像

可以使用三角函数通过分解旋转矩阵来旋转图像。旋转矩阵可以用矩阵来描述,对应的代码片段如下:

代码将通过评估原始图像中的对应点来迭代以填充旋转后的图像。需要注意的一些事情是需要用零填充图像以允许图片在旋转后不被剪裁。有关旋转矩阵的更详细推导,请查看此post:How do rotation matrices work?

代码段:

X_Displacement = Row - X_Midpoint;
Y_Displacement = Column - Y_Midpoint;

X_Prime = X_Displacement*cosd(Angle) + Y_Displacement*sind(Angle); 
Y_Prime = -X_Displacement*sind(Angle) + Y_Displacement*cosd(Angle);

在上面的代码片段中,使用了 cosd()sind() 以便可以接受角度的度数。如果您想交替使用弧度,请使用标准 cos()sin() 函数。当使用 round() 时,此处应用 nearest-neighbour 插值。这是由于旋转图像的性质。一个小案例正在旋转 3 x 3 图像。这带来了一个问题,您基本上只能以 9 种不同的方式旋转中心像素周围的像素,这并不代表所有可能的角度。

导出的透明图像:

对于灰度图像:

clear;
Angle = 30;

[Original_Image] = imread("cameraman.tif");
subplot(1,2,1); imshow(Original_Image);
title("Original Image");

[Image_Height,Image_Width] = size(Original_Image);

%Padding Image%
Padding_Bottom_And_Top = zeros(round(Image_Height/2),Image_Width);
Side_Padding = zeros(Image_Height+2*size(Padding_Bottom_And_Top,1),Image_Width/2);
Padded_Image = [Padding_Bottom_And_Top; Original_Image];
Padded_Image = [Padded_Image; Padding_Bottom_And_Top];
Padded_Image = [Side_Padding Padded_Image];
Padded_Image = [Padded_Image Side_Padding];

[Padded_Image_Height,Padded_Image_Width] = size(Padded_Image);
Rotated_Image = zeros(Image_Height,Image_Width);

%Finding the centre points%
X_Midpoint = Padded_Image_Height/2;
Y_Midpoint = Padded_Image_Width/2;


for Row = 1: Padded_Image_Height
    for Column = 1: Padded_Image_Width
    
    X_Displacement = Row - X_Midpoint;
    Y_Displacement = Column - Y_Midpoint;
    
    X_Prime = X_Displacement*cosd(Angle) + Y_Displacement*sind(Angle); 
    Y_Prime = -X_Displacement*sind(Angle) + Y_Displacement*cosd(Angle);
    
    X_Prime = round(X_Prime + X_Midpoint);
    Y_Prime = round(Y_Prime + Y_Midpoint);

    if(X_Prime >= 1 && Y_Prime >= 1 && X_Prime <= Padded_Image_Height && Y_Prime <= Padded_Image_Width)
        Rotated_Image(Row,Column) = Padded_Image(X_Prime,Y_Prime);
    end

    
    end 
end

Rotated_Image = uint8(Rotated_Image);
subplot(1,2,2); imshow(Rotated_Image);
title("Rotated Image"); 

%Saving rotated image with transparency%
Transparent_Region = (Rotated_Image ~= 0);
Transparent_Region = uint8(255.*Transparent_Region);
imwrite(Rotated_Image,'Rotated.png','Alpha',Transparent_Region);

导出的带透明度的彩色图像:

对于彩色图像:

clear;
Angle = 30;

[Original_Image] = imread("peppers.png");
subplot(1,2,1); imshow(Original_Image);
title("Original Image");

[Image_Height,Image_Width,~] = size(Original_Image);

%Padding Image%
Padding_Bottom_And_Top = zeros(round(Image_Height/2),Image_Width,3);
Side_Padding = zeros(Image_Height+2*size(Padding_Bottom_And_Top,1),Image_Width/2,3);
Padded_Image = [Padding_Bottom_And_Top; Original_Image];
Padded_Image = [Padded_Image; Padding_Bottom_And_Top];
Padded_Image = [Side_Padding Padded_Image];
Padded_Image = [Padded_Image Side_Padding];

[Padded_Image_Height,Padded_Image_Width,~] = size(Padded_Image);
Rotated_Image = zeros(Image_Height,Image_Width,3);

%Finding the centre points%
X_Midpoint = Padded_Image_Height/2;
Y_Midpoint = Padded_Image_Width/2;


for Row = 1: Padded_Image_Height
    for Column = 1: Padded_Image_Width
    
    X_Displacement = Row - X_Midpoint;
    Y_Displacement = Column - Y_Midpoint;
    
    X_Prime = X_Displacement*cosd(Angle) + Y_Displacement*sind(Angle); 
    Y_Prime = -X_Displacement*sind(Angle) + Y_Displacement*cosd(Angle);
    
    X_Prime = round(X_Prime + X_Midpoint);
    Y_Prime = round(Y_Prime + Y_Midpoint);

    if(X_Prime >= 1 && Y_Prime >= 1 && X_Prime <= Padded_Image_Height && Y_Prime <= Padded_Image_Width)
        Rotated_Image(Row,Column,:) = Padded_Image(X_Prime,Y_Prime,:);
    end

    
    end 
end

Rotated_Image = uint8(Rotated_Image);
subplot(1,2,2); imshow(Rotated_Image);
title("Rotated Image"); 

%Saving rotated image with transparency%
Transparent_Region = (Rotated_Image(:,:,1) ~= 0);
Transparent_Region = uint8(255.*Transparent_Region);
imwrite(Rotated_Image,'Rotated.png','Alpha',Transparent_Region);

运行 使用 MATLAB R2019b