MATLAB-based image rotation

Source: Internet
Author: User

I wrote my blog for the first time.

When using the imrotate function of MATLAB to rotate a binary image, you cannot implement the function. You have written a rotation program. The basic principle of rotation is as follows: rotate the vertex A (x0, y0) in the source image to (x1, Y1). If the rotation angle is Alpha, then X1 = x0 * Cos (alpha) + y0 * sin (alpha); Y1 =-x0 * sin (alpha) + y0 * Cos (alpha );

Operation logic:

(1) Find A Bit Of A (COL, row), Col, and row in the src_h and src_w source images, respectively, as the number of columns and number of rows in the pixel.

(2) create a coordinate system with the image center as the coordinate origin. Then the coordinate of Point A (COL, row) is X = col-src_w/2, y = 0.5 * Src_h-row;

(3) find four vertices on the original image and obtain the coordinates, calculate the size of the rotated image based on the rotation formula, and obtain the pixel value corresponding to the original image after rotation, use the adjacent interpolation method or bilinear interpolation method to solve the floating point problem.

% Rotate Alpha clockwise, Alpha in radians, Rotation Angle Range: 0-360

 

Function dest_img = rot2 (src_img, alpha)
[Src_h, src_w] = size (src_img );
X1 =-0.5 * src_w; Y1 = 0.5 * src_h;
X2 = 0.5 * src_w; y2 = 0.5 * src_h;
X3 = 0.5 * src_w; Y3 =-0.5 * src_h;
X4 =-0.5 * src_w; Y4 =-0.5 * src_h;
% Calculate the coordinates of the four corners of the new image. Take the image center as the coordinate origin.
Destx1 = cos (alpha) * X1 + sin (alpha) * Y1;
Desty1 =-sin (alpha) * X1 + cos (alpha) * Y1;
Destx2 = cos (alpha) * X2 + sin (alpha) * Y2;
Desty2 =-sin (alpha) * X2 + cos (alpha) * Y2;
Destx3 = cos (alpha) * X3 + sin (alpha) * Y3;
Desty3 =-sin (alpha) * X3 + cos (alpha) * Y3;
Destx4 = cos (alpha) * X4 + sin (alpha) * Y4;
Desty4 =-sin (alpha) * X4 + cos (alpha) * Y4;
% To the width and height of the rotated image
Dest_w = max (ABS (Destx1-Destx3), ABS (Destx2-Destx4 ));
Dest_h = max (ABS (Desty1-Desty3), ABS (Desty2-Desty4 ));
Dest_img = zeros (dest_h, dest_w );
For I = 1: dest_h
For j = 1: dest_w
% Convert to the coordinates before Rotation
The coordinates of % (I, j) points are dest_x = j-0.5 * dest_w dest_y = 0.5 * Dest_h-i
Dest_x = j-0.5 * dest_w;
Dest_y = 0.5 * Dest_h-i;
% Get the coordinates before Rotation
Src_x = dest_x * Cos (alpha)-dest_y * sin (alpha );
Src_y = dest_x * sin (alpha) + dest_y * Cos (alpha );
If (src_x>-0.5 * src_w + 1) & (src_x <0.5 * Src_w-1) & (src_y>-0.5 * src_h + 1) & (src_y <0.5 * Src_h-1)
% Maps the coordinates before rotation to the rows and columns of the image, rounded to the nearest interpolation.
Row = round (0.5 * Src_h-src_y );
Col = round (0.5 * src_w + src_x );
Dest_img (I, j) = src_img (row, col );
End
End
End
The effect is as follows:

Source image

Rotate 45 degrees after grayscale

References

Http://wenku.baidu.com/view/b687c10ff78a6529647d5384.html

Http://www.cnblogs.com/mlv5/archive/2012/02/02/2336321.html


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.