# Rotate an image around centroid without deformation

6 views (last 30 days)
Jack on 15 Sep 2022
Commented: Rik on 15 Sep 2022
Hey everyone. I am trying to create an image of a rectangle and rotate it 360* and save all of those images to do a comparison later. However the cell expands with imrotate and I need them to all to be the same size to do cell operations later. When I perform the crop in the last line it cuts the extra pixels from right to left, then distorting the positon of rectangle I have drawn.
Does anyone have an easy method to rotate just the rectangle 360* around its centroid but maintaining a 101*101 pixel canvas. Reference image to see how it moves off centre.
Thanks. :)
image=zeros(101,101);
image(29:78,43:60)=1; %Create a rectangular image (0 is white and 1 will be black)
for i=1:1:360
rotated{i}=imrotate(image,i); %roate the image 360 degrees
rotated{i}=rotated{1,i}(1:101,1:101); %Crop the image back down to 101x101 so cell operations can be conducted later

Rik on 15 Sep 2022
I'm not a fan of using i as a variable name, nor do I like shadowing the image primitive with a variable.
Other than that, reading the documentation helps, as it provides the option to crop the resulting image instead of extending it.
base_image=zeros(101,101);
base_image(29:78,43:60)=1; %Create a rectangular image (0 is white and 1 will be black)
for deg=20 % only 1 value for this example
rotated{deg}=imrotate(base_image,deg,'nearest','crop'); %rotate the image 360 degrees
end
imshow(rotated{deg})
##### 2 CommentsShowHide 1 older comment
Rik on 15 Sep 2022
You're welcome. I consider the Matlab documentation one of the main advantages over competitors/alternatives. I would strongly urge you to try to learn to use them. A Google search will generally bring up the documentation as well.
If you have trouble with Matlab basics you may consider doing the Onramp tutorial (which is provided for free by Mathworks). That should get you up to speed with the basics, leveling you up from total beginner to beginner.

### Categories

Find more on Image Arithmetic in Help Center and File Exchange

R2021b

### Community Treasure Hunt

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

Start Hunting!

Translated by