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 Center および File ExchangeDisplay Image についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by