clc;clf;
A=imread('countryside.jpg');
figure(1);imshow(A);
angle1=randi([1 359],1,1);
if angle<1
disp('choose angle > 1º');B4=A;
return; end;
[sy sx sz]=size(A);
xc=floor(sx/2);
yc=floor(sy/2);
[x1 y1]=ginput(1);
hold on;figure(1);plot(x1,y1,'r*','MarkerSize',10);
hold on;figure(1);plot(xc,yc,'ro','MarkerSize',10);
if (x1==xc)
x1=x1+2*(randi([0 1],1,1)-.5); end;
if (y1==yc)
y1=y1+2*(randi([0 1],1,1)-.5); end;
ks=0;
if (x1<xc & y1<yc) ks=1; end;
if (x1>xc & y1<yc) ks=2; end;
if (x1>xc & y1>yc) ks=3; end;
if (x1<xc & y1>yc) ks=4; end;
t=0:2*pi/100:2*pi;
switch ks
case 1
D=norm([x1 y1]-[sx sy]);
dx=D+x1-sx;
alpha=atand((sx-x1)/(sy-y1));
s=(2*D^2-2*D^2*cosd(alpha))^.5;
dy=(s^2-(sx-x1)^2)^.5;
dx=floor(dx);dy=floor(dy);
B=padarray(A,[dy,dx],'both');
figure(2);imshow(B);
[sby sbx sbz]=size(B);
hold on;figure(2);plot(x1+dx,y1+dy,'r*','MarkerSize',10);
hold on;figure(2);plot(xc+dx,yc+dy,'ro','MarkerSize',10);
x0d=D*cos(t); xd=x0d+dx+x1;
y0d=D*sin(t); yd=y0d+dy+y1;
hold all;figure(2);plot(xd,yd,'r','LineWidth',1.5);
hold all;figure(2);plot([1:1:sbx],ones(1,sbx).*(dy+y1),'w','LineWidth',1.5);
hold all;figure(2);plot(ones(1,sby).*(dx+x1),[1:1:sby],'w','LineWidth',1.5);
dx2=2*D-sbx;dx2=floor(dx2);
dy2=2*D-sby;dy2=floor(dy2);
B3=padarray(B,[dy2 dx2],50,'pre');
[sb3y sb3x sb3z]=size(B3);
figure(4);imshow(B3);
hold all;figure(4);plot(x1+dx+dx2,y1+dy+dy2,'r*','MarkerSize',10);
hold all;figure(4);plot(xc+dx+dx2,yc+dy+dy2,'ro','MarkerSize',10);
xd2=xd+dx2;
yd2=yd+dy2;
hold all;figure(4);plot(xd2,yd2,'r','LineWidth',2.5);
hold all;figure(4);plot([1:1:sb3x],ones(1,sb3x).*(dy+y1+dy2),'w','LineWidth',1.5);
hold all;figure(4);plot(ones(1,sb3y).*(dx+x1+dx2),[1:1:sb3y],'w','LineWidth',1.5);
case 2
D=norm([x1 y1]-[0 sy]);
dx=D-x1;
alpha=atand((sy-y1)/(sx-x1));
s=(2*D^2-2*D^2*cosd(alpha))^.5;
dy=(s^2-(sx-x1)^2)^.5;
dy=D-y1;
dx=floor(dx);dy=floor(dy);
B=padarray(A,[dy,dx],'pre');
figure(2);imshow(B);
[sby sbx sbz]=size(B);
hold on;figure(2);plot(x1+dx,y1+dy,'r*','MarkerSize',10);
hold on;figure(2);plot(xc+dx,yc+dy,'ro','MarkerSize',10);
x0d=D*cos(t); xd=x0d+dx+x1;
y0d=D*sin(t); yd=y0d+dy+y1;
hold all;figure(2);plot(xd,yd,'r','LineWidth',1.5);
hold all;figure(2);plot([1:1:sbx],ones(1,sbx).*(dy+y1),'w','LineWidth',1.5);
hold all;figure(2);plot(ones(1,sby).*(dx+x1),[1:1:sby],'w','LineWidth',1.5);
dx2=D-(sx-x1);dx2=floor(dx2);
dy2=D-(sy-y1);dy2=floor(dy2);
B3=padarray(B,[dy2 dx2],50,'post');
[sb3y sb3x sb3z]=size(B3);
figure(4);imshow(B3);
xc2=floor(sb3x/2);yc2=floor(sb3y/2);
hold all;figure(4);plot(xc2,yc2,'r*','MarkerSize',10);
hold all;figure(4);plot(xc2-(x1-xc),yc2-(y1-yc),'ro','MarkerSize',10);
xd2=xd+xc2;
yd2=yd+yc2;
hold all;figure(4);plot(xd,yd,'r','LineWidth',2.5);
hold all;figure(4);plot([1:1:sb3x],ones(1,sb3x).*yc2,'w','LineWidth',1.5);
hold all;figure(4);plot(ones(1,sb3y).*xc2,[1:1:sb3y],'w','LineWidth',1.5);
case 3
D=norm([x1 y1]-[0 0]);
dx=D-x1;
dy=D-y1;
dx=floor(dx);dy=floor(dy);
B=padarray(A,[dy dx],'pre');
figure(2);imshow(B);
[sby sbx sbz]=size(B);
hold on;figure(2);plot(x1+dx,y1+dy,'r*','MarkerSize',10);
hold on;figure(2);plot(xc+dx,yc+dy,'ro','MarkerSize',10);
x0d=D*cos(t); xd=x0d+dx+x1;
y0d=D*sin(t); yd=y0d+dy+y1;
hold all;figure(2);plot(xd,yd,'r','LineWidth',1.5);
hold all;figure(2);plot([1:1:sbx],ones(1,sbx).*(dy+y1),'w','LineWidth',1.5);
hold all;figure(2);plot(ones(1,sby).*(dx+x1),[1:1:sby],'w','LineWidth',1.5);
dx2=2*D-sbx;dx2=floor(dx2);
dy2=2*D-sby;dy2=floor(dy2);
B2=padarray(B,[0 dx2],50,'post');
figure(3);imshow(B2);
B3=padarray(B2,[dy2 0],100,'post');
[sb3y sb3x sb3z]=size(B3)
figure(4);imshow(B3);
xc2=floor(sb3x/2);yc2=floor(sb3y/2);
hold all;figure(4);plot(xc2,yc2,'r*','MarkerSize',10)'
hold all;figure(4);plot(xc2-(x1-xc),yc2-(y1-yc),'ro','MarkerSize',10)'
xd2=x0d+xc2;
yd2=y0d+yc2'
hold all;figure(4);plot(xd2,yd2,'r','LineWidth',2.5)'
hold all;figure(4);plot([1:1:sb3x],ones(1,sb3x).*yc2,'w','LineWidth',1.5);
hold all;figure(4);plot(ones(1,sb3y)*xc2,[1:1:sb3y],'w','LineWidth',1.5);
case 4
D=norm([x1 y1]-[sx 0])
dx=D+x1-sx
alpha=atand((sx-x1)/y1)
s=(2*D^2-2*D^2*cosd(alpha))^.5
dy=(s^2-(sx-x1)^2)^.5
dx=floor(dx);dy=floor(dy);
B=padarray(A,[dy,dx],'both');
figure(2);imshow(B)
[sby sbx sbz]=size(B)
hold on;figure(2);plot(x1+dx,y1+dy,'r*','MarkerSize',10)
hold on;figure(2);plot(xc+dx,yc+dy,'ro','MarkerSize',10)
x0d=D*cos(t); xd=x0d+dx+x1;
y0d=D*sin(t); yd=y0d+dy+y1;
hold all;figure(2);plot(xd,yd,'r','LineWidth',1.5);
hold all;figure(2);plot([1:1:sbx],ones(1,sbx).*(dy+y1),'w','LineWidth',1.5)
hold all;figure(2);plot(ones(1,sby).*(dx+x1),[1:1:sby],'w','LineWidth',1.5)
dx2=2*D-sbx;dx2=floor(dx2);
dy2=2*D-sby;dy2=floor(dy2);
B2=padarray(B,[0 dx2],50,'pre');
figure(3);imshow(B2);
B3=padarray(B2,[dy2 0],100,'post');
[sb3y sb3x sb3z]=size(B3)
figure(4);imshow(B3);
hold all;figure(4);plot(x1+dx+dx2,y1+dy,'r*','MarkerSize',10)
hold all;figure(4);plot(xc+dx+dx2,yc+dy,'ro','MarkerSize',10)
xd2=xd+dx2
yd2=yd
hold all;figure(4);plot(xd2,yd2,'r','LineWidth',2.5)
hold all;figure(4);plot([1:1:sb3x],ones(1,sb3x).*(dy+y1),'w','LineWidth',1.5)
hold all;figure(4);plot(ones(1,sb3y).*(dx+x1+dx2),[1:1:sb3y],'w','LineWidth',1.5)
otherwise
disp('\nerror\n')
return
end
imwrite(B3,'padded_image.jpg')
B4=imrotate(B3,angle);
[sb4y sb4x sb4z]=size(B4);
figure(5);imshow(B4);
hold all;figure(5);plot(floor(sb4x/2),floor(sb4y/2),'r*','MarkerSize',15);
xd4=x0d+floor(sb4x/2);
yd4=y0d+floor(sb4y/2);
hold all;figure(5);plot(xd4,yd4,'r','LineWidth',2.5);
if angle>0 str1='CCW'; end;
if angle<0 str1='CW'; end;
figure(5);legend(['angle : ' num2str(angle) ' º ' str1],['Radius : ' num2str(floor(D)) ' pixels'],'Location','Northwest');
imwrite(B4,'im_padded_rotated.jpg');
Best Answer