# how to rotate image by using pixel by pixel?

233 views (last 30 days)
mohamed yaseen on 4 Mar 2016
Hi please help me I have a problem in image rotation by using pixel by pixel. Write this code for rotation but in the left image and upper image problem the image some part is lost But the right image and down the image is right I wont to same thing in the left or upper image. This is my code for roration pixel by pixel.
clc;
clear;
x=imresize(x,[200 200]);
x3=double(x);
for i=1:400
for j=1:400
x1(i,j)=round((i-100).*cosd(45)+(j-100).*sind(45)+100);
x2(i,j)=round(-(i-100).*sind(45)+(j-100).*cosd(45)+100);
if(x1(i,j)<=200)&&(x1(i,j)>0)&&(x2(i,j)<=200)&&(x2(i,j)>0)
a(i,j,:)=x3(x1(i,j),x2(i,j),:);
end;
end;
end;
image(uint8(a));

Image Analyst on 4 Mar 2016
Just have a double loop
% Exercise left for the poster: Determine outputColumns and outputRows.
for outputCol = 1 : outputColumns
for outputRow = 1 : outputRows
inputRow = outputRow * cosd(angle) - outputCol * sind(angle);
inputCol = outputRow * sind(angle) + outputCol * cosd(angle);
% Exercise left for the poster: Make sure input row and column are inside the image.
% To be simple, just round the row and column. To be more accurate do bilinear interpolation.
outputImage(outputRow, outputCol) = inputImage(inputRow, inputCol);
end
end
It sounds like homework so I've left some of the parts for you to complete. By the way, you can't multply the input matrix by the rotation matrix or else you will get "holes" (unassigned pixels) in your output image.

mohamed yaseen on 5 Mar 2016
:( Sorry I can’t understand this code you can see my code and write again code by solving the problem
Image Analyst on 5 Mar 2016
You're going to have to decide how big your canvass needs to be to accommodate the rotated image and then shift your output coordinates by half that amount. You know that your canvass will be bigger by this ratio
scaleFactor = sqrt(rows^2+columns^2);
Then figure out how much to add to i and j to shift your center. You're a smart engineer so I know you can do it.

Guillaume on 4 Mar 2016
Why aren't you using imrotate?
a = imrotate(x, 45, 'nearest', 'loose')
would be the equivalent to what you're doing.

mohamed yaseen on 4 Mar 2016
thanx Guillaume but i wont this rotation pixel by pixel it is home work in my colage :(
Image Analyst on 4 Mar 2016
And how do you think imrotate() does it? Internally it does it pixel-by-pixel.
mohamed yaseen on 4 Mar 2016
no just by using for loop i dont wont imrotae() function :( plase help me i search in google but i cant found this answer.

Fayyaz Ahmad on 27 Mar 2020 at 7:07
%
% Please find the required code
%
clc;
clear;
close all;
figure(1)
subplot(2,1,1)
image(x);
axis equal
[m1,m2,m3]=size(x);
P1 = [ 0 1
-1 0];
P2 = [0 -1
1 0];
th = 30;
A = [cosd(th) -sind(th); sind(th) cosd(th)];
y = zeros(xmax,ymax,3)+255;
for i=1:m1
for j=1:m2
v = ceil(P2*A*P1*[i,j]'+[q1,q2]');
if(v(1)==0); v(1)=1; end
if(v(2)==0); v(2)=1; end
y(v(1),v(2),:)=x(i,j,:);
end
end
subplot(2,1,2)
image(uint8(y));
axis equal
w1 = [m1 m1 0
0 m2 m2];
w2 = P2*A*P1*w1;
ind1 = find(w2(1,:)<0);
ind2 = find(w2(2,:)<0);
xmax = ceil(max(w2(1,:)));
ymax = ceil(max(w2(2,:)));
q1 = 0;
q2 = 0;
if length(ind1)>0
q1 = -min(w2(1,find(w2(1,:)<0)));
xmax = ceil(max(w2(1,:)+q1));
end
if length(ind2)>0
q2 = -min(w2(2,find(w2(2,:)<0)));
ymax = ceil(max(w2(2,:)+q2));
end
end