数字图像处理 阮秋琦 MATLAB源程序

合集下载

数字图像处理及matlab实现源代码【1】

数字图像处理及matlab实现源代码【1】

% *-*--*-*-*-*-*-*-*-*-*-*-*图像处理*-*-*-*-*-*-*-*-*-*-*-*%{% (一)图像文件的读/写A=imread('drum.jpg'); % 读入图像imshow(A); % 显示图像imwrite(A,'drum.jpg');info=imfinfo('drum.jpg') % 查询图像文件信息% 用colorbar函数将颜色条添加到坐标轴对象中RGB=imread('drum.jpg');I=rgb2gray(RGB); % 把RGB图像转换成灰度图像h=[1 2 1;0 0 0;-1 -2 -1];I2=filter2(h,I);imshow(I2,[]);colorbar('vert') % 将颜色条添加到坐标轴对象中% wrap函数将图像作为纹理进行映射A=imread('4.jpg');imshow(A);I=rgb2gray(RGB);[x,y,z]=sphere;warp(x,y,z,I); % 用warp函数将图像作为纹理进行映射%}% subimage函数实现一个图形窗口中显示多幅图像RGB=imread('drum.jpg');I=rgb2gray(RGB);subplot(1,2,1);subimage(RGB); % subimage函数实现一个图形窗口中显示多幅图像subplot(1,2,2),subimage(I);% *-*--*-*-*-*-*-*-*-*-*-*-*图像处理*-*-*-*-*-*-*-*-*-*-*-*% (二)图像处理的基本操作% ----------------图像代数运算------------------%{% imadd函数实现两幅图像的相加或给一幅图像加上一个常数% 给图像每个像素都增加亮度I=imread('4.jpg');J=imadd(I,100); % 给图像增加亮度subplot(1,2,1),imshow(I);title('原图');subplot(1,2,2),imshow(J);title('增加亮度图');%% imsubtract函数实现将一幅图像从另一个图像中减去或减去一个常数I=imread('drum.jpg');J=imsubtract(I,100); % 给图像减去亮度subplot(1,2,1),imshow(I);%% immultiply实现两幅图像的相乘或者一幅图像的亮度缩放I=imread('drum.jpg');J=immultiply(I,2); % 进行亮度缩放subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(J);%% imdivide函数实现两幅图像的除法或一幅图像的亮度缩放I=imread('4.jpg');J=imdivide(I,0.5); % 图像的亮度缩放subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(J);%}% ----------------图像的空间域操作------------------%{% imresize函数实现图像的缩放J=imread('4.jpg');subplot(1,2,1),imshow(J);title('原图');X1=imresize(J,0.2); % 对图像进行缩放subplot(1,2,2),imshow(X1);title('缩放图');%% imrotate函数实现图像的旋转I=imread('drum.jpg');J=imrotate(I,50,'bilinear'); % 对图像进行旋转subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(J);%% imcrop函数实现图像的剪切I=imread('drum.jpg');I2=imcrop(I,[1 100 130 112]); % 对图像进行剪切subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(I2);%}% ----------------特定区域处理------------------%{% roipoly函数用于选择图像中的多边形区域I=imread('4.jpg');c=[200 250 278 248 199 172];r=[21 21 75 121 121 75];BW=roipoly(I,c,r); % roipoly函数选择图像中的多边形区域subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(BW);%% roicolor函数式对RGB图像和灰度图像实现按灰度或亮度值选择区域进行处理a=imread('4.jpg');subplot(2,2,1),imshow(a);I=rgb2gray(a);BW=roicolor(I,128,225); % 按灰度值选择的区域subplot(2,2,4),imshow(BW);%% ploy2mask 函数转化指定的多边形区域为二值掩模x=[63 186 54 190 63];y=[60 60 209 204 601];bw=poly2mask(x,y,256,256); % 转化指定的多边形区域为二值掩模imshow(bw);hold onplot(x,y,'r','LineWidth',2);hold off%% roifilt2函数实现区域滤波a=imread('4.jpg');I=rgb2gray(a);c=[200 250 278 248 199 172];r=[21 21 75 121 121 75];BW=roipoly(I,c,r); % roipoly函数选择图像中的多边形区域h=fspecial('unsharp');J=roifilt2(h,I,BW); % 区域滤波subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(J);%% roifill函数实现对特定区域进行填充a=imread('4.jpg');I=rgb2gray(a);c=[200 250 278 248 199 172];r=[21 21 75 121 121 75];J=roifill(I,c,r); % 对特定区域进行填充subplot(1,2,1),imshow(I);subplot(1,2,2),imshow(J);%}% ----------------图像变换------------------%{% fft2 和ifft2函数分别是计算二维的快速傅里叶变换和反变换f=zeros(100,100);subplot(1,2,1);imshow(f);f(20:70,40:60)=1;subplot(1,2,2);imshow(f);F=fft2(f); % 计算二维的快速傅里叶变换F2=log(abs(F));% 对幅值对对数figure;subplot(1,2,1),imshow(F),colorbar;subplot(1,2,2),imshow(F2),colorbar;%% fftsshift 函数实现了补零操作和改变图像显示象限f=zeros(100,100);subplot(2,2,1),imshow(f);title('f')f(10:70,40:60)=1;subplot(2,2,2),imshow(f);title('f取后')F=fft2(f,256,256);subplot(2,2,3),imshow(F);title('F')F2=fftshift(F); % 实现补零操作subplot(2,2,4),imshow(F2);title('F2')figure,imshow(log(abs(F2)));title('log(|F2|)')%% dct2 函数采用基于快速傅里叶变换的算法,用于实现较大输入矩阵的离散余弦变换% idct2 函数实现图像的二维逆离散余弦变换RGB=imread('drum.jpg');I=rgb2gray(RGB);J=dct2(I); % 对I进行离散余弦变换imshow(log(abs(J))),title('对原图离散后取对数'),colorbar;J(abs(J)<10)=0;K=idct2(J); % 图像的二维逆离散余弦变换figure,imshow(I),title('原灰度图')figure,imshow(K,[0,255]);title('逆离散变换');%% dctmtx 函数用于实现较小输入矩阵的离散余弦变figure;RGB=imread('4.jpg');I=rgb2gray(RGB);subplot(3,2,1),imshow(I),title('原灰度图');I=im2double(I);subplot(3,2,2),imshow(I),title('取双精度后');T=dctmtx(8); % 离散余弦变换subplot(3,2,3),imshow(I),title('离散余弦变换后');B=blkproc(I,[8,8],'P1*x*P2',T,T');subplot(3,2,4),imshow(B),title('blkproc作用I后的B');mask=[ 1 1 1 1 0 0 0 01 1 1 0 0 0 0 01 1 0 0 0 0 0 01 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 00 0 0 0 0 0 0 0 ];B2=blkproc(B,[8,8],'P1.*x',mask);subplot(3,2,5),imshow(B2),title('blkproc作用B后的B2');I2=blkproc(B2,[8,8],'P1*x*P2',T',T);subplot(3,2,6),imshow(I2),title('blkproc作用B2后的I2');%% edge函数用于提取图像的边缘RGB=imread('4.jpg');I=rgb2gray(RGB);BW=edge(I);imshow(I);figure,imshow(BW);%% radon 函数用来计算指定方向上图像矩阵的投影RGB=imread('4.jpg');I=rgb2gray(RGB);BW=edge(I);theta=0:179;[R,XP]=radon(BW,theta); % 图像矩阵的投影figure,imagesc(theta,XP,R);colormap(hot);xlabel('\theta(degrees)');ylabel('x\prime');title('R_{\theta}(x\prime)');colorbar;%}% ----------------图像增强、分割和编码------------------%{% imhist 函数产生图像的直方图A=imread('4.jpg');B=rgb2gray(A);subplot(2,1,1),imshow(B);subplot(2,1,2),imhist(B);%% histeq 函数用于对图像的直方图均衡化A=imread('4.jpg');B=rgb2gray(A);subplot(2,1,1),imshow(B);subplot(2,1,2),imhist(B);C=histeq(B); % 对图像B进行均衡化figure;subplot(2,1,1),imshow(C);subplot(2,1,2),imhist(C);%% filter2 函数实现均值滤波a=imread('4.jpg');I=rgb2gray(a);subplot(2,2,1),imshow(I);K1=filter2(fspecial('average',3),I)/255; % 3*3的均值滤波K2=filter2(fspecial('average',5),I)/255; % 5*5的均值滤波K3=filter2(fspecial('average',7),I)/255; % 7*7的均值滤波subplot(2,2,2),imshow(K1);subplot(2,2,3),imshow(K2);subplot(2,2,4),imshow(K3);%% wiener2 函数实现Wiener(维纳)滤波a=imread('4.jpg');I=rgb2gray(a);subplot(2,2,1),imshow(I);K1=wiener2(I,[3,3]); % 3*3 wiener滤波K2=wiener2(I,[5,5]); % 5*5 wiener滤波K3=wiener2(I,[7,7]); % 7*7 wiener滤波subplot(2,2,2),imshow(K1);subplot(2,2,3),imshow(K2);subplot(2,2,4),imshow(K3);%% medfilt2 函数实现中值滤波a=imread('4.jpg');I=rgb2gray(a);subplot(2,2,1),imshow(I);K1=medfilt2(I,[3,3]); % 3*3 中值滤波K2=medfilt2(I,[5,5]); % 5*5 中值滤波K3=medfilt2(I,[7,7]); % 7*7 中值滤波subplot(2,2,2),imshow(K1);subplot(2,2,3),imshow(K2);subplot(2,2,4),imshow(K3);%}% ----------------图像模糊及复原------------------%{% deconvwnr 函数:使用维纳滤波器I=imread('qier.jpg');imshow(I);% 对图像进行模糊处理LEN=31;THETA=11;PSF1=fspecial('motion',LEN,THETA); % 运动模糊PSF2=fspecial('gaussian',10,5); % 高斯模糊Blurred1=imfilter(I,PSF1,'circular','conv'); % 得到运动模糊图像Blurred2=imfilter(I,PSF2,'conv'); % 得到高斯噪声模糊图像figure;subplot(1,2,1);imshow(Blurred1);title('Blurred1--"motion"'); subplot(1,2,2);imshow(Blurred2);title('Blurred2--"gaussian"');% 对模糊图像加噪声V=0.002;BlurredNoisy1=imnoise(Blurred1,'gaussian',0,V); % 加高斯噪声BlurredNoisy2=imnoise(Blurred2,'gaussian',0,V); % 加高斯噪声figure;subplot(1,2,1);imshow(BlurredNoisy1);title('BlurredNoisy1'); subplot(1,2,2);imshow(BlurredNoisy2);title('BlurredNoisy2');% 进行维纳滤波wnr1=deconvwnr(Blurred1,PSF1); % 维纳滤波wnr2=deconvwnr(Blurred2,PSF2); % 维纳滤波figure;subplot(1,2,1);imshow(wnr1);title('Restored1,True PSF'); subplot(1,2,2);imshow(wnr2);title('Restored2,True PSF');%% deconvreg函数:使用约束最小二乘滤波器I=imread('qier.jpg');imshow(I);% 对图像进行模糊处理LEN=31;THETA=11;PSF1=fspecial('motion',LEN,THETA); % 运动模糊PSF2=fspecial('gaussian',10,5); % 高斯模糊Blurred1=imfilter(I,PSF1,'circular','conv'); % 得到运动模糊图像Blurred2=imfilter(I,PSF2,'conv'); % 得到高斯噪声模糊图像figure;subplot(1,2,1);imshow(Blurred1);title('Blurred1--"motion"');subplot(1,2,2);imshow(Blurred2);title('Blurred2--"gaussian"');% 对模糊图像加噪声V=0.002;BlurredNoisy1=imnoise(Blurred1,'gaussian',0,V); % 加高斯噪声BlurredNoisy2=imnoise(Blurred2,'gaussian',0,V); % 加高斯噪声figure;subplot(1,2,1);imshow(BlurredNoisy1);title('BlurredNoisy1');subplot(1,2,2);imshow(BlurredNoisy2);title('BlurredNoisy2');NP=V*prod(size(I));reg1=deconvreg(BlurredNoisy1,PSF1,NP); % 约束最小二乘滤波reg2=deconvreg(BlurredNoisy2,PSF2,NP); % 约束最小二乘滤波figure;subplot(1,2,1);imshow(reg1);title('Restored1 with NP');subplot(1,2,2);imshow(reg2);title('Restored2 with NP');%% deconvlucy函数:使用Lucy-Richardson滤波器I=imread('qier.jpg');imshow(I);% 对图像进行模糊处理LEN=31;THETA=11;PSF1=fspecial('motion',LEN,THETA); % 运动模糊PSF2=fspecial('gaussian',10,5); % 高斯模糊Blurred1=imfilter(I,PSF1,'circular','conv'); % 得到运动模糊图像Blurred2=imfilter(I,PSF2,'conv'); % 得到高斯噪声模糊图像figure;subplot(1,2,1);imshow(Blurred1);title('Blurred1--"motion"');subplot(1,2,2);imshow(Blurred2);title('Blurred2--"gaussian"');% 对模糊图像加噪声V=0.002;BlurredNoisy1=imnoise(Blurred1,'gaussian',0,V); % 加高斯噪声BlurredNoisy2=imnoise(Blurred2,'gaussian',0,V); % 加高斯噪声figure;subplot(1,2,1);imshow(BlurredNoisy1);title('BlurredNoisy1');subplot(1,2,2);imshow(BlurredNoisy2);title('BlurredNoisy2');luc1=deconvlucy(BlurredNoisy1,PSF1,5); % 使用Lucy-Richardson滤波luc2=deconvlucy(BlurredNoisy1,PSF1,15); % 使用Lucy-Richardson滤波figure;subplot(1,2,1);imshow(luc1);title('Restored Image,NUMIT=5'); subplot(1,2,2);imshow(luc2);title('Restored Image,NUMIT=15');%}% deconvblind 函数:使用盲卷积算法a=imread('4.jpg');I=rgb2gray(a);figure;imshow(I);title('Original Image');PSF=fspecial('motion',13,45); % 运动模糊figure;imshow(PSF);Blurred=imfilter(I,PSF,'circ','conv'); % 得到运动模糊图像figure;imshow(Blurred);title('Blurred Image');INITPSF=ones(size(PSF));[J,P]=deconvblind(Blurred,INITPSF,30); % 使用盲卷积figure;imshow(J);figure;imshow(P,[],'notruesize');% *-*--*-*-*-*-*-*-*-*-*-*-*图像处理*-*-*-*-*-*-*-*-*-*-*-* %{% 对图像进行减采样a=imread('lena.jpg');%subplot(1,4,1);figure;imshow(a);title('原图');b=rgb2gray(a);%subplot(1,4,2);figure;imshow(b);title('原图的灰度图');[wid,hei]=size(b);%---4倍减采样----quartimg=zeros(wid/2+1,hei/2+1);i1=1;j1=1;for i=1:2:widfor j=1:2:heiquartimg(i1,j1)=b(i,j);j1=j1+1;endi1=i1+1;j1=1;end%subplot(1,4,3);figure;imshow(uint8(quartimg));title('4倍减采样')% ---16倍减采样---quanrtimg=zeros(wid/4+1,hei/4+1);i1=1;j1=1;for i=1:4:widfor j=1:4:heiquanrtimg(i1,j1)=b(i,j);j1=j1+1;endi1=i1+1;j1=1;end%subplot(1,4,4);.figure;imshow(uint8(quanrtimg));title('16倍减采样');%}% 图像类型% 将图像转换为256级灰度图像,64级灰度图像,32级灰度图像,8级灰度图像,2级灰度图像a=imread('4.jpg');%figure;subplot(2,3,1);imshow(a);title('原图');b=rgb2gray(a); % 这是256灰度级的图像%figure;subplot(2,3,2);imshow(b);title('原图的灰度图像');[wid,hei]=size(b);img64=zeros(wid,hei);img32=zeros(wid,hei);img8=zeros(wid,hei);img2=zeros(wid,hei);for i=1:widfor j=j:heiimg64(i,j)=floor(b(i,j)/4); % 转化为64灰度级endend%figure;subplot(2,3,3);imshow(uint8(img64),[0,63]);title('64级灰度图像');for i=1:widfor j=1:heiimg32(i,j)=floor(b(i,j)/8);% 转化为32灰度级endend%figure;subplot(2,3,4);imshow(uint8(img32),[0,31]);title('32级灰度图像');for i=1:widfor j=1:heiimg8(i,j)=floor(b(i,j)/32);% 转化为8灰度级endend%figure;subplot(2,3,5);imshow(uint8(img8),[0,7]);title('8级灰度图像');for i=1:widfor j=1:heiimg2(i,j)=floor(b(i,j)/128);% 转化为2灰度级endend%figure;subplot(2,3,6);imshow(uint8(img2),[0,1]);title('2级灰度图像');% *-*--*-*-*-*-*-*-*-*-*-*-*图像处理*-*-*-*-*-*-*-*-*-*-*-* %{% ------------------ 图像的点运算------------------I=imread('lena.jpg');figure;subplot(1,3,1);imshow(I);title('原图的灰度图');J=imadjust(I,[0.3;0.6],[0.1;0.9]); % 设置灰度变换的范围subplot(1,3,2);imshow(J);title('线性扩展');I1=double(I); % 将图像转换为double类型I2=I1/255; % 归一化此图像C=2; % 非线性扩展函数的参数K=C*log(1+I2); % 对图像的对数变换subplot(1,3,3);imshow(K);title('非线性扩展');M=255-I;figure;subplot(1,3,1);imshow(M);title('灰度倒置');N1=im2bw(I,0.4); % 将此图像二值化,阈值为0.4N2=im2bw(I,0.7); % 将此图像二值化,阈值为0.7 subplot(1,3,2);imshow(N1);title('二值化阈值0.4');subplot(1,3,3);imshow(N2);title('二值化阈值0.7');%}%{% ------------------ 图像的代数运算------------------% 将两幅图像进行加法运算I=imread('lena.jpg');I=rgb2gray(I);J=imread('rice.png');% 以下把两幅图转化为大小一样for i=1:size(I)for j=size(J):size(I)J(i,j)=0;endendI=im2double(I); % 将图像转化为double型J=im2double(J);% imshow(I);figure;imshow(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,'i_lena1.jpg');%%% 将两幅图像做减运算,分离背景与原图A=imread('i_lena1.jpg');B=imread('rice.png');% 以下把两幅图转化为大小一样for i=1:size(A)for j=size(B):size(A)B(i,j)=0;endendC=A-0.3*B;a=imread('lena.jpg');subplot(2,2,1);imshow(a);title('原图图');subplot(2,2,2);imshow(A);title('混合图');subplot(2,2,3);imshow(B);title('背景图');subplot(2,2,4);imshow(C);title('分离后的图');%% 设置掩模,需要保留下来的区域,掩模图像的值为1,否则为0 A=imread('drum.jpg');A=rgb2gray(A);A=im2double(A);sizeA=size(A);subplot(1,2,1);imshow(A);title('原图');B=zeros(sizeA(1),sizeA(2)); % 设置模板B(100:400,100:500)=1;K=A.*B; % 两幅图像相乘subplot(1,2,2);imshow(K);title('局部图');%}%{% ------------------ 图像的缩放------------------A=imread('drum.jpg');B1=imresize(A,1.5); % 比例放大1.5杯,默认采用的是最近邻法进行线性插值B2=imresize(A,[420 384]); % 非比例放大到420:384C1=imresize(A,0.7); % 比例缩小0.7倍C2=imresize(A,[150 180]); % 非比例缩小到150:180figure;imshow(B1);title('比例放大图');figure;imshow(B2);title('非比例放大图');figure;imshow(C1);title('比例缩小图');figure;imshow(C2);title('非比例缩小图');% 检测非比例缩放得到的图片是否能还原到原图a=size(A)d=imresize(C2,[a(1),a(2)]);figure;imshow(d);%}% ------------------ 图像的旋转------------------I=imread('drum.jpg');J=imrotate(I,45); % 图像进行逆时针旋转,默认采用最近邻插值法进行插值处理K=imrotate(I,90); % 默认旋转出界的部分不被截出subplot(1,3,1);imshow(I);subplot(1,3,2);imshow(J);subplot(1,3,3);imshow(K);% 检测旋转后的图像是否失真P=imrotate(K,270);figure;imshow(P);。

数字图像处理MATLAB在数字图像处理PPT

数字图像处理MATLAB在数字图像处理PPT

感谢您的指导! 再见!
利用Matlab进行数字图像处理 四、利用Matlab进行数字图像处理
由于Matlab的广泛使用,出现了在各个专业领域 由于Matlab的广泛使用,出现了在各个专业领域 使用的工具箱,其中包括功能强大的适用于图像 分析和处理的工具箱,利用这些工具箱所提供的 丰富的函数,我们可以方便地对数字图像进行分 析处理和研究。 同时Matlab支持多种图形文件格式,打开各种不 同时Matlab支持多种图形文件格式,打开各种不 同图像格式的文件时不需要专业软件,使得操作 变得更加灵活。工具箱可靠性很高,因此用户可 以把精力集中在算法而不是编程上,大大提高了 工作效率。
八、参考文献
1 缪绍纲. 数字图像处理:活用Matlab[M ]. 成 缪绍纲. 数字图像处理:活用Matlab[M 都:西南交通大学出版社, 2001. 98 - 112. 西南交通大学出版社, 2 阮秋琦. 数字图像处理学[M ]. 北京:电子工 阮秋琦. 数字图像处理学[M 北京: 业出版社, 业出版社, 2001. 390 - 424. 3林蔚天. 图像处理与汽车牌照识别[ J ]. 山东 林蔚天. 图像处理与汽车牌照识别[ 理工大学学报, 理工大学学报, 2003, (6) : 30 - 34.
三、MATLAB的主要功能 MATLAB的主要功能
主要具备数值计算功能,符号计算功能,数据分 析功能,动态仿真功能,图形文字统一处理功能。 MATLAB可以进行图像类型转换,提供4 MATLAB可以进行图像类型转换,提供4种类型: 真彩色(RGB),索引色图像,灰度图像和二值 真彩色(RGB),索引色图像,灰度图像和二值 RGB 图像。在图像处理技术中,图像的正交变换技术 有着广泛的应用,通过变换,改变的图像的表示 域及表示数据。如傅里叶变换,离散余弦变换, radon变换,离散小波变换等,此外在图像增强, radon变换,离散小波变换等,此外在图像增强, 边缘检测和图像分割方面有很好的应用,利用 MATLAB有效的促进了数字图像处理的研究开发。 MATLAB有效的促进了数字图像处理的研究开发。

(完整版)数字图像处理MATLAB程序【完整版】

(完整版)数字图像处理MATLAB程序【完整版】

第一部分数字图像处理实验一图像的点运算实验1.1 直方图一.实验目的1.熟悉matlab图像处理工具箱及直方图函数的使用;2.理解和掌握直方图原理和方法;二.实验设备1.PC机一台;2.软件matlab。

三.程序设计在matlab环境中,程序首先读取图像,然后调用直方图函数,设置相关参数,再输出处理后的图像。

I=imread('cameraman.tif');%读取图像subplot(1,2,1),imshow(I) %输出图像title('原始图像') %在原始图像中加标题subplot(1,2,2),imhist(I) %输出原图直方图title('原始图像直方图') %在原图直方图上加标题四.实验步骤1. 启动matlab双击桌面matlab图标启动matlab环境;2. 在matlab命令窗口中输入相应程序。

书写程序时,首先读取图像,一般调用matlab自带的图像,如:cameraman图像;再调用相应的直方图函数,设置参数;最后输出处理后的图像;3.浏览源程序并理解含义;4.运行,观察显示结果;5.结束运行,退出;五.实验结果观察图像matlab环境下的直方图分布。

(a)原始图像 (b)原始图像直方图六.实验报告要求1、给出实验原理过程及实现代码;2、输入一幅灰度图像,给出其灰度直方图结果,并进行灰度直方图分布原理分析。

实验1.2 灰度均衡一.实验目的1.熟悉matlab图像处理工具箱中灰度均衡函数的使用;2.理解和掌握灰度均衡原理和实现方法;二.实验设备1.PC机一台;2.软件matlab;三.程序设计在matlab环境中,程序首先读取图像,然后调用灰度均衡函数,设置相关参数,再输出处理后的图像。

I=imread('cameraman.tif');%读取图像subplot(2,2,1),imshow(I) %输出图像title('原始图像') %在原始图像中加标题subplot(2,2,3),imhist(I) %输出原图直方图title('原始图像直方图') %在原图直方图上加标题a=histeq(I,256); %直方图均衡化,灰度级为256subplot(2,2,2),imshow(a) %输出均衡化后图像title('均衡化后图像') %在均衡化后图像中加标题subplot(2,2,4),imhist(a) %输出均衡化后直方图title('均衡化后图像直方图') %在均衡化后直方图上加标题四.实验步骤1. 启动matlab双击桌面matlab图标启动matlab环境;2. 在matlab命令窗口中输入相应程序。

数字图像处理实验程序MATLAB

数字图像处理实验程序MATLAB

实验一内容(一)(1)彩色图像变灰度图像A=imread('1.jpg');B=rgb2gray(A);figuresubplot(1,2,1),imshow(A)title('原图')subplot(1,2,2),imshow(B)title('原图灰度图像')(2)彩色图像变索引图像A=imread('1.jpg');figuresubplot(1,2,1),imshow(A)title('原图')[X,map]=rgb2ind(A,128);subplot(1,2,2),imshow(X,map)title('原图索引图像')(3)彩色图像变二值图像A=imread('1.jpg');figuresubplot(1,2,1),imshow(A)title('原图')C=im2bw(A,0.2);subplot(1,2,2),imshow(C)title('原图二值图像')(4)灰度图像变索引图像(一)A=imread('1.jpg');figureB=rgb2gray(A);subplot(1,2,1),imshow(B)title('灰度图像')C=grayslice(B,39);subplot(1,2,2),imshow(C)title('灰度变索引图像')(5)灰度图像变索引图像(二)A=imread('1.jpg');figureB=rgb2gray(A);subplot(1,2,1),imshow(B)title('灰度图像')[X,map]=gray2ind(B,63);subplot(1,2,2),imshow(X,map)title('灰度变索引图像')(6)灰度图像变彩色图像A=imread('1.jpg');figureB=rgb2gray(A);subplot(1,2,1),imshow(B)title('灰度图像')C=gray2rgb(B,map);subplot(1,2,2),imshow(C)title('灰度变彩色图像')内容(二)(1)灰度平均值A=imread('1.jpg');figureB=rgb2gray(A);subplot(1,2,1),imshow(B)title('灰度图像')B=double(B);[m,n]=size(B);sumg=0.0;for i=1:m;for j=1:n;sumg=sumg+B(i,j);endendavg=sumg/(m*n) % 均值maxg=max(max(B)) % 区域最大灰度ming=min(min(B)) % 区域最小灰度(2)彩色平均值figureimshow(A)title('彩色图像')A=double(A);[m,n]=size(A);sumg=0.0;for i=1:m;for j=1:n;sumg=sumg+A(i,j);endendavg=sumg/(m*n)squre=m*nmaxg=max(max(A))ming=min(min(A))内容(三)采样量化实验二图像变换傅里叶变换、反变换、I=imread('19.jpg');A=rgb2gray(I);x1=fft2(A);x2=fftshift(x1);x3=ifft(x1)/10;figure,subplot(1,4,1);imshow(A)title('原图');subplot(1,4,2);imshow(x1)title('频谱图');subplot(1,4,3);imshow(log(abs(x2)+1),[0 10]);title('直流分量移至频谱图中心');subplot(1,4,4);imshow(x3,[0 10])title('傅里叶反变换');DCT变换、反变换I=rgb2gray(X);subplot(1,3,1);imshow(I);title('原图');subplot(1,3,2);J=dct2(I);imshow(log(abs(J)),[0 20]);title('二维离散余弦变换');subplot(1,3,3);K=idct2(J)/20;imshow(K,[0 20]);title('二维离散反余弦变换');利用DCT变换压缩图像I=imread('19.jpg');A=rgb2gray(I);B=DCT2(A);B(abs(B)<0.1)=0;C=idct2(B)/255;figure,subplot(1,3,1);imshow(A);title('原图');subplot(1,3,2);imshow(B);title('二维离散余弦变换频谱图');subplot(1,3,3);imshow(C);title('压缩后图像');实验三图像增强(一)灰度图像增强(1)线性变换法clc;clear all;I=imread('19.jpg');A=rgb2gray(I);colormap;imshow(A);%设置图像倒数参数j=imadjust(A,[0 1],[1 0],1.5);figure;subimage(j)(2)灰度图像的非线性变换(之对数)I=imread('19.jpg');colormapimshow(I)J=double(I);J=45*log(J+1);I=uint8(J);figure,subimage(J)(二)直方图校正直方图均衡I=imread('19.jpg');B=rgb2gray(I);imshow(B,[40 255]);figure,imhist(B)title('直方图')J=imadjust(B,[0.15 0.9],[0 1]); figure,imhist(B,64)title('均衡直方图')滤波I=imread('19.jpg');figure,B=rgb2gray(I);C=imnoise(B,'salt & pepper',0.02);D=imfilter(B,fspecial('average',3)); E=medfilt2(B);subplot(1,3,2)imshow(D)title('均值滤波')subplot(1,3,3)imshow(D)title('中值滤波')subplot(1,3,1)imshow(C)title('加入椒盐噪声图像')锐化处理I=imread('19.jpg');A=rgb2gray(I);figure,subplot(2,3,1),imshow(A);title('原图');hs=fspecial('sobel');S=imfilter(A,hs);hp=fspecial('prewitt');P=imfilter(A,hs);A=double(A);%双精度型H=[0 1 0,1 -4 1,0 1 0];%拉普拉斯算子J=conv2(A,H,'same');K=A-J;subplot(2,3,2),imshow(K);title('拉普拉斯锐化图像');B=edge(A,'roberts',0.1);subplot(2,3,3),imshow(B);title('罗伯特锐化图像');subplot(2,3,4),imshow(S);title('sobel算子锐化图像');subplot(2,3,5),imshow(P);title('prewitt算子锐化图像');实验四放缩A=imread('19.jpg');imshow(A);title('原图')B=imresize(A,2)figure,imshow(B);title('二倍图')C=imresize(A,0.5)figureimshow(C)title('二分之一图')旋转A=imread('19.jpg');figuresubplot(1,4,1),imshow(A);title('原图像')B=imrotate(A,30,'nearest');subplot(1,4,2),imshow(uint8(B));title('旋转30度图像')C=imrotate(A,45,'nearest');subplot(1,4,3),imshow(uint8(C));title('旋转45度图像')D=imrotate(A,60,'nearest');subplot(1,4,4),imshow(uint8(D));title('旋转60度图像')镜像A1=imread('19.jpg');A1=double(A1);Figure,subplot(1,4,1),imshow(uint8(A1));H=size(A1);title('原像')A2(1:H(1),1:H(2),1:H(3))=A1(H(1):-1:1,1:H(2),1:H(3));%垂直镜像subplot(1,4,2),imshow(uint8(A2));title('垂直镜像')A3(1:H(1),1:H(2),1:H(3))=A1(1:H(1),H(2):-1:1,1:H(3));%水平镜像subplot(1,4,3),imshow(uint8(A3));title('水平镜像')A4(1:H(1),1:H(2),1:H(3))=A1(H(1):-1:1,H(2):-1:1,1:H(3));%对角镜像subplot(1,4,4),imshow(uint8(A4));title('对角镜像')剪切A1=imread('19.jpg');A2=imcrop(A1,[75 68 100 110]);figuresubplot(1,2,1),imshow(A1);title('原像')subplot(1,2,2),imshow(A2);title('剪切后像')实验五阈值分割A=imread('19.jpg');figuresubplot(1,4,1),imshow(A);title('原图像')B=im2bw(A,91/255);subplot(1,4,2),imshow(B);title('阈值91的图像')C=im2bw(A,71/255);subplot(1,4,3),imshow(C);title('阈值71的图像')D=im2bw(A,140/255);subplot(1,4,4),imshow(D);title('阈值140的图像')边缘检测I=imread('19.jpg');A=rgb2gray(I);figuresubplot(1,4,1),imshow(A);title('原图像')B=edge(A,'sobel',0.1);%edge边缘检测函数subplot(1,4,2),imshow(B);title('sobel算子检测')C=edge(A,'roberts',0.1);%0.1为门限subplot(1,4,3),imshow(C);title('roberts算子检测')D=edge(A,'prewitt',0.1);subplot(1,4,4),imshow(D);title('prewitt算子检测')所谓数字图像处理[7]就是利用计算机对图像信息进行加工以满足人的视觉心理或者应用需求的行为。

数字图像处理概述

数字图像处理概述

光电结合处理:
用光学方法完成运算量巨大的处理(如频 谱变换等),而用计算机对光学处理结果 (如频谱)进行分析判断等处理。
该方法是前两种方法的有机结合,它集结 了二者的优点。
光电结合处理是今后图像处理的发展方向 ,也是一个值得关注的研究方向。
三、数字图像的表示方法
图像的数学表示:
一幅图像所包含的信息首先表现为光的强度 (intensity),即一幅图像可看成是空间各个 坐标点上的光强度I 的集合,其普遍数学表达 式为:
I = f (x,y,z,λ,t) , 式中 x,y,z 是空间坐标, λ是波长, t是时间
, I是光点(x,y,z) 的强度(幅度)。 上式表示一幅运动的 (t) 、彩色/多光谱的
(λ)、立体的(x,y,z) 图像。
静止图像,与时间t无关; 单色图像(也称灰度图像)波长λ为一常数 平面图像,则与坐标z无关。 在每一种情况下图像的表示可省略掉一维,即
字图像处理
Reference 主要参考文献
1. R.C. Gonzalez and R.E. Woods, “Digital Image Processing”, 3rd Ed., Prentice-Hall’2008
2. 数字图像处理 阮秋琦等译 电子工业出版社 3. Any other book with a similar title is fine
图片
采样列间隔

255
➢灰度级 灰度图像(128x128)及其对应的数值矩阵
(仅列出一部分(26x31))
125,153,158,157,127, 70,103,120,129,144,144,150,150,147,150,160,165,160,164,165, 167,175,175,166,133, 60, 133,154,158,100,116,120, 97, 74, 54, 74,118,146,148,150,145,157,164,157,158,162,165,171,155,115, 88, 49, 155,163, 95,112,123,101,137,108, 81, 71, 63, 81,137,142,146,152,159,161,159,154,138, 81, 78, 84,114, 95, 167, 69, 85, 59, 65, 43, 85, 34, 69, 78,104,101,117,132,134,149,160,165,158,143,114, 99, 57, 45, 51, 57,

数字图像处理MATLAB程序

数字图像处理MATLAB程序

数字图象处理MATLAB程序数字图象处理是指对数字图象进行各种操作和处理,以改善图象的质量、增强图象的特征、提取图象的信息等。

MATLAB是一种强大的数值计算和数据可视化软件,也是数字图象处理领域常用的工具之一。

本文将介绍如何使用MATLAB编写数字图象处理程序的标准格式。

一、引言在引言部份,需要对数字图象处理的背景和意义进行简要介绍。

可以从以下几个方面进行描述:1. 数字图象处理的定义和作用;2. 数字图象处理在各个领域的应用,如医学影像、遥感图象等;3. MATLAB在数字图象处理中的重要性和优势。

二、问题描述在问题描述部份,需要明确说明本文将要解决的具体问题。

可以从以下几个方面进行描述:1. 需要进行的数字图象处理操作,如图象增强、图象滤波、图象分割等;2. 需要处理的图象的特点和要求,如图象的大小、图象的格式等;3. 需要实现的目标和效果。

三、方法与算法在方法与算法部份,需要详细介绍用于解决问题的具体方法和算法。

可以从以下几个方面进行描述:1. 图象预处理:对图象进行去噪、灰度化、尺寸调整等预处理操作;2. 图象增强:使用直方图均衡化、滤波器等方法增强图象的对照度和清晰度;3. 图象分割:使用阈值分割、边缘检测等方法将图象分割为不同的区域;4. 特征提取:提取图象的纹理特征、形状特征等;5. 图象重建:根据处理后的图象进行图象重建和修复。

四、MATLAB程序实现在MATLAB程序实现部份,需要给出具体的代码实现,并附上详细的注释。

可以从以下几个方面进行描述:1. 导入图象:使用MATLAB的图象处理工具箱中的函数导入图象;2. 图象预处理:使用MATLAB的函数对图象进行预处理;3. 图象增强:使用MATLAB的函数对图象进行增强;4. 图象分割:使用MATLAB的函数对图象进行分割;5. 特征提取:使用MATLAB的函数提取图象的特征;6. 图象重建:根据处理后的图象进行图象重建和修复。

数字图像处理实验三(附源程序)

数字图像处理实验三(附源程序)

南京航空航天大学 close al329
某某
%%%%%%%%处理 test3_1.jpg I=imread('test3_1.jpg'); I=im2double(I); %%%%%%%%邻域平均函数“nlfilter” tic J=nlfilter(I,[5 5],@mean2); toc tic T=nlfilter(I,[5 5],@std2); toc figure(1),subplot(2,3,1),imshow(I),title('原始图像');axis on; subplot(2,3,2),imshow(J),title('邻域均值图像');axis on; subplot(2,3,3),imshow(T),title('邻域均值标准差图像');axis on; imwrite(J,'nlfilterl 滤波后.jpg'); %%%%%%%%加噪声后用邻域平均法 I=imnoise(I,'gaussian',0,0.02); J=nlfilter(I,[5 5],@mean2); T=nlfilter(I,[5 5],@std2); subplot(2,3,4),imshow(I),title('加噪图像');axis on; subplot(2,3,5),imshow(J),title('加噪邻域均值图像');axis on; subplot(2,3,6),imshow(T),title('加噪邻域均值标准差图像');axis on; imwrite(J,'加噪 nlfilterl 滤波后.jpg'); %%%%%%%%%处理 test3_2.jpg I2=imread('test3_2.jpg'); J2=im2double(I2); %%%%%%%%%邻域平均窗函数“filter2” tic ave=fspecial('average',5); J2=filter2(ave,J2); 南京航空航天大学 电子信息工程学院 信息工程 041010329 某某

matlab数字图像处理源代码

matlab数字图像处理源代码

数字图像去噪典型算法及matlab实现希望得到大家的指点和帮助图像去噪是数字图像处理中的重要环节和步骤。

去噪效果的好坏直接影响到后续的图像处理工作如图像分割、边缘检测等。

图像信号在产生、传输过程中都可能会受到噪声的污染,一般数字图像系统中的常见噪声主要有:高斯噪声(主要由阻性元器件内部产生)、椒盐噪声(主要是图像切割引起的黑图像上的白点噪声或光电转换过程中产生的泊松噪声)等;目前比较经典的图像去噪算法主要有以下三种:均值滤波算法:也称线性滤波,主要思想为邻域平均法,即用几个像素灰度的平均值来代替每个像素的灰度。

有效抑制加性噪声,但容易引起图像模糊,可以对其进行改进,主要避开对景物边缘的平滑处理。

中值滤波:基于排序统计理论的一种能有效抑制噪声的非线性平滑滤波信号处理技术。

中值滤波的特点即是首先确定一个以某个像素为中心点的邻域,一般为方形邻域,也可以为圆形、十字形等等,然后将邻域中各像素的灰度值排序,取其中间值作为中心像素灰度的新值,这里领域被称为窗口,当窗口移动时,利用中值滤波可以对图像进行平滑处理。

其算法简单,时间复杂度低,但其对点、线和尖顶多的图像不宜采用中值滤波。

很容易自适应化。

Wiener维纳滤波:使原始图像和其恢复图像之间的均方误差最小的复原方法,是一种自适应滤波器,根据局部方差来调整滤波器效果。

对于去除高斯噪声效果明显。

实验一:均值滤波对高斯噪声的效果I=imread('C:\Documents and Settings\Administrator\桌面\1.gif');%读取图像J=imnoise(I,'gaussian',0,0.005);%加入均值为0,方差为0.005的高斯噪声subplot(2,3,1);imshow(I);title('原始图像');subplot(2,3,2); imshow(J);title('加入高斯噪声之后的图像');%采用MATLAB中的函数filter2对受噪声干扰的图像进行均值滤波K1=filter2(fspecial('average',3),J)/255; %模板尺寸为3K2=filter2(fspecial('average',5),J)/255;% 模板尺寸为5K3=filter2(fspecial('average',7),J)/255; %模板尺寸为7K4= filter2(fspecial('average',9),J)/255; %模板尺寸为9subplot(2,3,3);imshow(K1);title('改进后的图像1');subplot(2,3,4); imshow(K2);title('改进后的图像2');subplot(2,3,5);imshow(K3);title('改进后的图像3');subplot(2,3,6);imshow(K4);title('改进后的图像4');PS:filter2用法:filter2用法fspecial函数用于创建预定义的滤波算子,其语法格式为:h = fspecial(type)h = fspecial(type,parameters)参数type制定算子类型,parameters指定相应的参数,具体格式为:type='average',为均值滤波,参数为n,代表模版尺寸,用向量表示,默认值为[3,3]。

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。

%系统自动生成的创建对话框的代码function varargout = myproject(varargin)% MYPROJECT M-file for myproject.fig% MYPROJECT, by itself, creates a new MYPROJECT or raises the existing% singleton*.%% H = MYPROJECT returns the handle to a new MYPROJECT or the handle to% the existing singleton*.%% MYPROJECT('CALLBACK',hObject,eventData,handles,...) calls the local% function named CALLBACK in MYPROJECT.M with the given input arguments. %% MYPROJECT('Property','Value',...) creates a new MYPROJECT or raises the% existing singleton*. Starting from the left, property value pairs are% applied to the GUI before myproject_OpeningFcn gets called. An% unrecognized property name or invalid value makes property application% stop. All inputs are passed to myproject_OpeningFcn via varargin.%% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one% instance to run (singleton)".%% See also: GUIDE, GUIDA TA, GUIHANDLES% Edit the above text to modify the response to help myproject% Last Modified by GUIDE v2.5 07-Jun-2008 11:33:02% Begin initialization code - DO NOT EDITgui_Singleton = 1;gui_State = struct('gui_Name', mfilename, ...'gui_Singleton', gui_Singleton, ...'gui_OpeningFcn', @myproject_OpeningFcn, ...'gui_OutputFcn', @myproject_OutputFcn, ...'gui_LayoutFcn', [] , ...'gui_Callback', []);if nargin && ischar(varargin{1})gui_State.gui_Callback = str2func(varargin{1});endif nargout[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});elsegui_mainfcn(gui_State, varargin{:});end% End initialization code - DO NOT EDIT% --- Executes just before myproject is made visible.function myproject_OpeningFcn(hObject, eventdata, handles, varargin)% This function has no output args, see OutputFcn.% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDA TA)% varargin command line arguments to myproject (see V ARARGIN)% Choose default command line output for myprojecthandles.output = hObject;% Update handles structureguidata(hObject, handles);% UIWAIT makes myproject wait for user response (see UIRESUME)% uiwait(handles.figure1);% --- Outputs from this function are returned to the command line.function varargout = myproject_OutputFcn(hObject, eventdata, handles)% varargout cell array for returning output args (see V ARARGOUT);% hObject handle to figure% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDA TA)% Get default command line output from handles structurevarargout{1} = handles.output;%以下为另存为按钮的回调函数,功能为存储图像处理后的图像到用户选择的磁盘空间中。

% --- Executes on button press in lingcunwei.function lingcunwei_Callback(hObject, eventdata, handles) %另存为按钮的回调函数global ImagenUmbral %定义全局变量if isempty(ImagenUmbral)==1,msgbox('Doesn''t exist an image');return,end %如果ImagenUmbral不包含图像,则弹出对话框并显示'Doesn''t exist an image' [filename,pathname]=uiputfile({'*.jpg';,'*.tif';,'*.gif';,'*.bmp';,'*.png';, ...'*.hdf';,'*.pcx';,'*.xwd';,'*.ico';,'*.cur';,'*.ras';, ...'*.pdm';,'*.pgm';,'*.ppm'},'Save file name'); %显示保存文件的对话框if isequal(filename,0) | isequal(pathname,0)errordlg('Saving canceled','Threshold GUI'); error('Saving canceled')else %如果不存在该文件,或者不存在保存路径,则显示错误信息tryimwrite(ImagenUmbral,[ pathname,filename]); %保存文件end %tryend %if% hObject handle to lingcunwei (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDATA)%打印按钮的回调函数% --- Executes on button press in dayinshuchu.function dayinshuchu_Callback(hObject, eventdata, handles) %打印按钮的回调函数printdlg %显示打印对话框% hObject handle to dayinshuchu (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDA TA)%复制到剪切板的回调函数,其功能为将处理后的图像存储到剪切板,以备处理图像处理后的图像之用。

% --- Executes on button press in fuzhidaojianqieban.function fuzhidaojianqieban_Callback(hObject, eventdata, handles)%复制到剪切板按钮的回调函数global ImagenUmbral %处理后的图像global J %处理前的图像J=ImagenUmbral %将处理后的图像赋予处理前的图像% hObject handle to fuzhidaojianqieban (see GCBO)% eventdata reserved - to be defined in a future version of MATLAB% handles structure with handles and user data (see GUIDA TA)%几何变换的回调函数,它包含三个基本的几何变换,分别为水平变换,垂直变换,对角变换。

相关文档
最新文档