图像的几何变换又叫空间变换,是将原图像的坐标位置映射到另一幅图像中新坐标的位置。学习几何变换就是学习确定这种空间映射的关系,以及映射过程中的变换参数。几何变换不会改变图像的像素值,只是在图像平面上对像素重新安排位置。
图像平移
将图像中所有的点按照指定的平移量水平或者垂直移动。
例
a=imread('pout.tif');
%strel用来创建形态学结构元素
%translate(se,[y x])在图像se上进行y和x方向上的平移
se=translate(strel(1),[80 50]);
b=imdilate(a,se);%imdilate形态学膨胀
figure;
subplot(1,2,1);
imshow(a);
subplot(1,2,2);
imshow(b);
由结果可得,原图像部分变为黑色,移出图像边界的内容被丢弃。
图像镜像
原理分析
镜像分为两类
- 水平镜像,将图像左半部分与右半部分以图像垂直中线为中心轴,进行对换。
- 竖直镜像,将图像上半部分与下半部分以图像水平中线为中心轴,进行对换。
编程实现
imtransform函数用于完成一般的二维空间变换。语法形式b=imtransform(a,tform,method);
- a为要变换的图像
- 空间变换结构tform指定了具体的变换类型
- 可选参数method允许为imtransform函数选择的插值方法
使用maketform函数可以获得tform结构。语法形式t=maketform(transformtype,matrix)
- transformtype指定了变换的类型,常见的’affine’为二维或多维仿射变换,包括平移、旋转、比例、拉伸、和错切等。
- matrix为相应的仿射变换矩阵。
a=imread('pout.tif');
[height,width,dim]=size(a);
tform=maketform('affine',[-1 0 0 ;0 1 0;width 0 1;]);
b=imtransform(a,tform,'nearest');
tform2=maketform('affine',[1 0 0;0 -1 0;0 height 1;]);
c=imtransform(a,tform2,'nearest');
subplot(1,3,1);imshow(a);
subplot(1,3,2);imshow(b);
subplot(1,3,3);imshow(c);
图像转置
原理分析
将图像像素的x坐标和y坐标互换。
编程实现
a=imread('pout.tif');
tform=maketform('affine',[0 1 0;1 0 0;0 0 1]);
b=imtransform(a,tform,'nearest');
figure;
subplot(1,2,1);imshow(a);
subplot(1,2,2);imshow(b);
图像缩放
原理分析
图像的大小按照指定比例放大或缩小
代码实现
仍然可以用imtransform函数来实现。此外还可以用imresize函数,语法形式b=imresize(a,scale,method)
scale是缩放比例。
a=imread('pout.tif');
b=imresize(a,3.5,'nearest');
figure;
% subplot(1,2,1);
imshow(a);
% subplot(1,2,2);
figure;
imshow(b);
图像旋转
原理分析
图像围绕某一点旋转一定角度。
代码实现
imrotate函数,语法形式b=imrotate(a,angle,method,'crop');
- angle为要旋转的角度,单位为度,正值为逆时针旋转。
- method为插值方法
- 'crop’会裁剪旋转后增大的图像,使得到的图像与原图大小一致。
a=imread("pout.tif");
b=imrotate(a,30,'nearest','crop');
figure;
subplot(1,2,1);imshow(a);
subplot(1,2,2);imshow(b);