Learn How to Rotate Images in Matlab: Step-by-Step Guide | Expert Tips

In summary: This is because the transformation matrix for rotating a point in the x-y plane is:| cos(theta) -sin(theta)|| sin(theta) cos(theta)|In summary, the conversation is about rotating an image in Matlab and the issue of a black background and lines/dots appearing on the rotated image. The code used to rotate the image is shared, along with images of the original and rotated versions. Suggestions are made to improve the code, such as using regionprops and centroid coordinates. The conversation ends with a suggestion to check the transformation matrix used for rotation.
  • #1
gnome
1,041
1
OK here I am, bigshot, giving out advice on how to rotate an image when I'm just trying to learn that myself.

I'm trying to do this in Matlab; it's rotating, but for some reason the rotated image is acquiring a black background and has various lines and dots superimposed on it. Can anyone tell me why?

Here's the code:
Code:
% rotation.m

% reads in and rotates a figure in file triangles.png

a = imread('triangles.png');
image(a);         % this just displays the original image
pause;
close;              % this closes it
[rows cols layers] = size(a);     % this declares 3 variables named rows, cols
                                           % &  layers & assigns to them the dimensions of array a
s = min(rows,cols);
%% want to make sure the new image is square, just for convenience
rows=s;
cols=s;
%% declare a 32-bit int vector p (1 x 2) to hold the coordinates of the new point, initialized (0 0)
p=int32(zeros(1,2));
%% for simplicity, I will only deal with one color so define two 2D matrices, size rows x cols, 
%% type double to allow multiplication, initialized to all 0s
b = double(zeros(rows,cols));
c = double(zeros(rows,cols));
%% loop through the array, copying just the red layer of a to the corresponding pixels of b
for row = 1:rows,
    for col = 1:cols,
        b(row,col)=a(row,col,1);
    end
end

%% define the transformation matrix t
t = [ cos(pi/4) -sin(pi/4); sin(pi/4) cos(pi/4)];
%% now loop through the arrays computing rotated & shifted coordinates for array c
%% and copy pixels from b to their new locations in c
for row = 1:rows,
    for col = 1:cols,
        %% first compute the coords of the new point; note I'm rotating
        %% and shifting to prevent any negative indices
        %% got the 140 right-shift by trial & error
        p = round([row col]*t) + [0 140];
        %% now copy the current pixel value to the new matrix
        c(p(1,1),p(1,2)) = b(row,col);
    end
end

image(c);
hold on;         % this holds the image window open so I can view one superimposed over  the other 
image(b);
imwrite(b,'tri.png');
imwrite(c,'newtri.png');

Images are attached. triangles.png is the original image. tri.png is just the red layer of that file; that's the array I'm rotating. newtri.png is the result.

Edit: I just noticed that tri.png got rotated somehow in the process of uploading, so it wasn't oriented the same as the original image. Now it's OK.

Edit: added more comments to the code (everything starting with % is a comment)
 

Attachments

  • triangles.png
    triangles.png
    1.6 KB · Views: 898
  • newtri.png
    newtri.png
    1.9 KB · Views: 810
  • tri.png
    tri.png
    632 bytes · Views: 852
Last edited:
Physics news on Phys.org
  • #2
On second thought, I guess what's surprising is that it works at all. Even though I've shifted it so that my triangle figure ends up within the boundaries of the original image frame, no matter where I put it, after rotation there will be corners "hanging out" of the boundaries of the original frame. So apparently Matlab is already doing something to handle that for me, but obviously not perfectly.

So, do I have to change the program so that pixels that get rotated out of the frame are simply dropped?
 
  • #3
I have no idea how to understand the 'Matlab" lanuage lol. I am still trying to figure this out :P
 
  • #4
yaa

Yaa may it's th case withthe location of the triangle for every movement u do in the for loop. tryin assigning using the regioprops and centroid of the triangle , and maintain the same coordintes of the centroid even after rotation. that might help U.


:smile:
 
  • #5
Wrong Matrix?

I haven't tested the code, but off the cuff I think your transformation matrix is wrong:

You have:
t = [ cos(pi/4) -sin(pi/4); sin(pi/4) cos(pi/4)];

I think it should be:

t = [ cos(pi/4) sin(pi/4); -sin(pi/4) cos(pi/4)];
 

FAQ: Learn How to Rotate Images in Matlab: Step-by-Step Guide | Expert Tips

How do I rotate an image in Matlab?

To rotate an image in Matlab, you can use the imrotate function. This function takes in the image as well as the desired rotation angle as parameters. Alternatively, you can also use the imrotate function with the 'bilinear' or 'bicubic' interpolation methods for a smoother rotation.

Can I rotate multiple images at once in Matlab?

Yes, you can rotate multiple images at once in Matlab by using a for loop. First, you can store all the image file names in a cell array and then use a for loop to iterate through each image and apply the imrotate function to rotate them.

How do I save the rotated image in Matlab?

To save the rotated image in Matlab, you can use the imwrite function. This function takes in the rotated image as well as the desired file name and file format as parameters. Make sure to specify the file format, such as 'jpg' or 'png', in order to save the image correctly.

Can I rotate an image by a specific angle in Matlab?

Yes, you can rotate an image by a specific angle in Matlab by using the imrotate function with the desired rotation angle as a parameter. The rotation angle can be positive or negative, indicating clockwise or counterclockwise rotation, respectively.

Is there a way to preview the rotated image before saving it in Matlab?

Yes, you can use the imshow function to preview the rotated image before saving it in Matlab. This function takes in the rotated image as a parameter and displays it in a figure window. This can help you make any adjustments or corrections to the rotated image before saving it.

Similar threads

Back
Top