8第2章图像获取2.3.2二维连续傅里叶变换例2.2figure(1);%建立图形窗口1[u,v]=meshgrid(-1:0.01:1);%生成二维频域网格F1=abs(sinc(u.*pi));F2=abs(sinc(v.*pi));F=F1.*F2;%计算幅度频谱F=|F(u,v)|surf(u,v,F);%显示幅度频谱,如图2.3(b)shadinginterp;%平滑三维曲面上的小格axisoff;%关闭坐标系figure(2);%建立图形窗口2F1=histeq(F);%扩展F的对比度以增强视觉效果imshow(F1);%用图像来显示幅度频谱,如图2.3(c)第3章图像变换3.4.4二维FFT的MATLAB实现例3.2简单图像及其傅里叶变换MATLAB程序:%建立简单图像d并显示之d=zeros(32,32);%图像大小3232d(13:20,13:20)=1;%中心白色方块大小为88figure(1);%建立图形窗口1imshow(d,'notruesize');%显示图像d如图3.5(a)所示%计算傅里叶变换并显示之D=fft2(d);%计算图像d的傅里叶变换,fft2(d)=fft(fft(d).').'figure(2);%建立图形窗口2imshow(abs(D),[-15],'notruesize');%显示图像d的傅里叶变换谱如3.5(b)所示例3.3MATLAB图像及其傅里叶变换谱MATLAB程序:figure(1);loadimdemossaturn2;%装入MATLAB图像saturn2imshow(saturn2);%显示图像saturn2如图3.6(a)所示figure(2);S=fftshift(fft2(saturn2));%计算傅里叶变换并移位imshow(log(abs(S)),[]);%显示傅里叶变换谱如3.6(b)所示例3.4真彩图像及其傅里叶变换谱MATLAB程序:figure(1);A=imread('image1.jpg');%装入真彩图像,见图1.1(b)B=rgb2gray(A);%将真彩图像转换为灰度图像9imshow(B);%显示灰度图像如图3.7(a)所示C=fftshift(fft2(B));%计算傅里叶变换并移位figure(2);imshow(log(abs(C)),[]);%显示傅里叶变换谱如3.7(b)所示3.5.4离散余弦变换的MATLAB实现例3.5计算并显示真彩图像余弦变换的MATLAB程序如下:RGB=imread('image2.jpg');%装入真彩图像figure(1);imshow(RGB);%显示彩色图像GRAY=rgb2gray(RGB);%将真彩图像转换为灰度图像figure(2);imshow(GRAY);%显示灰度图像如图3.10(a)所示DCT=dct2(GRAY);%进行余弦变换figure(3);imshow(log(abs(DCT)),[]);%显示余弦变换如图3.10(b)所示。3.8.2Radon变换的MATLAB实现例3.8真彩图像的Radon变换MATLAB程序如下:RGB=imread('image2.jpg');%装入真彩图像GRAY=rgb2gray(RGB);%将真彩图像转换为灰度图像figure(2);imshow(GRAY);%显示灰度图像如图3.16(a)[R,xp]=radon(GRAY,[045]);%计算变换角度为0°和45°的Radon变换figure;plot(xp,R(:,1));title('R_{0^o}(x\prime)')%显示0°方向上的Radon变换如图3.16(b)figure;plot(xp,R(:,2));title('R_{45^o}(x\prime)')%显示45°方向上的Radon变换如图3.16(c)例3.9连续角度的Radon变换对于一组连续角度的Radon变换通常用一幅图像来表示。本例先建立一幅简单图像,然后令变换角度从0°以1°的增量变化到180°时的Radon变换情况。其MATLAB程序如下:I=zeros(100,100);%建立简单图像如图3.17(a)I(25:75,25:75)=1;figure(1);imshow(I);theta=0:180;%规定变换角度的范围[R,xp]=radon(I,theta);%计算Radon变换figure(2);imagesc(theta,xp,R);%以图像方式显示变换结果R,%其x轴和y轴分别为theta和xptitle(‘R_{\theta}(X\prime)’);%显示图像标题)'(xR10xlabel(‘\theta(degrees)’);%显示x坐标“)egrees(d”ylabel(‘X\prime’);%显示y坐标“'x”set(gca,’Xtick’,0:20:180);%设置x坐标刻度colormap(hot);%设置调色板colorbar;%显示当前图像的调色板第4章图像增强4.2.1直接灰度变换Matlab程序实现图像求反:I=imread('cameraman.tif');imshow(I)I=double(I)I=256-1-II=uint8(I)figureimshow(I)例4.1用Matlab程序实现线性灰度变换的图像增强:%读入并显示原始图像I=imread('pout.tif');imshow(I);I=double(I);[M,N]=size(I);%进行线性灰度变换fori=1:Mforj=1:NifI(i,j)=30I(i,j)=I(i,j);elseifI(i,j)=150I(i,j)=(200-30)/(150-30)*(I(i,j)-30)+30;elseI(i,j)=(255-200)/(255-150)*(I(i,j)-150)+200;endendend例4.2I=imread('lena.bmp');figure;imshow(I);I=double(I);I2=41*log(1+I);I2=uint8(I2);figure;imshow(I2);例4.3灰度切割变换的Matlab的程序如下:I=imread('007.bmp');figure;imshow(I);I=double(I)[M,N]=size(I);11fori=1:Mforj=1:NifI(i,j)=50I(i,j)=40;elseifI(i,j)=180I(i,j)=220;elseI(i,j)=40;endendendI=uint8(I);figure;imshow(I);例4.4具体Matlab程序如下:I=imread('lena.bmp');imshow(I);I=double(I);[M,N]=size(I);fork=1:8J=zeros(M,N);fori=1:Mforj=1:Ntemp=I(i,j);s1=0;s2=0;range=[k:-1:1];ford=ranges1=2^(8-d)+s1;s2=2^(8-d+1);iftemp=s1&temps2;J(i,j)=255;break;endendendendJ=uint8(J);figure;imshow(J);end4.2.2直方图修正例4.6直方图均衡化效果实例用Matlab中的histeq函数实现直方图均衡化的程序如下:I=imread('circuit.tif');figuresubplot(221);imshow(I);subplot(222);imhist(I)I1=histeq(I);figure;subplot(221);imshow(I1)subplot(222);imhist(I1)例4.8:直方图规定效果实例12用matlab中的histeq函数实现直方图均衡化的程序如下:I=imread('circuit.tif');[M,N]=size(I);fori=1:8:257counts(i)=i;endQ=imread('circuit.tif');N=histeq(Q,counts);figuresubplot(221);imshow(N);subplot(222);imhist(N);axis([026005000]);4.2.3图像间运算例:用图像平均减少随机噪声I=imread('tire.tif');[M,N]=size(I);II1=zeros(M,N);fori=1:16II(:,:,i)=imnoise(I,'gaussian',0,0.01);II1=II1+double(II(:,:,i));ifor(or(i==1,i==4),or(i==8,i==16));figure;imshow(uint8(II1/i));endend4.3空域滤波增强Matlab实现的邻域平均法抑制噪声的程序:I=imread('eight.tif');J=imnoise(I,'salt&pepper',0.02);subplot(231),imshow(I);title('原图像');subplot(232),imshow(J);title('添加椒盐噪声图像')k1=filter2(fspecial('average',3),J);%进行3×3模板平滑滤波k2=filter2(fspecial('average',5),J);%进行5×5模板平滑滤波k3=filter2(fspecial('average',7),J);%进行7×7模板平滑滤波k4=filter2(fspecial('average',9),J);%进行9×9模板平滑滤波subplot(233),imshow(uint8(k1));title('3×3模板平滑滤波');subplot(234),imshow(uint8(k2));title('5×5模板平滑滤波');subplot(235),imshow(uint8(k3));title('7×7模板平滑滤波');subplot(236),imshow(uint8(k4));title('9×9模板平滑滤波')例4.10:使用中值滤波降低图像噪声I=imread('eight.tif');J=imnoise(I,'salt&pepper',0.02);subplot(231),imshow(I);title('原图像');subplot(232),imshow(J);title('添加椒盐噪声图像')k1=medfilt2(J);%进行3×3模板中值滤波k2=medfilt2(J,[55]);%进行5×5模板中值滤波13k3=medfilt2(J,[77]);%进行7×7模板中值滤波k4=medfilt2(J,[99]);%进行9×9模板中值滤波subplot(233),imshow(k1);title('3×3模板中值滤波')subplot(234),imshow(k2);title('5×5模板中值滤波')subplot(235),imshow(k3);title('7×7模板中值滤波')subplot(236),imshow(k4);title('9×9模板中值滤波')例4.11:梯度锐化实例I=imread('cameraman.tif');subplot(131),imshow(I)H=fspecial('Sobel');H=H';%Sobel垂直模板TH=filter2(H,I);subplot(132),imshow(TH,[]);H=H';%Sobel水平模板TH=filter2(H,I);subplot(133),imshow(TH,[])4.4图像频域增强例4.12:频域低通滤波所产生的模糊%理想低通过滤波器所产生的模糊和振铃现象J=imread('lena.bmp');subplot(331);imshow(J);J=double(J);%采用傅里叶变换f=fft2(J);%数据矩阵平衡g=fftshift(f);su