how to rotate image by using pixel by pixel?
7 vues (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (4)
Image Analyst
le 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 commentaires
Seyedeh Bagheri
le 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
le 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
le 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 commentaires
Fego Etese
le 2 Août 2020
Modifié(e) : Fego Etese
le 2 Août 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
le 10 Août 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
le 4 Mar 2016
a = imrotate(x, 45, 'nearest', 'loose')
would be the equivalent to what you're doing.
3 commentaires
Image Analyst
le 4 Mar 2016
And how do you think imrotate() does it? Internally it does it pixel-by-pixel.
Fayyaz Ahmad
le 31 Juil 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 commentaires
Rik
le 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
le 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?
Voir également
Catégories
En savoir plus sur Image Segmentation and Analysis dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!



