您好,欢迎访问三七文档
当前位置:首页 > 临时分类 > 南昌大学数字图像处理实验程序2~7所有打包
说明:程序都是经过调试很久后弄上来的,是最后的上交给老师的版本,昌大学弟学妹们可放心使用~!实验二、图像灰度变换、图像取反、直方图均衡%%图像灰度变换a=imread('G:\shuzituxiangshiyan\1\me.jpg');A=rgb2gray(a);stretchlim(A);%看看原图本来的灰度范围.B=imadjust(A,stretchlim(A),[]);%默认为[01]C=GrayTransf(A,0,1);%调用自己编写的GrayTransf函数.D=imadjust(A,stretchlim(A),[0.10.8]);%灰度变换至[0.0.8]E=GrayTransf(A,0.1,0.8);%调用自己编写的GrayTransf函数.figure(1);subplot(3,2,1),imshow(a),title('原图');subplot(3,2,2),imshow(A),title('灰度原图');subplot(3,2,3),imshow(B),title('变换到区间[01]图');subplot(3,2,4),imshow(C),title('调用函数变换到区间[01]图');subplot(3,2,5),imshow(D),title('变换到区间[0.10.8]图');subplot(3,2,6),imshow(E),title('调用函数变换到区间[0.10.8]图');%%图像取反a=imread('G:\shuzituxiangshiyan\1\me.jpg');A=rgb2gray(a);d=A;[m,n]=size(d);fori=1:mforj=1:ne(i,j)=255-d(i,j);%用225减像素值,实现黑白转换.endendfigure(2);subplot(2,2,1);imshow(d);title('原始灰度图');subplot(2,2,2);imshow(e);title('反变换图像');subplot(2,2,3);imhist(d);title('反变换图像灰度分布');subplot(2,2,4);imhist(e);title('反变换图像灰度分布');%%另一种反变换a=imread('G:\shuzituxiangshiyan\1\me.jpg');A=rgb2gray(a);f=A;g=imadjust(f,[],[10]);%前面那个中括号,默认是[01],后面调整为[10],相当于是反过来了.figure(3);subplot(2,2,1);imshow(f);title('原始灰度图像');subplot(2,2,2);imshow(g);title('反变换图像');subplot(2,2,3);imhist(f);title('图像灰度分布');subplot(2,2,4);imhist(g);title('反变换图像灰度分布');%%再一种反变换a=imread('G:\shuzituxiangshiyan\1\me.jpg');A=rgb2gray(a);h=A;i=ones(2,1);%定义一个一行两列的矩阵.i=stretchlim(h);%把原灰度图像的灰度级敢为矩阵赋值给a矩阵.a是个列矩阵.j=i';%转为行矩阵.k=[0,1;1,0];l=j*k;%实现矩阵值调换,即[xy]变为[yx]J=imadjust(h,i,l);figure(4);subplot(2,2,1);imshow(h);title('原灰度图像');subplot(2,2,2);imshow(J);title('反变换图像');subplot(2,2,3);imhist(h);title('灰度图像');subplot(2,2,4);imhist(J);title('反变换图像');%%Gamma变换a=imread('G:\shuzituxiangshiyan\1\me.jpg');A=rgb2gray(a);m=A;O=GrayGamma(m,0.5);P=GrayGamma(m,1);Q=GrayGamma(m,2);figure(5);subplot(2,2,1);imshow(m);title('原灰度图像');subplot(2,2,3);imshow(O);title('r=0.5');subplot(2,2,2);imshow(P);title('r=1');subplot(2,2,4);imshow(Q);title('r=2');figure(6);subplot(2,2,1);imhist(m);title('原灰度图像');subplot(2,2,3);imhist(O);title('r=0.5');subplot(2,2,2);imhist(P);title('r=1');subplot(2,2,4);imhist(Q);title('r=2');[rs]=size(m);M=reshape(m,1,r*s);p=reshape(P,1,r*s);o=reshape(O,1,r*s);q=reshape(Q,1,r*s);figure(7);plot(M,o,'r');holdonplot(M,p,'g');holdonplot(M,q,'b');title('r=2红,r=1绿,r=2蓝');figure(8);subplot(2,2,1);plot(M,o,'r');title('r=0.5');subplot(2,2,2);plot(M,p,'g');title('r=1');subplot(2,2,3);plot(M,q,'b');title('r=2');%%直方图均衡a=imread('G:\shuzituxiangshiyan\1\LH.jpg');%这里故意选择了比较暗一点的图像实验!A=rgb2gray(a);n=A;R=GrayEqualize(n);figure(9);subplot(2,2,1);imshow(n);title('原灰度图像');subplot(2,2,3);imhist(n);title('原图像灰度直方图');subplot(2,2,2);imshow(R);title('均衡后灰度图');subplot(2,2,4);imhist(R);title('均衡后灰度直方图');figure(10);subplot(2,2,1);imshow(n);title('原灰度图像');o=histeq(n);subplot(2,2,3);imhist(n);title('原图像灰度直方图');subplot(2,2,2);imshow(o);title('均衡后灰度图');subplot(2,2,4);imhist(o);title('自带均衡函数灰度直方图');imwrite(o,'megrayeq.jpg');%imwrite(tu,'tudemingzi.jpg');%imwrite()有保存图的功能.保存在程序一起的文件夹下面.%%直方图均衡方法的基本原理是:对在图像中像素个数多的灰度值(即对画面起主要作用的灰度值)进行展宽.%%而对像素个数少的灰度值(即对画面不起主要作用的灰度值)进行归并。从而达到清晰图像的目的.实验二、图像滤波:中值滤波器、均值滤波器、频域低通滤波、主函数:tryh=imread('G:\shuzituxiangshiyan\me.jpg');%读入彩色图片c=rgb2gray(h);%把彩色图片转化成灰度图片,默认256级.figure(1),imshow(c),title('原始图象');%显示原始图象g=imnoise(c,'salt&pepper',0.5);%加入高斯噪声,均值0.1,方差0.002.椒盐噪声.[XY]=size(g);figure(2),imshow(g),title('加入椒盐噪声之后的图象');%显示加入高斯噪声之后的图象n=input('请输入均值滤波器模板大小\n');%%用自己的编写的函数进行均值滤波Y1=avgfilter(g,3);%调用自编函数进行均值滤波,n为模板大小figure(3),imshow(Y1),title('用自己的编写的函数进行均值滤波之后的结果');%显示滤波后的图象n2=input('请输入中值滤波的模板的大小\n');%%用自己的编写的函数进行中值滤波Y3=midfilter(g,n2);%调用自己编写的函数进行中值滤波,figure(4),imshow(Y3),title('用自己编写的函数进行中值滤波之后的结果');catch%捕获异常disp(lasterr);%如果程序有异常,输出end子函数1:%%按要求编写的均值函数x是需要滤波的图像,n是模板大小(即n×n)functiond=avgfilter(x,n)%定义均值滤波函数.a(1:n,1:n)=1;%a即n×n模板,元素全是1[height,width]=size(x);%输入图像是hight*width的,且hightn,widthnx1=double(x);x2=x1;fori=1:height-n+1forj=1:width-n+1c=x1(i:i+(n-1),j:j+(n-1)).*a;%取出x1中从(i,j)开始的n行n列元素与模板相乘s=sum(sum(c));%求c矩阵中各元素之和x2(i+(n-1)/2,j+(n-1)/2)=s/(n*n);%将与模板运算后的各元素的均值赋给模板中心位置的元素endend%未被赋值的元素取原值d=uint8(x2);子函数2:%自编的中值滤波函数。x是需要滤波的图像,n是模板大小(即n×n)functiond=midfilter(x,n)[height,width]=size(x);%输入图像是p×q的,且pn,qnx1=double(x);x2=x1;fori=1:height-n+1forj=1:width-n+1c=x1(i:i+(n-1),j:j+(n-1));%取出x1中从(i,j)开始的n行n列元素,即模板(n×n的)e=c(1,:);%e是矩阵c的第一行foru=2:ne=[e,c(u,:)];%将c矩阵变为一个行矩阵endmm=median(e);%mm是中值x2(i+(n-1)/2,j+(n-1)/2)=mm;%将模板各元素的中值赋给模板中心位置的元素endend%未被赋值的元素取原值d=uint8(x2);%%频域低通滤波进行去噪.i=imread('G:\shuzituxiangshiyan\2\me.jpg');I0=rgb2gray(i);subplot(2,3,1),imshow(I0);title('原图');I1=imnoise(I0,'gaussian');%对原图像加噪声subplot(2,3,2),imshow(I1);title('加入噪声后')%将灰度图像的二维不连续Fourier变换的零频率(DC)成分移到频谱的中心s=fftshift(fft2(I1));%s=(fft2(I1));%fftshift()%%fftshift就是对换数据的左右两边比如??subplot(2,3,3),imshow(log(1+abs(s)),[]);title('fftshift');[M,N]=size(s);%分别返回s的行数到M中,列数到N中%%n1=floor(M/2);%对M/2进行取整n2=floor(N/2);%对N/2进行取整%%%ILPF滤波(程序中以d0=45为例)d0=45;%d0为带通半径~fori=1:Mforj=1:Nd=sqrt((i-n1)^2+(j-n2)^2);%点(i,j)到傅立叶变换中心的距离ifd=d0%点(i,j)在通带内的情况h(i,j)=1;%通带变换函数else%点(i,j)在阻带内的情况h(i,j)=0;%阻带变换函数ends(i,j)=h(i,j)*s(i,j);%I
本文标题:南昌大学数字图像处理实验程序2~7所有打包
链接地址:https://www.777doc.com/doc-2597393 .html