实验四、图像的基本运算1(1)选择一幅图像lena8.jpg,设置输入/输出变换的灰度级范围,a=0.3,b=0.6,c=0.1,d=0.9(2)设置非线性扩展函数的参数c=2(3)采用灰度倒置变换函数s=255-r进行图像变换(4)设置二值化图像的阈值,分别为level=0.4,level=0.7程序如下I=imread('C:\lena8.jpg');figure;subplot(2,3,1);imshow(I);title('原图');J=imadjust(I,[0.3;0.6],[0.1;0.9]);%设置灰度变换的范围subplot(2,3,2);imshow(J);title('线性扩展');I1=double(I);%将图像转换为double类型I2=I1/255;%归一化此图像C=2;K=C*log(1+I2);%求图像的对数变换subplot(2,3,3);imshow(K);title('非线性扩展');M=im2bw(I,0.5);M=~M;%M=255-I;%将此图像取反%Figuresubplot(2,3,4);imshow(M);title('灰度倒置');N1=im2bw(I,0.4);%将此图像二值化,阈值为0.4N2=im2bw(I,0.7);%将此图像二值化,阈值为0.7subplot(2,3,5);imshow(N1);title('二值化阈值0.4');subplot(2,3,6);imshow(N2);title('二值化阈值0.7');2选取两幅大小一样的灰度图像hough.bmp和rice.bmp,将两幅图像进行加法运算。程序如下I=imread('C:\hough.bmp');%I=rgb2gray(I);J=imread('C:\rice.bmp');I=im2double(I);%将图像转换成double型J=im2double(J);K=I+0.3*J;%两幅图像相加subplot(1,3,1);imshow(I);title('物图');subplot(1,3,2);imshow(J);title('背景图');subplot(1,3,3);imshow(K);title('相加后的图');imwrite(K,'C:\lena1.jpg');3选取一幅混合图像,如相加得到的图像lenal.jpg,将混合图像与背景图像做减法运算。程序如下A=imread('C:\lena1.jpg');B=imread('C:\rice.bmp');A=im2double(A);%将图像转换成double型B=im2double(B);C=A-0.3*B;subplot(1,3,1);imshow(A);title('混合图');subplot(1,3,2);imshow(B);title('背景图');subplot(1,3,3);imshow(C);title('分离后的图');4选取一幅尺寸为256x256像素的灰度图,如rice.bmp。设置掩模模板,对于需要保留下来的区域,掩模图像的值置为1,而需要被抑制掉的区域,掩模图像的值置为0。程序如下A=imread('C:\rice.bmp');%A=rgb2gray(A);A=im2double(A);subplot(1,2,1);imshow(A);title('原图');B=zeros(256,256);B(40:200,40:200)=1;K=A.*B;subplot(1,2,2);imshow(K);title('局部图');5选取一幅大小为256x256像素的图像,如lena8.jpg.分别将图比例放大1.5倍,比例缩小0.7倍,非比例放大到420x384像素,非比例缩小到15x180像素。程序如下A=imread('C:\lena8.jpg');Bl=imresize(A,1.5);%比例放大1.5倍,默认采用的是最近邻法进行线性插值B2=imresize(A,[420384]);%非比例放大至420:384Cl=imresize(A,0.7);%比例缩小0.7倍C1=imresize(A,0.7);C2=imresize(A,[150180]);%非比例缩小到150:180figure;imshow(Bl);title('比例放大图');figure;imshow(B2);title('非比例放大图');figure;imshow(C1);title('比例缩小图');figure;imshow(C2);title('非比例缩小图');实验五、图像的变换1选取一幅图像,进行离散傅里叶变换,再对其分别进行X轴与Y轴上的平移,得其离散傅里叶变换,观察结果图。程序如下I=imread('C:\1.bmp');%I=imread('C:\lena8.jpg');%imshow(I);imshow(real(I));figure(1)I=I(:,:,3);fftI=fft2(I);sfftI=fftshift(fftI);%求离散傅里叶频谱%对原始图像进行二维离散傅里叶变换,并将其坐标原点移到频谱图中央位置RRfdpl=real(sfftI);IIfdpl=imag(sfftI);a=sqrt(RRfdpl.^2+IIfdpl.^2);a=(a-min(min(a)))/(max(max(a))-min(min(a)))*225;figure(2);imshow(real(a));I=imread('C:\2.bmp');figure(3);imshow(real(I));I=I(:,:,3);fftI=fft2(I);sfftI=fftshift(fftI);%求离散傅里叶频谱%对原始图像进行二维离散傅里叶变换,并将其坐标原点移到频谱图中央位置RRfdpl=real(sfftI);IIfdpl=imag(sfftI);a=sqrt(RRfdpl.^2+IIfdpl.^2);a=(a-min(min(a)))/(max(max(a))-min(min(a)))*225;figure(4);imshow(real(a));I=imread('C:\3.bmp');figure(5);imshow(real(I));I=I(:,:,3);fftI=fft2(I);sfftI=fftshift(fftI);%求离散傅里叶频谱%对原始图像进行二维离散傅里叶变换,并将其坐标原点移到频谱图中央位置RRfdpl=real(sfftI);IIfdpl=imag(sfftI);a=sqrt(RRfdpl.^2+IIfdpl.^2);a=(a-min(min(a)))/(max(max(a))-min(min(a)))*225;figure(6);imshow(real(a));2选取一幅图像,进行离散傅里叶变换,再对其进行一定角度的旋转,进行离散傅里叶变化。程序如下%构造原始图像I=zeros(256,256);I(88:168,124:132)=1;%图像范围是256*256,前一值是纵向比,后一值是横向比imshow(I);%求原始图像的傅里叶变换J=fft2(I);F=abs(J);J1=fftshift(F);figureimshow(J1,[550]);%对原始图像进行旋转J=imrotate(I,90,'bilinear','crop');figureimshow(J)%求旋转后图像的傅里叶频谱J1=fft2(J);F=abs(J1);J2=fftshift(F);figureImshow(J2,[550])3选取一幅图像,进行离散余弦变换,并对其进行离散余弦反变换,观察其结果程序如下%对lena24.ipg文件计算二维DCT变换RGB=imread('C:\lena24.jpg');figure(1)imshow(RGB)I=rgb2gray(RGB);%真彩色图像转换成灰度图像J=dct2(I);%计算二维DCT变换figure(2)imshow(log(abs(J)),[])%图像大部分能量集中在上左角处figure(3);J(abs(J)10)=0;%把变换矩阵中小于10的值置换为0,然后用idct2重构图像K=idct2(J)/255;imshow(K)4选取一幅图像,进行离散余弦变换,并对其进行压缩解压,观察其结果。程序如下RGB=imread('C:\lena24.jpg');I=rgb2gray(RGB);I=im2double(I);%转换图像矩阵为双精度型T=dctmtx(8);%产生二维00?变换矩阵,%矩阵T及其转置T‘是DCT函数P1*X*P2的参数B=blkproc(I,[88],'P1*x*P2',T,T');maskl=[1111000011100000110000001000000000000000000000000000000000000000];%二值掩模,用来压缩DCT系数B2=blkproc(B,[88],'P1.*x',maskl);%只保留DCT变换的10个系数I2=blkproc(B2,[88],'P1*x*P2',T',T);%重构图像figure,imshow(I);figure,imshow(B2);figure,imshow(I2);实验六、图像的增强1对一幅灰度图像进行幂次变换。程序如下clearallcloseallI{1}=double(imread('C:\lena8.jpg'));I{1}=I{1}/255;figure(1),subplot(2,4,1),imshow(I{1},[]),holdonI{2}=double(imread('C:\lena1.jpg'));I{2}=I{2}/255;subplot(2,4,5),imshow(I{2},[]),holdonform=1:2Index=0;forlemta=[0.55]Index=Index+1;F{m}{Index}=I{m}.^lemta;subplot(2,4,(m-1)*4+Index+1),imshow(F{m}{Index},[])endend2对一幅灰度图像采用两种目标直方图来规定化。程序如下clearallcloseall%0.读图像I=double(imread('C:\lena8.jpg'));figure,imshow(I,[])N=32;Hist_image=hist(I(:),N);%直方图Hist_image=Hist_image/sum(Hist_image);Hist_image_cumulation=cumsum(Hist_image);%累计直方图figure,stem([0:N-1],Hist_image);%1.设计目标直方图Index=0:N-1;Index=0:7;%正态分布直方图Hist{1}=exp(-(Index-4).^2/8);Hist{1}=Hist{1}/sum(Hist{1});Hist_cumulation{1}=cumsum(Hist{1});figure,stem([0:7],Hist{1})%倒三角形状直方图Hist{2}=abs(15-2*Index);Hist{2}=Hist{2}/sum(Hist{2});Hist_cumulation{2}=cumsum(Hist{2});figure,stem([0:7],Hist{2})%2.规定化处理form=1:2Image=I;%2.1SML处理fork=1:NTemp=abs(Hist_image_cumulation(k)-Hist_cumulation{m});[Tempi,Project{m}(k)]=min(Temp);end%2.2变换后直方图fork=1:NTemp=find(Project{m}==k);ifisempty(Temp)Hist_result{m}(k)=0;elseHistresult{m}(k)=sum(Hist_image(Temp))