(2)、相减 %K为上题图
I=imread('A.jpg'); J= imsubtract(K,I);
subplot(1,3,1),imshow(I);title(‘原图1’); subplot(1,3,2),imshow(K); title(‘相加图’); subplot(1,3,3),imshow(J2); title(‘相减后图’)
3、几何运算 (1)、对任意一幅图像调用imresize函数,分别实现对原图的2倍比例放大,0.8倍比例缩小,非比例放大和缩小(人为定义调整后的图像长宽尺寸),并分别显示结果,对比分析不同的插值方法效果。
参考程序如下:
A=imread(‘lena.jpg’);%假设原图为256*256 B=imresize(A,2);%2倍放大 C=imresize(A,0.8);%0.8倍缩小
D=imresize(A,[300 300]);%非比例放大到300*300 E=imresize(A,[200 200]);%非比例缩小到200*200
%在以上几条语句的基础上,也可在Imresize后面添上一个参数method,指明插值方式,如’nearest’,’bilinear’,’bicubic’ subplot(2,2,1),imshow(B),title(‘放大2倍’) subplot(2,2,2),imshow(C),title(‘0.8倍缩小’) subplot(2,2,3),imshow(D),title(‘非比例放大’) subplot(2,2,4),imshow(E),title(‘非比例缩小’) (2)、任选一副图像,使用imrotate函数,分别顺时针60度,逆时针90度旋转,选择裁剪性旋转或非裁剪性旋转,在同一个窗口中显示处理后的图像。 参考程序如下:
I=imread(‘hua.jpg’);
J11=imrote(I,-60,’bilinear’);%顺时针旋转60度,非裁剪型旋转 J12=imrote(I,-60,’bilinear’,‘crop’);%顺时针旋转60度,裁剪型旋转 J21=imrote(I,90,’nearest’);%逆时针旋转90度,非裁剪型旋转 J22=imrote(I,90,’nearest’,‘crop’);%逆时针旋转90度,裁剪型旋转
subplot(2,2,1),imshow(J11),title(‘放大2倍’) subplot(2,2,2),imshow(J12),title(‘0.8倍缩小’) subplot(2,2,3),imshow(J21),title(‘非比例放大’) subplot(2,2,4),imshow(J22),title(‘非比例缩小’) 显示结果如下:
(3)、任选一幅图像,调用imcrop对该图片进行剪裁,显示处理后的图像。 I=imread(‘hua.jpg’); J=imcrop;
subplot(2,1,1),imshow(I);title(‘原图’); subplot(2,1,2),imshow(J);title(‘剪裁后图’); (4)、任选一幅图像,编程实现该图像的平移和镜像(选作)。
?x'??10x0??x?
?'???x'?x?x0???y? y?01y即:???0???? ?1??001??1??y'?y?y0??????
参考程序如下:
function [I]=hmove(i,x0,y0);
%编写实现图像平移的函数hmove,平移量为x0,y0,平移前图像矩阵为i, [r,c]=size(i);
I(r+x0,c+y0)=0; %平移后图像矩阵为I for x=1:r;
for y=1:c; x1=x+x0; y1=y+y0;
I(x1,y1)=i(x,y); end; end; 主程序:
RGB=imread(‘hua.jpg’); subplot(2,2,1)
imshow(RGB),title(‘原图’) subplot(2,2,2)
gray1=rgb2gray(RGB);title(‘灰度图’) imagesc(gray1),colormap(gray); subplot(2,2,3)
I1=hmove(gray1,100,20);title(‘平移图’) subimage(gray1),axis('image');
subplot(2,2,4),imagesc(I1),colormap(gray),axis([1,700],[1,820]); 镜像
沿纵轴翻转,水平镜像:a(x,y) = -x; b(x,y) = y;
沿横轴翻转,垂直镜像:a(x,y) = x; b(x,y) = -y; 对于实际的图像来说,即将图像的左右或上下翻转, 程序如下:
I0=imread(‘hua.jpg’); I=rgb2gray(I0); [r,c]=size(I); I1=zeros(r,c); %水平镜像
I1(1:r,1:c)=I(1:r,c:1); %垂直镜像
I1(1:r,1:c)=I(r:1,1:c);
(5)、了解用imtransform和maketform函数实现各种空间变换的方法。 演示一下程序:
仿射变换,可以用以下函数来描述:
,其中,A是变形矩阵,b是平移矩阵。
尺度变换:
CLF;I=checkerboard(20,2);
subplot(121);imshow(I);axis on;title('原图') s=1.5;T=[s 0;0 s;0 0]; tf=maketform('affine',T);
I1=imtransform(I,tf,'bicubic','FillValues',0.3); subplot(122);imshow(I1);axis on;title('尺度变换') 伸缩变换 【例】
CLF;I=checkerboard(20,2);
subplot(121);imshow(I);axis on;title('原图') t=2;T=[1 0;0 t;0 0];
tf=maketform('affine',T);
I1=imtransform(I,tf,'bicubic','FillValues',0.3); subplot(122);imshow(I1);axis on;title('伸缩变换') 旋转变换:
CLF;I = checkerboard(20,2);
subplot(1,2,1);imshow(I);title('原图') angle=20*pi/180;
sc=cos(angle);ss=sin(angle); T=[sc -ss; ss sc;0 0]; tf=maketform('affine',T);
I1=imtransform(I,tf,'bicubic','FillValues',0.3); subplot(122);imshow(I1);title('旋转变换') 综合变换 【例】
CLF;I = checkerboard(20,2);
subplot(1,2,1);imshow(I);title('原图') s=2;As=[s 0;0 s]; % 尺度 t=2;At=[1 0;0 t]; % 伸缩 u=1.5;Au=[1 u;0 1]; % 扭曲
st=30*pi/180;sc=cos(angle);ss=sin(angle); Ast=[sc -ss; ss sc]; % 旋转 T=[As*At*Au*Ast;3 5]; tf=maketform('affine',T);
I1=imtransform(I,tf,'bicubic','FillValues',0.3); subplot(122);imshow(I1);title('综合')
四、图像邻域操作、区域操作、图像统计及MATLAB实现 1、运行以下程序,观察显示结果: (1)、%用函数mean作滑动处理 CLF
I=imread('tire.tif');
相关推荐: