Rotating an image counterclockwise

조회 수: 2 (최근 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:
Rena Berman
Rena Berman 2021년 1월 15일

(Answers Dev) Restored edit

댓글을 달려면 로그인하십시오.

답변 (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.

카테고리

Help CenterFile Exchange에서 Display Image에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by