用三角函数旋转图像 [英] Rotating image with trigonometric functions

查看:118
本文介绍了用三角函数旋转图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我只想使用三角函数来批准图像.

假设"q"为0.作为"theta"

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

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

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

我在图像处理方面还很新,但是我写了一个我认为是正确的代码.

在此示例中,"theta"被认为是"30度".但我将开发代码,并将其用于其他任何程度的工作.

所以基本上,我希望您从我那里发现自己的错误,或者如果代码是完全错误的,请告诉我正确的方法.

  im1 = zeros(64 * 64);子图(1,2,1);imshow(im1);[x,y] = size(im1);%theta = 30度对于我= 1:64对于j = 1:64x2(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和j放在哪里,但是我更喜欢这样.结尾结尾im2 = [x2,y2];子图(1,2,2);imshow(im2); 

解决方案

使用三角函数旋转图像

可以使用三角函数通过分解旋转矩阵来旋转图像.旋转矩阵可以描述为矩阵和下面的相应代码段:

代码将通过评估原始图像中的相应点来迭代以填充旋转的图像.需要注意的一点是,需要使用零填充图像,以使旋转后图片不会被剪切.有关旋转矩阵的详细推导,请查看以下文章:导出的图像具有透明度:

对于灰度图像:

  clear;角度= 30;[Original_Image] = imread("cameraman.tif");子图(1,2,1);imshow(Original_Image);标题(原始图像");[Image_Height,Image_Width] = size(Original_Image);%填充图片%Padding_Bottom_And_Top =零(round(Image_Height/2),Image_Width);Side_Padding =零(Image_Height + 2 * size(Padding_Bottom_And_Top,1),Image_Width/2);Padded_Image = [Padding_Bottom_And_Top;Original_Image];Padded_Image = [Papped_Image;Padding_Bottom_And_Top];Padded_Image = [Side_Padding Padded_Image];Padded_Image = [Papped_Image Side_Padding];[Papped_Image_Height,Padded_Image_Width] = size(Papped_Image);Rotated_Image =零(Image_Height,Image_Width);%找到中心点%X_Midpoint = Padded_Image_Height/2;Y_Midpoint = Padded_Image_Width/2;对于行= 1:Padded_Image_Height对于列= 1:Padded_Image_WidthX_Displacement =行-X_Midpoint;Y_Displacement =列-Y_Midpoint;X_Prime = X_Displacement * cosd(角度)+ Y_Displacement * sind(角度);Y_Prime = -X_Displacement * sind(Angle)+ Y_Displacement * cosd(Angle);X_Prime =回合(X_Prime + X_Midpoint);Y_Prime =圆(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)= Papped_Image(X_Prime,Y_Prime);结尾结尾结尾Rotated_Image = uint8(Rotated_Image);子图(1,2,2);imshow(Rotated_Image);title(旋转图像");%保存具有透明度的旋转图像%Transparent_Region =(Rotated_Image〜= 0);Transparent_Region = uint8(255. * Transparent_Region);imwrite(Rotated_Image,'Rotated.png','Alpha',Transparent_Region); 

导出具有透明度的彩色图像:

对于彩色图像:

  clear;角度= 30;[Original_Image] = imread("peppers.png");子图(1,2,1);imshow(Original_Image);标题(原始图像");[Image_Height,Image_Width,〜] = size(Original_Image);%填充图片%Padding_Bottom_And_Top =零(round(Image_Height/2),Image_Width,3);Side_Padding =零(Image_Height + 2 * size(Padding_Bottom_And_Top,1),Image_Width/2,3);Padded_Image = [Padding_Bottom_And_Top;Original_Image];Padded_Image = [Papped_Image;Padding_Bottom_And_Top];Padded_Image = [Side_Padding Padded_Image];Padded_Image = [Papped_Image Side_Padding];[Papped_Image_Height,Papped_Image_Width,〜] = size(Papped_Image);Rotated_Image =零(Image_Height,Image_Width,3);%找到中心点%X_Midpoint = Padded_Image_Height/2;Y_Midpoint = Padded_Image_Width/2;对于行= 1:Padded_Image_Height对于列= 1:Padded_Image_WidthX_Displacement =行-X_Midpoint;Y_Displacement =列-Y_Midpoint;X_Prime = X_Displacement * cosd(角度)+ Y_Displacement * sind(角度);Y_Prime = -X_Displacement * sind(Angle)+ Y_Displacement * cosd(Angle);X_Prime =回合(X_Prime + X_Midpoint);Y_Prime =圆(Y_Prime + Y_Midpoint);if(X_Prime> = 1&& Y_Prime> = 1&& X_Prime< = Papped_Image_Height&& Y_Prime< = Papped_Image_Width)Rotated_Image(Row,Column,:) =填充图像(X_Prime,Y_Prime,:);结尾结尾结尾Rotated_Image = uint8(Rotated_Image);子图(1,2,2);imshow(Rotated_Image);title(旋转图像");%保存具有透明度的旋转图像%Transparent_Region =(Rotated_Image(:,:,1)〜= 0);Transparent_Region = uint8(255. * Transparent_Region);imwrite(Rotated_Image,'Rotated.png','Alpha',Transparent_Region); 

使用MATLAB R2019b进行运行

Hello I want to ratate image with only using trigonometric functions.

Assume "q" as a "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' and y' is going to be our new x and y values.

I am quite new in image proccesiing but i wrote a code which i think is kind of right.

In this example "theta" is considered "30 degrees" but i will develop the code and it will work for any other degrees...

So basically, I want from u to find my misake or if the code is totally wrong show me to right way to do it, please.

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);

解决方案

Rotating Images Using Trigonometric Functions

Rotating an image can be done using trigonometric functions by decomposing the rotation matrix. The rotation matrix can be described as matrices and the corresponding code snippet below:

The code will iterate to populate the rotated image by evaluating the corresponding point in the original image. Some things to note is that padding the image with zeroes is required to allow for the picture not to be clipped after rotation. For a more detailed derivation of the rotation matrix please check out this post: How do rotation matrices work?

Code Snippet:

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);

In the above code snippet cosd() and sind() are used so that angles can be accepted in terms of degrees. If you'd like to alternatively use radians use the standard cos() and sin() functions. Here nearest-neighbour interpolation is applied when round() is used. This is due to the nature of rotating an image. A small case is rotating a 3 by 3 image. This brings an issue to light where you essentially can only pivot the pixels surrounding the centre pixel in 9 different ways which not representative of all the possible angles.

Exported Image with Transparency:

For Greyscale Images:

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);

Exported Colour Image with Transparency:

For Coloured Images:

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);

Ran using MATLAB R2019b

这篇关于用三角函数旋转图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆