Rotating an image counterclockwise

4 次查看(过去 30 天)
Oah Joan
Oah Joan 2018-11-20
编辑: John Kelly 2021-1-15
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 个评论
Rik
Rik 2020-11-20
编辑:John Kelly 2021-1-15
The Korean cache still had the original post:

请先登录,再进行评论。

回答(1 个)

Image Analyst
Image Analyst 2018-11-20
编辑:Image Analyst 2018-11-20
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.

Community Treasure Hunt

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

Start Hunting!

Translated by