In Matlab, write a function g = imgRotate(f, theta, mode) for performing image r
ID: 2085829 • Letter: I
Question
In Matlab, write a function g = imgRotate(f, theta, mode) for performing image rotation about the center of the image, f is the original grayscale image, theta is the rotation angle in degrees, with positive angles going counterclockwise. If mode = 'crop', the rotated image should be cropped (about its center) to the same size as the input image. This should be the default. If mode = 'full', the output image should be the smallest size capable of containing the full rotated input image for the given angle. The background of the rotated image should be black. To test your function, rotate a test image (one of those on BBLearn) by 30 degrees, -45 degrees, and 110 degrees. Use full mode, but include at least one test using crop mode. Show your results. Include your Matlab source code in your homework submission.Explanation / Answer
image=torso;
%image padding
[Rows, Cols] = size(image);
Diagonal = sqrt(Rows^2 + Cols^2);
RowPad = ceil(Diagonal - Rows) + 2;
ColPad = ceil(Diagonal - Cols) + 2;
imagepad = zeros(Rows+RowPad, Cols+ColPad);
imagepad(ceil(RowPad/2):(ceil(RowPad/2)+Rows-1),ceil(ColPad/2):(ceil(ColPad/2)+Cols-1)) = image;
degree=45;
%midpoints
midx=ceil((size(imagepad,1)+1)/2);
midy=ceil((size(imagepad,2)+1)/2);
imagerot=zeros(size(imagepad));
%rotation
for i=1:size(imagepad,1)
for j=1:size(imagepad,2)
x=(i-midx)*cos(degree)-(j-midy)*sin(degree);
y=(i-midx)*sin(degree)+(j-midy)*cos(degree);
x=round(x)+midx;
y=round(y)+midy;
if (x>=1 && y>=1)
imagerot(x,y)=imagepad(i,j); % k degrees rotated image
end
end
end
A=imread('panda2.jpg');
x1=zeros([size(A,1)*size(A,2) 1]);
x2=zeros([size(A,2)*size(A,1) 1]);
%Specify the degree
deg=90;
%Change the image size
C=uint8(zeros([size(A,1) size(A,2) 3 ]));
m=1;
%Find the midpoint
midx=ceil((size(C,1)+1)/2);
midy=ceil((size(C,2)+1)/2);
for i=1:size(A,1)
i1=i-midx;
for j=1:size(A,2)
%convert from cartesian to polar
[t,r]=cart2pol(i1,j-midy);
%Convert from radians to degree and add the degree value
t1=radtodeg(t)+deg;
%Convert from degree to radians
t=degtorad(t1);
%Convert to Cartesian Co-ordinates
[x,y]=pol2cart(t,r);
x1(m)=round(x+midx);
x2(m)=round(y+midy);
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.