图像几何变换
一、实验目的
(1)学习几种常见的图像几何变换,并通过实验体会几何变换的效果;
(2)掌握图像平移、剪切、缩放、旋转、镜像、错切等几何变换的算法原理及编
程实现
(3)掌握matlab编程环境中基本的图像处理函数
(4)掌握图像的复合变换
二、涉及知识点
(1)图像几何变换不改变图像像素的值,只改变像素所在的几何位置
(2)图像裁剪imcrop函数,语法格式为:
B=imcrop(A);交互式用鼠标选取区域进行剪切
B=imcrop(A,[left top right bottom]);针对指定的区域[left top right bottom]进行剪切
(3)图像缩放imresize函数,语法格式为:
B = imresize(A,m,method)
这里参数method用于指定插值的方法,可选用的值为'nearest'(最邻近法),'bilinear'(双线性插值),'bicubic'(双三次插值),默认为'nearest'。
B = imresize(A,m,method)返回原图A的m倍放大的图像(m小于1时效果是
缩小)。
(4)图像旋转imrotate函数,语法格式为:
B = imrot ate(A,angle,’crop’),参数crop用于指定裁剪旋转后超出图像的部分。
三、实验内容
(1)将图像hehua.bmp裁剪成200X200大小,并保存
(2)制作动画,将一幅图像逐渐向左上角平移移出图像区域,空白的地方用白色
填充
(3)利用剪切图像函数制作动画
(4)将图像分别放大1.5倍和缩小0.8倍,插值方法使用最近邻域法和双线性插
值法,对比显示图像。
(5)将图像水平镜像,再顺时针旋转45度,显示旋转后的图像。
(6)将图像分别进行水平方向30度错切,垂直方向45度错切,分别显示结果
具体实现:
1.将图像hehua.bmp裁剪成200X200大小,并保存
I=imread('hehua.bmp');
n=size(I);
figure;
subplot(1,2,1);
imshow(I);
title('原图');
I=double(I);
I1=zeros(200,200,n(3));
I1=I(1:200,1:200,1:n(3));
subplot(1,2,2);
imshow(uint8(I1));
title('裁剪');
imwrite(uint8(I1),'hehua1.bmp','bmp');
2.制作动画,将一幅图像逐渐向左上角平移移出图像区域,空白的地方用白色填充
I=imread('hehua1.bmp');
[m,n,l]=size(I);
figure;
imshow(I);
title('原图');
I=double(I);
for i=1:10
x=10*i;
y=10*i;
subplot(3,4,i);
G=zeros(m,n,l)+255;
for i=1:m-x-1
for j=1:n-y-1
for k=1:l
G(i,j,k)=I(i+x+1,j+y+1,k);
end
end
end
imshow(uint8(G));
title('平移图');
end
3. 利用剪切图像函数制作动画I=imread('hehua.bmp');
[m,n,l]=size(I);
figure;
subplot(3,4,1);
imshow(I);
title('原图');
I=double(I);
for i=1:10
x=200-i*20;
y=200+i*20;
subplot(3,4,i+1);
G=imcrop(I,[x,x,y,y]) imshow(uint8(G));
end
4. 将图像分别放大1.5倍和缩小0.8倍,插值方法使用最近邻域法和双线性插值法,对比显示图像。
im=imread('hehua1.bmp');
subplot(2,3,1);
imshow(im);
title('原图');
xscale=1.5;yscale=1.5;
[row,col,r]=size(im);
row=row*yscale; col=col*xscale;
im1=uint8(zeros(uint16(row),uint16(col),uint16(r)));
for i=1:row
for j=1:col
x=j/xscale; y=i/yscale;
im1(i,j,1)=im(uint16(y),uint16(x),1);
im1(i,j,2)=im(uint16(y),uint16(x),2);
im1(i,j,3)=im(uint16(y),uint16(x),3);
end
end
subplot(2,3,2);
imshow(im1);
title('最近邻域法1.5');
im2=uint8(zeros(uint16(row),uint16(col),uint16(r)));
for i=2:row-1
for j=2:col-1
x=j/xscale; y=i/yscale;
left=floor(x); top=floor(y);
right=left+1; bottom=top+1;
u=x-left; v=y-top;
im2(i,j,1)=uint8((1-u)*(1-v)*im(top,left,1)+(1-u)*v*im(top,right,1)+...
u*(1-v)*im(bottom,left,1)+u*v*im(bottom,right,1));
im2(i,j,2)=uint8((1-u)*(1-v)*im(top,left,2)+(1-u)*v*im(top,right,2)+...
u*(1-v)*im(bottom,left,2)+u*v*im(bottom,right,2));
im2(i,j,3)=uint8((1-u)*(1-v)*im(top,left,3)+(1-u)*v*im(top,right,3)+...
u*(1-v)*im(bottom,left,3)+u*v*im(bottom,right,3));
end
end
subplot(2,3,3);
imshow(im2);
title('双线性插值1.5');
im=imread('hehua1.bmp');
subplot(2,3,4);
imshow(im);
title('原图');
xscale=0.8;yscale=0.8;
[row,col,r]=size(im);
row=row*yscale; col=col*xscale;
im1=uint8(zeros(uint16(row),uint16(col),uint16(r)));
for i=1:row
for j=1:col
x=j/xscale; y=i/yscale;
im1(i,j,1)=im(uint16(y),uint16(x),1);
im1(i,j,2)=im(uint16(y),uint16(x),2);
im1(i,j,3)=im(uint16(y),uint16(x),3);
end
end
subplot(2,3,5);
imshow(im1);
title('最近邻域法0.8');
im2=uint8(zeros(uint16(row),uint16(col),uint16(r)));
for i=2:row-1
for j=2:col-1
x=j/xscale; y=i/yscale;
left=floor(x); top=floor(y);
right=left+1; bottom=top+1;
u=x-left; v=y-top;
im2(i,j,1)=uint8((1-u)*(1-v)*im(top,left,1)+(1-u)*v*im(top,right,1)+...
u*(1-v)*im(bottom,left,1)+u*v*im(bottom,right,1));
im2(i,j,2)=uint8((1-u)*(1-v)*im(top,left,2)+(1-u)*v*im(top,right,2)+...
u*(1-v)*im(bottom,left,2)+u*v*im(bottom,right,2));
im2(i,j,3)=uint8((1-u)*(1-v)*im(top,left,3)+(1-u)*v*im(top,right,3)+...
u*(1-v)*im(bottom,left,3)+u*v*im(bottom,right,3));
end
end
subplot(2,3,6);
imshow(im2);
title('双线性插值0.8');
5. 将图像水平镜像,再顺时针旋转45度,显示旋转后的图像。
I=imread('hehua1.bmp');
figure;
subplot(1,2,1);
imshow(I);
title('原图');
I=double(I);
[m,n,l]=size(I);
I1(1:m,1:n,1:l)=I(1:m,n:-1:1,1:l);
I2=imrotate(I1,315,'nearest');
I3=uint8(I2);
subplot(1,2,2);
imshow(I3);
title('水平镜像顺时针旋转45度');
6.将图像分别进行水平方向30度错切,垂直方向45度错切,分别显示结果I=imread('hehua.bmp');
[m,n,l]=size(I);
figure;
subplot(1,3,1);
imshow(I);
title('原图');
I=double(I);
G=zeros(m,n,l)
for i=1:m
for j=1:n
for k=1:l
G(i+round(sqrt(3)/3*j),j,k)=I(i,j,k);
end
end
end
subplot(1,3,2);
imshow(uint8(G));
title('水平30度');
G1=zeros(m,n,l)
for i=1:m
for j=1:n
for k=1:l
G1(i,j+i,k)=I(i,j,k);
end
end
end
subplot(1,3,3);
imshow(uint8(G1));
title('垂直45度');
(注:文档可能无法思考全面,请浏览后下载,供参考。可复制、编制,期待你的好评与关注)