1、南昌大学 数字图像处理实验程序27所有打包说明:程序都是经过调试很久后弄上来的,是最后的上交给老师的版本,昌大学弟学妹们可放心使用!实验二、图像灰度变换、图像取反、直方图均衡% 图像灰度变换a = imread(G:shuzi tuxiang shiyan1me.jpg);A=rgb2gray(a);stretchlim(A); %看看原图本来的灰度范围.B = imadjust(A,stretchlim(A),); %默认为0 1C=GrayTransf(A,0,1); %调用自己编写的GrayTransf函数.D=imadjust(A,stretchlim(A),0.1 0.8); %灰度
2、变换至0. 0.8E=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(变换到区间0 1图);subplot(3,2,4),imshow(C),title(调用函数变换到区间0 1图);subplot(3,2,5),imshow(D),title(变换到区间0.1 0.8图);subplot(3,2,6),imshow(E),tit
3、le(调用函数变换到区间0.1 0.8图);% 图像取反a = imread(G:shuzi tuxiang shiyan1me.jpg);A=rgb2gray(a);d=A;m,n=size(d);for i=1:m for j=1:n e(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
4、,2,4);imhist(e);title(反变换图像灰度分布);% 另一种反变换a = imread(G:shuzi tuxiang shiyan1me.jpg);A=rgb2gray(a);f=A;g=imadjust(f,1 0); %前面那个中括号,默认是0 1,后面调整为1 0,相当于是反过来了.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);imhis
5、t(g);title(反变换图像灰度分布);% 再一种反变换a = imread(G:shuzi tuxiang shiyan1me.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; %实现矩阵值调换,即x y变为y xJ = imadjust(h,i,l);figure(4);subplot(2,2,1);imshow(h);title(原灰度图像);subplot(2,2,2);imshow(
6、J);title(反变换图像);subplot(2,2,3);imhist(h);title(灰度图像);subplot(2,2,4);imhist(J);title(反变换图像);% Gamma变换a = imread(G:shuzi tuxiang shiyan1me.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);s
7、ubplot(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);r s=size(m);M=reshape(m,1,r*s);p=reshape(P,1,r*s);o=reshape(O,1,r*s);q=
8、reshape(Q,1,r*s);figure(7);plot(M,o,r);hold onplot(M,p,g);hold onplot(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:shuzi tuxiang shiyan1LH.jpg); % 这里故意选择了比较暗一点的图像实验!A=rgb
9、2gray(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(原图像灰度直方图
10、);subplot(2,2,2);imshow(o);title(均衡后灰度图);subplot(2,2,4);imhist(o);title(自带均衡函数灰度直方图);imwrite(o,megrayeq.jpg); % imwrite(tu,tudemingzi.jpg); %imwrite()有保存图的功能.保存在程序一起的文件夹下面.% % 直方图均衡方法的基本原理是:对在图像中像素个数多的灰度值(即对画面起主要作用的灰度值)进行展宽.% % 而对像素个数少的灰度值(即对画面不起主要作用的灰度值)进行归并。从而达到清晰图像的目的.实验二、图像滤波:中值滤波器、均值滤波器、频域低通滤波、
11、主函数:tryh=imread(G:shuzi tuxiang shiyanme.jpg); %读入彩色图片c=rgb2gray(h); %把彩色图片转化成灰度图片,默认256级.figure(1),imshow(c),title(原始图象); %显示原始图象g=imnoise(c,salt & pepper,0.5); %加入高斯噪声,均值0.1,方差0.002. 椒盐噪声.X Y=size(g);figure(2),imshow(g),title(加入椒盐噪声之后的图象); %显示加入高斯噪声之后的图象 n=input(请输入均值滤波器模板大小n); % 用自己的编写的函数进行均值滤波 Y
12、1=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是需要滤波的图像
13、,n是模板大小(即nn)function d=avgfilter(x,n) %定义均值滤波函数. a(1:n,1:n)=1; %a即nn模板,元素全是1height, width=size(x); %输入图像是hight*width的,且hightn,widthnx1=double(x);x2=x1;for i=1:height-n+1 for j=1:width-n+1 c=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
14、); %将与模板运算后的各元素的均值赋给模板中心位置的元素 endend%未被赋值的元素取原值d=uint8(x2);子函数2:%自编的中值滤波函数。x是需要滤波的图像,n是模板大小(即nn)function d=midfilter(x,n) height, width=size(x); %输入图像是pq的,且pn,qnx1=double(x);x2=x1;for i=1:height-n+1 for j=1:width-n+1 c=x1(i:i+(n-1),j:j+(n-1); %取出x1中从(i,j)开始的n行n列元素,即模板(nn的) e=c(1,:); %e是矩阵c的第一行 for u
15、=2:n e=e,c(u,:); %将c矩阵变为一个行矩阵 end mm=median(e); %mm是中值 x2(i+(n-1)/2,j+(n-1)/2)=mm; %将模板各元素的中值赋给模板中心位置的元素 endend %未被赋值的元素取原值d=uint8(x2);% 频域低通滤波进行去噪.i = imread(G:shuzi tuxiang shiyan2me.jpg); I0 = rgb2gray(i); subplot(2,3,1),imshow(I0);title(原图);I1 = imnoise(I0,gaussian); %对原图像加噪声subplot(2,3,2),imsho
16、w(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
17、为带通半径for i=1:M for j=1:N d=sqrt(i-n1)2+(j-n2)2); %点(i,j)到傅立叶变换中心的距离 if d=7); % 第二种图像增强,选取阈值,使明显的边缘轮廓得到突出, J2(K)=G(K); %又不会破坏原来的灰度变化平缓部分J3=I; K=find(G=7); % 第三种图像增强J3(K)=255; %大于该阈值处灰度值为255J4=I; K=find(G=7); %第四种图像增强 J4(K)=255; %小于该阈值处灰度值为255J5=I; K=find(G=7); J5(Q)=255; %大于该阈值处灰度值为255figure, subplot
18、(2,3,1),imshow(I,); title(原图像); subplot(2,3,2),imshow(J1,); title(第一种); subplot(2,3,3),imshow(J2,); title(第二种); subplot(2,3,4),imshow(J3,); title(第三种); subplot(2,3,5),imshow(J4,); title(第四种); subplot(2,3,6),imshow(J5,); title(第五种);实验四、图像伪彩色处理clc;cj=imread(G:shuzi tuxiang shiyan上交程序4fengjing.jpg);img
19、=rgb2gray(cj);z=3;x y=size(img);% imshow(img);img=double(img);img_cai=zeros(x,y,z);Max=max(max(img);Min=min(min(img);img=(255/(Max-Min)*img-(255*Min)/(Max-Min);figure,subplot(131),imshow(cj);subplot(132),imshow(img,);r=1;g=2;b=3;for i=1:x for j=1:y temp=(2*pi/(Max-Min)*img(i,j)-(2*pi*Min)/(Max-Min);
20、 if temppi/2 & temppi & temppi*3/2 img_cai(i,j,r)=255*(-sin(temp); img_cai(i,j,g)=0; img_cai(i,j,b)=0; end endendsubplot(133),imshow(img_cai,);% 另一种效果更佳的伪彩色处理 选取了一张色彩比较足的风景图.main:a=imread(G:shuzi tuxiang shiyan上交程序4fengjing.jpg);% a=imresize(cj,200 300); %y原图太大,改小尺寸. imresize()% imwrite(a,b.jpg,jpg) %首先图像灰度化并存入对应文件夹下.% b=imread(G:shuzi tuxiang shiyan上交程序4b.jpg);b=rgb2gray(a);imwrite(b,fengjing1.jpg,jpg) %首先图像灰度化并存入对应文件夹下.i=G:shuzi tuxiang shiyan上交程序4fengjing1.jpg; %原图对应的灰度图像路径.j=G:shuzi tuxiang shiyan上交程序4fengjing.jpg; %原图像路径.c
copyright@ 2008-2022 冰豆网网站版权所有
经营许可证编号:鄂ICP备2022015515号-1