how to rotate image by using pixel by pixel?
52 views (last 30 days)
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));
2 Comments
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
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
%
% 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 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
Image Analyst
on 4 Mar 2016
And how do you think imrotate() does it? Internally it does it pixel-by-pixel.
Fayyaz Ahmad
on 31 Jul 2020
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
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?
See Also
Categories
Find more on Visual Exploration 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!