370 likes | 530 Views
第三章 图像运算. 图像的点运算 图像的代数运算 图像的几何运算 图像的邻域操作. §3.1 图像的点运算. 3. 线性点运算. 线性点运算:灰度变换 函数 f 为线性函数。. 当 a>1 对比度增大 a<1 对比度减小 a=1, b=0 简单复制 a=1,b~=0 明亮或灰暗 平均灰度. 方差. 0≦i<W 0≦j<H. 4. 非线性点运算. 直方图均衡化. § 3.2 图像的代数运算. § 3.3 图像的几何运算. 2. 灰度级插值. 0≤x < W, 0≤y < H. 3. 空间变换.
E N D
第三章 图像运算 • 图像的点运算 • 图像的代数运算 • 图像的几何运算 • 图像的邻域操作
3. 线性点运算 线性点运算:灰度变换 函数f为线性函数。 当a>1 对比度增大 a<1 对比度减小 a=1, b=0 简单复制 a=1,b~=0 明亮或灰暗 平均灰度 方差 0≦i<W 0≦j<H
2. 灰度级插值 0≤x<W, 0≤y<H
3. 空间变换 空间变换:将输入图像的像素位置映射到输出图像的新位 置。 常用的仿射变换为: 尺度变换 伸缩变换 扭曲变换 旋转变换
前述五种变换的级联矩阵为: 透视变换: aij 为指定的变换系数,且:
MATLAB空间变换方法: 首先创建一个结构体 TFORM,然后调用imtransform函数 b=imtransform(A, TFORM,INTERP) A 为需要变换的图像 TFORM 为结构体 INTERP 为使用的插值方式
Maketform函数支持的空间变换类型: Affine 仿射变换(平移、旋转、尺度、拉伸、剪切) Projiective 透视变换 Box 对图像的每一维单独进行仿射变换 Custom 用户定义的变换 Composite 两种或多种变换结合
操作步骤: (1)选择 (2)判断 (3)函数求值 (4)寻找并设置(5)重复1~4步
2. 分离邻域操作 分离邻域:将矩阵划分为m×n后得到的矩阵部分。 见右上图的9个4×8邻域的11×24矩阵。 见右下图,在15×30 矩阵中有1×2重叠部分的邻域,图中用阴影表示重叠部分。
3. 列处理 列处理:在图像处理之前,将图像数据矩阵转换为矩阵列。 colfilt函数实现列操作的执行过程:先将图像的每一个滑动或分离邻域重新排列到一个临时矩阵的某一列中,然后将临时矩阵传递给指定的计算函数,计算得到的结果经过重新排列变为原始图像的形状。
对于滑动邻域操作:原始图像中的每一个像素都对应于colfilt函数所创建的临时矩阵的一个单独列,该列包含该像素邻域内的所有数值。对于滑动邻域操作:原始图像中的每一个像素都对应于colfilt函数所创建的临时矩阵的一个单独列,该列包含该像素邻域内的所有数值。
对于分离邻域操作:colfilt函数通过将输入图像的每一个邻域进行重新排列来创建一个临时矩阵,在此之前如果必要会对原始图像进行零填充。对于分离邻域操作:colfilt函数通过将输入图像的每一个邻域进行重新排列来创建一个临时矩阵,在此之前如果必要会对原始图像进行零填充。
MATLAB程序举例 %%ch3 %%f3.6(直方图均衡化) i=imread('pout.tif'); subplot(1,2,1),imshow(i); subplot(1,2,2),imhist(i); i2=histeq(i); figure, subplot(1,2,1),imshow(i2); subplot(1,2,2),imhist(i2); %%f3.2(线性点运算) rice=imread('rice.tif'); I=double(rice);
J=I*0.43+60; rice2=uint8(J); subplot(1,2,1),imshow(rice); subplot(1,2,2),imshow(rice2); %%f3.7_8(加法运算:两图像相加) i=imread('rice.tif'); j=imread('cameraman.tif'); k=imadd(i,j); subplot(1,3,1),imshow(i); subplot(1,3,2),imshow(j); subplot(1,3,3),imshow(k); %%imshow(k);
%%f3.9(加法运算:增加亮度值) RGB=imread('flowers.tif'); RGB2=imadd(RGB,50); RGB3=imadd(RGB,100); subplot(1,3,1),imshow(RGB); subplot(1,3,2),imshow(RGB2); subplot(1,3,3),imshow(RGB3); %%f3.10(减法运算—减背景) rice=imread('rice.tif'); background=imopen(rice,strel('disk',15)); rice2=imsubtract(rice,background);
subplot(1,2,1),imshow(rice); subplot(1,2,2),imshow(rice2); %%f3.10(减法运算—图像相减) rice=imread('rice.tif'); pout=imread('pout.tif'); c=imsubtract(rice,pout); subplot(1,2,2),imshow(c); %不能这样相减 %提示使用 Z = imlincomb(1.0, X, -1.0, Y) %%f3.11(乘法运算:j,k取值不同)
i=imread('moon.tif'); j=immultiply(i,1.2); k=immultiply(i,2); subplot(1,3,1),imshow(i); subplot(1,3,2),imshow(j); subplot(1,3,3),imshow(k); %%f3.12(除法运算:j,k,l取值不同) rice=imread('rice.tif'); i=double(rice); j=i*0.43+90; k=i*0.1+90; l=i*0.01+90;
rice2=uint8(j); rice3=uint8(k); rice4=uint8(l); ip=imdivide(rice,rice2); ik=imdivide(rice,rice3); il=imdivide(rice,rice4); imshow(ip,[]); figure,imshow(ik,[]); figure,imshow(il,[]); %%subplot(1,2,1),imshow(rice); %%subplot(1,2,2),imshow(rice2); %%f3.13(四则运算1: imadd_imdivide混合用法) i=imread('rice.tif');
i2=imread('cameraman.tif'); l=imadd(i,i2); k=imdivide(imadd(i,i2),6); imshow(l); figure,imshow(k,[]); %%f3.13(四则运算2: imlincomb用法) X=imread('rice.tif'); Y=imread('rice.tif'); A=0.5; B=2.1; C=2.3; z1=imlincomb(A,X,C); z2=imlincomb(A,X,B,Y);
figure,imshow(z1,[]); figure,imshow(z2,[]); %%f3.15(非缺省空间坐标系统下图像效果) A=magic(5); x=[19.5 23.5]; y=[8.0 12.0]; image(A,'XData',x,'YData',y), axis image,colormap(jet(25)); %% A=magic(50); x=[19.5 23.5]; y=[8.0 12.0];
image(A,'XData',x,'YData',y), axis image,colormap(jet(255)); %% A=magic(10); x=[19.5 23.5]; y=[8.0 12.0]; image(A,'XData',x,'YData',y), axis image,colormap(jet(100)) %%f3.18(投影变换) I=imread('cameraman.tif'); udata=[0 1]; vdata=[0 1]; tform=maketform('projective',[0 0;1 0;1 1;0 1],...
[-4 2;-8 -3;-3 -5;6 3]); [B,xdata,ydata]=imtransform(I,tform,'bicubic','udata',udata,... 'vdata',vdata,'size',size(I),'fill',128); subplot(1,2,1),imshow(udata,vdata,I),axis on; subplot(1,2,2),imshow(xdata,ydata,B),axis on; %%f3.19(图像缩放1) i=imread('ic.tif'); j=imresize(i,1.25); imshow(i); figure,imshow(j); %%f3.19(图像缩放2) load woman; X1=imresize(X,2,'bilinear'); X2=imresize(X,2,'bicubic');
imshow(X,map); figure,imshow(X1,map); figure,imshow(X2,map); %%f3.20(图像旋转与裁剪) i=imread('ic.tif'); j=imrotate(i,60,'bilinear'); j1=imrotate(i,60,'bilinear','crop'); subplot(1,3,1),imshow(i); subplot(1,3,2),imshow(j); subplot(1,3,3),imshow(j1); %%f3.21(图像剪切) i=imread('ic.tif'); imshow(i); j=imcrop;
k=imresize(j,3); figure,imshow(j); figure,imshow(k); %%f3.23(滑动邻域操作) i=imread('tire.tif'); f=inline('max(x(:))'); i2=nlfilter(i,[3 3],f); subplot(1,2,1),imshow(i); subplot(1,2,2),imshow(i2); %%f3.25(分离邻域操作1) i=imread('tire.tif'); f=inline('uint8(round(mean2(x)*ones(size(x))))'); i2=blkproc(i,[8 8],f); i3=blkproc(i,[1 8],f);
subplot(1,3,1),imshow(i); subplot(1,3,2),imshow(i2); subplot(1,3,3),imshow(i3); %%f3.25(分离邻域操作2) A=imread('alumgrns.tif'); B=blkproc(A,[5 5],'std2(x)*ones(size(x))'); imshow(A); figure,imshow(B); %%f3.26(重叠否与减法) i=imread('tire.tif'); f=inline('uint8(round(mean2(x)*ones(size(x))))'); i1=blkproc(i,[8 8],f); i2=blkproc(i,[8 8],[1 2],f); i3=imsubtract(i2,i1);
imshow(i1); figure,imshow(i2); figure,imshow(i3); %%习题%% %%补充 A1=[1,2,3;4,5,6;7,8,9]; A1 A2=[2,3,1;4,6,5;9,8,7]; A2 A=cat(3,A1,A2); A B=cat(2,A1,A2); B
C=cat(1,A1,A2); C %% size(A1) %%习题解答 %%题1 I=imread('rice.tif'); X=ones(size(I)); I1=im2double(I); J1=I1.*I1-X/256; J2=I1*0.6+X/64; subplot(1,2,1),imshow(J1); subplot(1,2,2),imshow(J2);
%%题2 I=imread('rice.tif'); [m,n]=size(I); I1=zeros(m,n); %I1(1:m-100,1:n-60)=I(1:m-100,1:n-60); I1(1:m,1:n-30)=I(1:m,1:n-30); %I1(1:m-100,1:n)=I(1:m-100,1:n); I1=uint8(I1); sub=imsubtract(I1,I); div=imdivide(I1,I); subplot(1,2,1),imshow(sub); subplot(1,2,2),imshow(div,[]); %%题3解_1 I=imread('tire.tif');
f=inline('uint8(round(mean2(x)*ones(size(x))))'); %f=uint8(colfilt(I,[3 3],'distinct','mean'); I2=colfilt(I,[3 3],'distinct',@f); %I2=colfilt(I,[3 3],'distinct',f); imshow(I2); %%题3解_2 I=imread('rice.tif'); I=imread('tire.tif'); f=uint8(colfilt(I,[5 5],'sliding','mean')); f2=uint8(colfilt(I,[10 10],'sliding','mean'));
subplot(1,2,1),imshow(f); subplot(1,2,2),imshow(f2); %%题4解 i=imread('c1513.tif'); i1=imcrop(i); imshow(i1); %%题4解+1 i=imread('c1513.tif'); background=imopen(i,strel('disk',15)); i2=imsubtract(i,background); subplot(1,2,1),imshow(i); subplot(1,2,2),imshow(i2);