how to rotate image by using pixel by pixel?
Show older comments
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=imread('2.jpg');
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));

Answers (4)
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.
4 Comments
mohamed yaseen
on 5 Mar 2016
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.
Seyedeh Bagheri
on 13 Mar 2022
Would you please complete your double loop code? Because I faced this error 'subscript indices must either be real posetive integers or logicals.'
Image Analyst
on 13 Mar 2022
@Seyedeh Bagheri, it's so simple you probably have done it by now, but it would probably be something like
[inputRows, inputColumns, inputColors] = size(inputImage)
for outputCol = 1 : outputColumns
for outputRow = 1 : outputRows
inputRow = round(outputRow * cosd(angle) - outputCol * sind(angle));
if inputRow < 1 || inputRow > inputRows
% It's outside of the input image, so skip it.
continue;
end
inputCol = round(outputRow * sind(angle) + outputCol * cosd(angle));
if inputCol < 1 || inputCol > inputColumns
% It's outside of the input image, so skip it.
continue;
end
% 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
Fayyaz Ahmad
on 27 Mar 2020
1 vote
%
% Please find the required code 

%
clc;
clear;
close all;
x=imread('2.jpeg');
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)];
[xmax, ymax,q1,q2] = frame_adjust(m1,m2,A,P1,P2);
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
function [xmax,ymax,q1,q2] = frame_adjust(m1,m2,A,P1,P2)
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
8 Comments
Fego Etese
on 30 Jul 2020
Hello Fayyaz Ahmad, please I am trying to apply the same rotation, but not to an image exactly, but to a set of pixels of an image, I will need to rotate the pixels while keeping those pixels within the image, Please how can i do that?
Thanks
Image Analyst
on 30 Jul 2020
You can get the coordinates of the pixels, then use the rotation matrix (see Wikipedia) to get new rotated coordinates. Then write the pixels in to the existing image at the new locations. Start your own question with your actual data attached if you need more help.
Fayyaz Ahmad
on 31 Jul 2020
%I have modified the code and it will help you plot the photo pixel by pixel
clc;
clear;
close all;
x=imread('2.jpeg');
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)];
[xmax, ymax,q1,q2] = frame_adjust(m1,m2,A,P1,P2);
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,:);
% this part will plot pixel by pixel
subplot(2,1,2)
image(uint8(y));
axis equal
drawnow;
end
end
%subplot(2,1,2)
%image(uint8(y));
%axis equal
function [xmax,ymax,q1,q2] = frame_adjust(m1,m2,A,P1,P2)
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
Fego Etese
on 2 Aug 2020
Hello Fayyaz, sorry this is coming late, but I have tried the code and it takes a really long time to run, I don't know why, what i really want to do is to be able to rotate the image and also using the same rotation algorithm for a set of points in the image below just to find their co-ordinates.
Thank you Image Analyst for your opinion sir, when i tried to use the rotation matrix normally, it turned some of the rotated coordinates into negative values, please how can i do it that there are no negative values, i was thinking of looking for the maximum row negative value for example rotated coordinates such as [-15, 105], and assuming for all the row values -15 is the largest, i was thinking of adding 15 to all the rows so as to shift the coordinates to fit in the image, is this the right approach?
These are the points i want to rotate
This is an orientation map for the fingerprint in a non-rotated position, but i want to get the orientation map for the fingerprint in the rotated position, so i will need to rotate the image and get the orientation map, but that intrduxes artfifacts and a few wrong orientations, i was thinking of just adding the rotation value to each of the orientation values, do you think that wil work?Thank you, I appreciate your time and effort
Fego Etese
on 2 Aug 2020
This is what I've been able to do so far, but after rotation the points have been translated, I'm guessing it is so because imrotate rotated the whole image pixels, while i rotated only specific points and plotted on the rotated image, is this correct?
Also when i add a negative rotation value to the orientation map valuse, I get a negative angle, please how can i resolve that, should i just turn the angle to be positive or do i have to subtract from 360 to get the positive angle?

Thank you
Image Analyst
on 2 Aug 2020
When you rotate an image, some of the points will rotate out of the original frame of course. You have to decide what to do with those.
- One approach is to just ignore any points that have been rotated out of the original field of view.
- The other approach is to keep them but you'll need to enlarge the canvass, essentially making a larger image, but of course the coordinate system will now be shifted because what was in column 1 was column 1, but now it may be that x=-4 or -30 might be column 1, and you have to be aware of that and shift the points properly.
Fego Etese
on 2 Aug 2020
Edited: Fego Etese
on 2 Aug 2020
Thank you Image Analyst, I have one other issue, I am also using the distance between the points for matching, do you think i could use the scale factor of the enlarged image to the original to reduce the distance between the points? I am not sure if this will give me the distance berween the points before the rotation, what do you think sir?
I will have to keep all points that have been rotated out of the image, so that i can use them for the matching, but the distance between them may increase.
Imran Riaz
on 10 Aug 2022
@Fayyaz Ahmad There is error in ur code(function [xmax,ymax,q1,q2] = frame_adjust(m1,m2,A,P1,P2)
How to remove it.
Guillaume
on 4 Mar 2016
a = imrotate(x, 45, 'nearest', 'loose')
would be the equivalent to what you're doing.
3 Comments
mohamed yaseen
on 4 Mar 2016
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
Fayyaz Ahmad
on 31 Jul 2020
0 votes
Please find the code. It will help you plot rotated photo pixel by pixel
clc;
clear;
close all;
x=imread('2.jpeg');
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)];
[xmax, ymax,q1,q2] = frame_adjust(m1,m2,A,P1,P2);
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,:);
% this part will plot pixel by pixel
subplot(2,1,2)
image(uint8(y));
axis equal
drawnow;
end
end
%subplot(2,1,2)
%image(uint8(y));
%axis equal
function [xmax,ymax,q1,q2] = frame_adjust(m1,m2,A,P1,P2)
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
4 Comments
Image Analyst
on 3 Aug 2020
Here is a much faster way.
clc; % Clear the command window.
fprintf('Beginning to run %s.m.\n', mfilename);
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
x=imread('moon.tif');
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)];
[xmax, ymax,q1,q2] = frame_adjust(m1,m2,A,P1,P2);
y = zeros(xmax,ymax,3)+255;
for i=1:m1
fprintf('Row = %d\n', i);
for j=1:m2
% fprintf('Row = %d, column = %d\n', i, j);
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,:);
% this part will plot pixel by pixel
% subplot(2,1,2)
% image(uint8(y));
% axis equal
% drawnow;
end
end
subplot(2,1,2)
image(uint8(y));
axis equal
drawnow;
fprintf('Done running %s.m.\n', mfilename);
function [xmax,ymax,q1,q2] = frame_adjust(m1,m2,A,P1,P2)
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

However you scanned in the wrong direction. You scanned the input image and "sent" pixels to the output image. That is why your output image has white "holes" in it where no pixel ever got sent to. The proper way to do it is to scan the output image and decide where to "pull" the input pixel from. That way every pixel in the output image will have a value.
Fayyaz Ahmad
on 9 Aug 2020
To place this piece of code in outer loop make the plotting faster.
subplot(2,1,2)
image(uint8(y));
axis equal
drawnow;
It will plot figure row (of pixel) by row.
When we rotate a figure through an angle that is different from 90,180, 270 we can not maintain the quality of picture. One needs to use some interpolation method to maintain the quality of figure.
Rik
on 27 Mar 2022
I have 28x28 pixel data and I want to rotate it about vertical axis. Can you please help me with that? Thanks and Regards
Image Analyst
on 27 Mar 2022
manik, what exactly does that mean? Do you mean rotating out of the plane of the screen, like into the Z dimension?
Categories
Find more on Image Segmentation and Analysis in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!