Rotating an image counterclockwise

2 visualizaciones (últimos 30 días)
Oah Joan
Oah Joan el 20 de Nov. de 2018
Editada: John Kelly el 15 de En. de 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 comentarios
Rik
Rik el 20 de Nov. de 2020
Editada: John Kelly el 15 de En. de 2021
The Korean cache still had the original post:
Rena Berman
Rena Berman el 15 de En. de 2021

(Answers Dev) Restored edit

Iniciar sesión para comentar.

Respuestas (1)

Image Analyst
Image Analyst el 20 de Nov. de 2018
Editada: Image Analyst el 20 de Nov. de 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.

Categorías

Más información sobre Display Image en Help Center y File Exchange.

Community Treasure Hunt

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

Start Hunting!

Translated by