Rotating an image counterclockwise

3 views (last 30 days)
Oah Joan
Oah Joan on 20 Nov 2018
Edited: John Kelly on 15 Jan 2021
I want to write a function myimrotateimage(image,angle) that rotates a greyscale OR color image counterclockwise by the angle specified in degrees. The function below only seems to work for greyscale images, everytime I use a color image I get an error and i am unsure why. Can anyone explain why and how I can fix it? (WITHOUT USING imrotate)
function imagerot = imrotategrey(image,angle)
[Rows, Cols] = size(image);
Diagonal = sqrt(Rows^2 + Cols^2);
Row2 = ceil(Diagonal - Rows) + 2;
Col2 = ceil(Diagonal - Cols) + 2;
image2 = zeros(Rows+Row2, Cols+Col2);
image2(ceil(Row2/2):(ceil(Row2/2)+Rows-1),ceil(Col2/2):(ceil(Col2/2)+Cols-1)) = image;
%midpoints
midx=ceil((size(image2,1)+1)/2);
midy=ceil((size(image2,2)+1)/2);
imagerot=zeros(size(image2)); % midx and midy same for both
for i=1:size(imagerot,1)
for j=1:size(imagerot,2)
x= (i-midx)*cosd(angle)+(j-midy)*sind(angle);
y=-(i-midx)*sind(angle)+(j-midy)*cosd(angle);
x=round(x)+midx;
y=round(y)+midy;
if (x>=1 && y>=1 && x<=size(image2,2) && y<=size(image2,1))
imagerot(i,j)=image2(x,y); % k degrees rotated image
end
end
end
end
  3 Comments
Rik
Rik on 20 Nov 2020
Edited: John Kelly on 15 Jan 2021
The Korean cache still had the original post:
Rena Berman
Rena Berman on 15 Jan 2021

(Answers Dev) Restored edit

Sign in to comment.

Answers (1)

Image Analyst
Image Analyst on 20 Nov 2018
Edited: Image Analyst on 20 Nov 2018
Because you got the size wrong. It should be
[Rows, Cols, numberOfColorChannels] = size(image);
Also, DO NOT use image as the name of your variable since that is already the name of a built-in function. Likewise, don't use any other function either, like size, sum, min, max, etc.

Categories

Find more on Display Image in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!