how to rotate image by using pixel by pixel?

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));

2 Comments

sir i have error on this programming
How do you expect anyone to help you with so little to go on?
Have a read here and here. It will greatly improve your chances of getting an answer.

Sign in to comment.

Answers (4)

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

:( Sorry I can’t understand this code you can see my code and write again code by solving the problem
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.
Would you please complete your double loop code? Because I faced this error 'subscript indices must either be real posetive integers or logicals.'
@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

Sign in to comment.

%
% 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

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
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.
%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
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
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
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.
  1. One approach is to just ignore any points that have been rotated out of the original field of view.
  2. 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.
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.
@Fayyaz Ahmad There is error in ur code(function [xmax,ymax,q1,q2] = frame_adjust(m1,m2,A,P1,P2)
How to remove it.

Sign in to comment.

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

3 Comments

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

Sign in to comment.

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

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.
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.
Comment posted as flag by @Manik Kumar:
I have 28x28 pixel data and I want to rotate it about vertical axis. Can you please help me with that? Thanks and Regards
manik, what exactly does that mean? Do you mean rotating out of the plane of the screen, like into the Z dimension?

Sign in to comment.

Tags

Asked:

on 4 Mar 2016

Commented:

on 10 Aug 2022

Community Treasure Hunt

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

Start Hunting!