Matlab equivalent of python code to rotate an image

What is the matlab equivalent for the following python code:
image = cv2.flip(imgage, 1)
row,col = image.shape
center=tuple(np.array([row,col])/2)
rot_mat = cv2.getRotationMatrix2D(center,-130,1.0)
image = cv2.warpAffine(image, rot_mat, (col,row))
new_image=cv2.warpPerspective(imgage, other_matrix, (row, col), cv2.INTER_NEAREST)
I already have,
image=flipdim(image ,1); % is this correct??
rot_cen=[size(image,1)/2, size(image,2)/2];
rot_ang=deg2rad(-130);
rot_mat=affine2d([cos(rot_ang) sin(rot_ang) 0; -sin(rot_ang) cos(rot_ang) 0; ((1-cos(rot_ang))*rot_cen(1)-sin(rot_ang)*rot_cen(2)) (sin(rot_ang)*rot_cen(1)+(1-cos(rot_ang))*rot_cen(2)) 1]);
image=imwarp(image,rot_mat);
but I'm quite unsure about it (and one part is missing..)

 Accepted Answer

@Marcel345614, thanks for reinforcing why I use MATLAB instead of Python! 😄
image=flipdim(image ,1); % is this correct??
No that is not correct. You should never call a variable image (or any other function name) since image is the name of a very important built-in function. Simply do
rotatedImage = imrotate(inputImage, angleInDegrees);

8 Comments

Thanks, this already helped me! But one problem that occurs is that the image is cropped differently when rotating:
Original-Image(300x300)
Matlab-Image(after rotation 424 x 423):
img=imrotate(imageIN,-130);
And the image from Python (still 300 x 300):
How can I solve this problem?
For one thing, it looks like the image in MATLAB did not have it's black cross at the center of the image, which is the center of rotation. There are also options to say, when you rotate the image, if you want the image clipped, or if you want the canvass expanded to take the entire image.
Attach your original image.
And for the following line I have not found a Matlab function:
new_image=cv2.warpPerspective(imgage, other_matrix, (row, col), cv2.INTER_NEAREST)
My matrix is 3x3 matrix (not a normal transformation matrix).
Sorry I didn't saw your previous post.
Here is the original image.
@Marcel345614 as you can see by the axes labels, the image size is not changed for a cropping rotation, and the center of the image is at the center of rotation so there is not shifting and assymmetry like you showed. If you post your code I can show you where you went wrong.
rgbImage = imread('test.jpg');
subplot(2, 2, 1);
imshow(rgbImage);
impixelinfo;
axis('on', 'image')
title('Original RGB Image');
% Rotate it 45 degrees, cropping to original image.
rgbImage2 = imrotate(rgbImage, 45, "bicubic", "crop");
subplot(2, 2, 2);
imshow(rgbImage2);
impixelinfo;
axis('on', 'image')
title('Rotated Image, Same Canvass so Cropped')
% Rotate it 45 degrees, Expanding canvass to keep entire original image.
rgbImage3 = imrotate(rgbImage, 45, "bicubic", "loose");
subplot(2, 2, 3);
imshow(rgbImage3);
impixelinfo;
axis('on', 'image')
title('Rotated Image, Expanded Canvass')
Thank you so much for your answer! This helped a lot!
About the image rotation center:
I noticed that even the original image is not plotted correctly when I use imshow(it is shifted upwards in the figure as the rotated picture in my previous post)... When I use the other image-plotting tools, this is not the case. This is so weird!
(Apart from this imshow-problem, I now get the correct image.)
And do you have an idea for the last python line (warp perspective for an arbitrary matrix with m_{33}=1 and m_{i j} not 1 for i,j=1,2 ?
Honestly I don't do much or any warping in MATLAB so you'd have to figure that out just like I'd have to, so I'll leave it to you.
imshow() does not shift the image. I think you're not using the image you thought you were, like maybe you saved the figure with saveas() or print() or exportgraphics() rather than imwrite(), so you're getting some of the figure padding around the image, which depends on how you size and shape the figure on your screen.
If your original question is basically answered, can you click on the Accept this answer link for the best answer? Thanks in advance.
I have no idea what the problem with imshow was. I restarted Matlab(I know this would always be the best problem solution ;-) ) and now it works properly.
Thanks for your help!

Sign in to comment.

More Answers (1)

If you are using imwarp then you have the image processing toolbox. Try looking at imrotate.

Products

Release

R2021b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!