Matlab图像做图像变换,包括放缩和旋转。
- 对给定的两幅图像(灰度、真彩)进行坐标变换(同时缩放、旋转)并计算变换后图像的文件和信息数据的改变;
要求:
1) 旋转:以图像左下角为圆心点,逆时针旋转300
2) 缩放:sx=0.8,sy=1.2
x1=imread('lena256.bmp');
x2=imread('tiger.bmp');
[L1,L2]=size(x1);
ylen1=round(L1*1.2);
xlen1=round(L2*0.8);
x11(1:xlen1,1:ylen1)=uint8(255);%旋转
x22(1:xlen1,1:ylen1,1)=uint8(255);
x22(1:xlen1,1:ylen1,2)=uint8(255);
x22(1:xlen1,1:ylen1,3)=uint8(255);
for m=1:L1
for n=1:L2
x11(round(0.8*m),round(1.2*n))=x1(m,n);
x22(round(0.8*m),round(1.2*n),1)=x2(m,n,1);
x22(round(0.8*m),round(1.2*n),2)=x2(m,n,2);
x22(round(0.8*m),round(1.2*n),3)=x2(m,n,3);
end
end
for r=1:xlen1%插值补全
for s=1:ylen1
if((x11(r,s)==255)&&(r>1)&&(r<xlen1)&&(s>1)&&(s<ylen1))
x11(r,s)=round((0.5*x11(r,s-1)+0.5*x11(r,s+1)));
x22(r,s,1)=round((0.5*x22(r,s-1,1)+0.5*x22(r,s+1,1)));
x22(r,s,2)=round((0.5*x22(r,s-1,2)+0.5*x22(r,s+1,2)));
x22(r,s,3)=round((0.5*x22(r,s-1,3)+0.5*x22(r,s+1,3)));
end
end
end
[L3,L4]=size(x11);
L=sqrt(L3^2+L4^2);
sita=pi/6;
fai=sita+atan(0.8/1.2);
ylen=round(L*cos(fai))+2*round(L3*sin(sita));
xlen=round(L*sin(fai));
x0=L3;
y0=1;
dx=L*sin(fai)-L3;
dy=L3*sin(sita);
subplot(2,2,1)
imshow(x1)
subplot(2,2,3)
imshow(x2)
y(1:xlen,1:ylen)=uint8(255);%旋转
y1(1:xlen,1:ylen,1)=uint8(255);
y1(1:xlen,1:ylen,2)=uint8(255);
y1(1:xlen,1:ylen,3)=uint8(255);
for i=1:L3
for j=1:L4
y(round((x0+(i-x0)*cos(sita)-(j-y0)*sin(sita)+dx)),round((y0+(j-y0)*cos(sita)+(i-x0)*sin(sita)+dy)))=x11(i,j);
y1(round((x0+(i-x0)*cos(sita)-(j-y0)*sin(sita)+dx)),round((y0+(j-y0)*cos(sita)+(i-x0)*sin(sita)+dy)),1)=x22(i,j,1);
y1(round((x0+(i-x0)*cos(sita)-(j-y0)*sin(sita)+dx)),round((y0+(j-y0)*cos(sita)+(i-x0)*sin(sita)+dy)),2)=x22(i,j,2);
y1(round((x0+(i-x0)*cos(sita)-(j-y0)*sin(sita)+dx)),round((y0+(j-y0)*cos(sita)+(i-x0)*sin(sita)+dy)),3)=x22(i,j,3);
end
end
for r=1:xlen%插值补全
for s=1:ylen
if((y(r,s)==255)&&(r>1)&&(r<xlen)&&(s>1)&&(s<ylen))
y(r,s)=round((0.5*y(r,s-1)+0.5*y(r,s+1)));
y1(r,s,1)=round((0.5*y1(r,s-1,1)+0.5*y1(r,s+1,1)));
y1(r,s,2)=round((0.5*y1(r,s-1,2)+0.5*y1(r,s+1,2)));
y1(r,s,3)=round((0.5*y1(r,s-1,3)+0.5*y1(r,s+1,3)));
end
end
end
imwrite(y,'变换过后的人.bmp');
imwrite(y1,'变换过后的老虎.bmp');
subplot(2,2,2)
imshow(y)
subplot(2,2,4)
imshow(y1)
将变换后的图像写出文件中,并通过此文件获取相关信息与上述1计算的结果比较之;
imfinfo('lena256.bmp')
imfinfo('tiger.bmp')
imfinfo('变换过后的人.bmp')
imfinfo('变换过后的老虎.bmp')