【实验目的】

1.掌握常见的图像增强方法

2.掌握利用Matlab进行编程实现图像增强

3.观察图像增强前后的效果

【实验内容】

1.利用Matlab对标准测试图像和自建图像进行对比度增强、直方图均衡化和直方图规定化

2.利用Matlab对标准测试图像和自建图像进行加噪处理,对含噪声图像进行均值滤波、中值滤波和高斯滤波处理

3.利用Matlab对标准测试图像和自建图像进行锐化处理

4.利用Matlab对标准测试图像和自建图像进行Butterworth低通、高通滤波处理

【实验要求】

1.写出实现图像增强的Matlab源代码

2.给出图像增强前后的效果图

实验程序

增强对比度:

clear;
close all;
 X1=imread('F:\MatlabShijueTupian/flower.JPG');
 figure(1),subplot(2,2,1),imshow(X1),title('原图像');
 f0=0;g0=0;
 f1=70;g1=30;
 f2=180;g2=230;
 f3=255;g3=255; %绘制变换函数曲线
 subplot(1,2,2),plot([f0,f1,f2,f3],[g0,g1,g2,g3])
 axis tight,xlabel('f'),ylabel('g')
 title('增强对比度的变换曲线')
 r1=(g1-g0)/(f1-f0);
 b1=g0-r1*f0;
 r2=(g2-g1)/(f2-f1);
 b2=g1-r2*f1;
 r3=(g3-g2)/(f3-f2);
 b3=g2-r3*f2;
 [m,n]=size(X1);
 X2=double(X1);
 for i=1:m  %循环对矩阵中的每个元素进行变换处理
    for j=1:n
        f=X2(i,j);
        g(i,j)=0;
        if(f>=0)&(f<=f1)
            g(i,j)=r1*f+b1;
        elseif(f>=f1)&(f<=f2)
            g(i,j)=r2*f+b2;
        elseif(f>=f2)&(f<=f3)
            g(i,j)=r3*f+b3;
        end;
    end;
 end;
 subplot(2,2,3),imshow(mat2gray(X2));
title('增强对比度后的图像');%(2)利用imadjust()函数增强对比度
 X1=imread('F:\MatlabShijueTupian/plane.gif');
 figure(2),subplot(1,2,1),imshow(X1),title('原图像');
 J=imadjust(X1,[0.25,0.6],[],1.2322)
 subplot(1,2,2),imshow(J),title('增强对比度后的图像');

增强对比度:

clear;
close all;
i=imread('F:\MatlabShijueTupian/5.jpg');
X1=rgb2gray(i);
%  X1=imread('F:\MatlabShijueTupian/flower.JPG');
 figure(1),subplot(2,2,2),imshow(X1),title('灰度图像');
 subplot(2,2,1),imshow(i),title('原图像');
 f0=0;g0=0;
 f1=70;g1=30;
 f2=180;g2=230;
 f3=255;g3=255; %绘制变换函数曲线
 subplot(2,2,3),plot([f0,f1,f2,f3],[g0,g1,g2,g3])
 axis tight,xlabel('f'),ylabel('g')
 title('增强对比度的变换曲线')
 r1=(g1-g0)/(f1-f0);
 b1=g0-r1*f0;
 r2=(g2-g1)/(f2-f1);
 b2=g1-r2*f1;
 r3=(g3-g2)/(f3-f2);
 b3=g2-r3*f2;
 [m,n]=size(X1);
 X2=double(X1);
 for i=1:m  %循环对矩阵中的每个元素进行变换处理
    for j=1:n
        f=X2(i,j);
        g(i,j)=0;
        if(f>=0)&(f<=f1)
            g(i,j)=r1*f+b1;
        elseif(f>=f1)&(f<=f2)
            g(i,j)=r2*f+b2;
        elseif(f>=f2)&(f<=f3)
            g(i,j)=r3*f+b3;
        end;
    end;
 end;
 subplot(2,2,4),imshow(mat2gray(X2));
title('增强对比度后的图像');%(2)利用imadjust()函数增强对比度
 i=imread('F:\MatlabShijueTupian/8.jpg');
 X1=rgb2gray(i);
 figure(2),subplot(1,3,2),imshow(X1),title('灰度图像');
 subplot(1,3,1),imshow(i),title('原图像');
 J=imadjust(X1,[0.25,0.6],[],1.2322)
 subplot(1,3,3),imshow(J),title('增强对比度后的图像');

直方图均衡化:

I=imread('F:\MatlabShijueTupian/peppers.bmp');
J=histeq(I);%直方图均衡化函数,n是均衡化后的灰度级数,缺省为64
figure(1),subplot(2,2,1),imshow(I) ,title('原图像'); 
subplot(2,2,2),imhist(I,64) ,title('原图像直方图'); 
subplot(2,2,3),imshow(J) ,title('直方图均衡化'); %变换后所得图像矩阵
subplot(2,2,4),imhist(J,64) ,
title('直方图均衡化后的直方图');

直方图均衡化:

clear;
close all;
i=imread('F:\MatlabShijueTupian/10.jpg');
X1=rgb2gray(i);
J=histeq(X1);%直方图均衡化函数,n是均衡化后的灰度级数,缺省为64
figure(1),subplot(2,2,1),imshow(X1) ,title('灰度图像'); 
subplot(2,2,2),imhist(X1,64) ,title('原图像直方图'); 
subplot(2,2,3),imshow(J) ,title('直方图均衡化'); %变换后所得图像矩阵
subplot(2,2,4),imhist(J,64) ,
title('直方图均衡化后的直方图');

直方图规定化:

I=imread('F:\MatlabShijueTupian/lena.gif')
hgram=0:255%hgram是由用户指定的向量,hgram的每一个元素都在[0,1]中
J=histeq(I,hgram);%直方图规定化函数,规定将原始图像的直方图近似变成hgram
figure(1),subplot(2,2,1),imshow(I),title('原图像'); 
subplot(2,2,2),imshow(J)
title('直方图规定化后图像');  subplot(2,2,3),imhist(I,64)
title('原图像直方图'); 
subplot(2,2,4),imhist(J,64)
title('直方图规定化后的直方图');

直方图规定化:

clear;
close all;
i=imread('F:\MatlabShijueTupian/6.jpg');
I=rgb2gray(i);
hgram=0:255%hgram是由用户指定的向量,hgram的每一个元素都在[0,1]中
J=histeq(I,hgram);%直方图规定化函数,规定将原始图像的直方图近似变成hgram
figure(1),subplot(2,2,1),imshow(I),title('灰度图像'); 
subplot(2,2,2),imshow(J)
title('直方图规定化后图像');  subplot(2,2,3),imhist(I,64)
title('原图像直方图'); 
subplot(2,2,4),imhist(J,64)
title('直方图规定化后的直方图');

均值滤波:

I=imread('F:\MatlabShijueTupian/peppers.bmp')
J=imnoise(I,'salt',0.02);
figure(11),subplot(2,2,1),imshow(I),title('原图像');
subplot(2,2,2),imshow(J),title('加入椒盐噪声的图像');
K1=filter2(fspecial('average',3),J)/255;%3*3均值滤波处理结果
K2=filter2(fspecial('average',5),J)/255;%5*5均值滤波处理结果
subplot(2,2,3),imshow(K1),
title('3*3均值滤波处理结果');
subplot(2,2,4),imshow(K2),
title('5*5均值滤波处理结果');

均值滤波:

clear;
close all;
i=imread('F:\MatlabShijueTupian/4.jpg');
I=rgb2gray(i);
J=imnoise(I,'salt',0.02);
figure(11),subplot(2,2,1),imshow(I),title('原图像');
subplot(2,2,2),imshow(J),title('加入椒盐噪声的图像');
K1=filter2(fspecial('average',3),J)/255;%3*3均值滤波处理结果
K2=filter2(fspecial('average',5),J)/255;%5*5均值滤波处理结果
subplot(2,2,3),imshow(K1),
title('3*3均值滤波处理结果');
subplot(2,2,4),imshow(K2),
title('5*5均值滤波处理结果');

高斯滤波:

I=imread('F:\MatlabShijueTupian/peppers.bmp');
 J=imnoise(I,'gaussian',0,0.005);
 h=fspecial('gaussian');
 K=filter2(h,J)/255;
 K1=wiener2(J,[5,5]);
 figure(12),subplot(2,2,1),imshow(I),title('原图像');
 subplot(2,2,2),imshow(J),title('加入高斯噪声的图像');
 subplot(2,2,3),imshow(K),title('高斯低通滤波的结果');
 subplot(2,2,4),imshow(K1),title('维纳滤波后的结果');

高斯滤波:

clear;
close all;
i=imread('F:\MatlabShijueTupian/4.jpg');
I=rgb2gray(i);
 J=imnoise(I,'gaussian',0,0.005);
 h=fspecial('gaussian');
 K=filter2(h,J)/255;
 K1=wiener2(J,[5,5]);
 figure(12),subplot(2,2,1),imshow(I),title('原图像');
 subplot(2,2,2),imshow(J),title('加入高斯噪声的图像');
 subplot(2,2,3),imshow(K),title('高斯低通滤波的结果');
 subplot(2,2,4),imshow(K1),title('维纳滤波后的结果');

中值滤波:

I=imread('F:\MatlabShijueTupian/peppers.bmp');
 J=imnoise(I,'salt',0.02);
 figure(13),subplot(2,2,1),imshow(I),title('原图像');
 subplot(2,2,2),imshow(J),title('加入椒盐噪声后的图像');
 K1=medfilt2(J,[3,3]);% 用模板实现中值过滤器
 subplot(2,2,3),imshow(K1),title('中值滤波后的结果');

中值滤波:

clear;
close all;
i=imread('F:\MatlabShijueTupian/4.jpg');
I=rgb2gray(i);
J=imnoise(I,'salt',0.02);
 figure(1),subplot(2,2,2),imshow(I),title('灰度图像');
 subplot(2,2,1),imshow(i),title('原图像');
 subplot(2,2,3),imshow(J),title('加入椒盐噪声后的图像');
 K1=medfilt2(J,[3,3]);% 用模板实现中值过滤器
 subplot(2,2,4),imshow(K1),title('中值滤波后的结果');

unsharp算子:

I=imread('F:\MatlabShijueTupian/lena.gif');
 h=fspecial('laplacian');
 I2=filter2(h,I);
 figure(14),subplot(2,2,1),imshow(I),title('原图像');
 subplot(2,2,2),imshow(I2),title('拉普拉斯算子滤波后的结果'); %采用'unsharp'算子实现对比度增强滤波器
 h=fspecial('unsharp',0.5);
 I3=filter2(h,I)/255;
subplot(2,2,3),imshow(I3),
title('unsharp算子实现对比度增强滤波后的结果');

unsharp算子:

clear;
close all;
i=imread('F:\MatlabShijueTupian/4.jpg');
I=rgb2gray(i);
 h=fspecial('laplacian');
 I2=filter2(h,I);
 figure(14),subplot(2,2,2),imshow(I),title('灰度图像');
  subplot(2,2,1),imshow(i),title('原图像');
 subplot(2,2,3),imshow(I2),title('拉普拉斯算子滤波后的结果'); %采用'unsharp'算子实现对比度增强滤波器
 h=fspecial('unsharp',0.5);
 I3=filter2(h,I)/255;
subplot(2,2,4),imshow(I3),
title('unsharp算子实现对比度增强滤波后的结果');

sobel滤波:

I1=imread('F:\MatlabShijueTupian/lena.gif');
 h1=fspecial('sobel');
 I2=filter2(h1,I1);%sobel卷积
 I3=conv2(I1,h1);
 h2=fspecial('prewitt');
 I4=filter2(h2,I1);
 h3=fspecial('log');
 I5=filter2(h3,I1);
 figure(15),
subplot(2,2,1),imshow(I1),title('原图像');
 subplot(2,2,2),imshow(I3);
title('sobel滤波');
subplot(2,2,3),imshow(I4);
title('prewitt滤波');
subplot(2,2,4),imshow(I5);
title('log滤波');

sobel滤波:

clear;
close all;
i=imread('F:\MatlabShijueTupian/4.jpg');
I1=rgb2gray(i);
 h1=fspecial('sobel');
 I2=filter2(h1,I1);%sobel卷积
 I3=conv2(I1,h1);
 h2=fspecial('prewitt');
 I4=filter2(h2,I1);
 h3=fspecial('log');
 I5=filter2(h3,I1);
 figure(15),
subplot(2,2,1),imshow(I1),title('灰度图像');
 subplot(2,2,2),imshow(I3);
title('sobel滤波');
subplot(2,2,3),imshow(I4);
title('prewitt滤波');
subplot(2,2,4),imshow(I5);
title('log滤波');

Butterworth滤波:

I1=imread('F:\MatlabShijueTupian/lena2.gif');
 I2=imnoise(I1,'salt');
 f=double(I2);
 g=fft2(f);     %采用傅立叶变换
 g=fftshift(g); %数据矩阵平移
 [N1,N2]=size(g);
 n=2;
 d0=50;
 d1=5;
 n1=fix(N1/2);
 n2=fix(N2/2);
 for i=1:N1
     for j=1:N2
         d=sqrt((i-n1)^2+(j-n2)^2);
         %计算Butterworth低通变换函数
         h=1/(1+0.414*(d/d0)^(2*n));
         result(i,j)=h*g(i,j);
     end
 end
 result=ifftshift(result);
 X2=ifft2(result);
 X3=uint8(real(X2));
 if d==0
            h=0;
          else
            h=1/(1+(d1/d)^(2*n));
          end
            result(i,j)=h*g(i,j);
 result=ifftshift(result);
 X4=ifft2(result);
 X5=uint8(real(X2));
 figure(16),subplot(2,2,1),imshow(I1),title('原图像');
 subplot(2,2,2),imshow(I2),title('加噪图像');
subplot(2,2,3),imshow(X3),title('Butterworth低通滤波器去噪图像');
subplot(2,2,4),imshow(X5),title('Butterworth高通滤波器去噪图像');

Butterworth滤波:

clear;
close all;
i=imread('F:\MatlabShijueTupian/4.jpg');
I1=rgb2gray(i);
 subplot(2,2,1),imshow(i),title('原图像');
 I2=imnoise(I1,'salt');
 f=double(I2);
 g=fft2(f);     %采用傅立叶变换
 g=fftshift(g); %数据矩阵平移
 [N1,N2]=size(g);
 n=2;
 d0=50;
 d1=5;
 n1=fix(N1/2);
 n2=fix(N2/2);
 for i=1:N1
     for j=1:N2
         d=sqrt((i-n1)^2+(j-n2)^2);
         %计算Butterworth低通变换函数
         h=1/(1+0.414*(d/d0)^(2*n));
         result(i,j)=h*g(i,j);
     end
 end
 result=ifftshift(result);
 X2=ifft2(result);
 X3=uint8(real(X2));
 if d==0
            h=0;
          else
            h=1/(1+(d1/d)^(2*n));
          end
            result(i,j)=h*g(i,j);
 result=ifftshift(result);
 X4=ifft2(result);
 X5=uint8(real(X2));
 figure(1),subplot(2,2,2),imshow(I1),title('灰度图像');
subplot(2,2,3),imshow(X3),title('Butterworth低通滤波器去噪图像');
subplot(2,2,4),imshow(X5),title('Butterworth高通滤波器去噪图像');

本人能力有限,解释尚不清楚明了,如遇任何问题,大家可留言或私信。因使用私人图片进行实验,结果图不便上传,望理解。后续将程序文件打包上传,供大家学习使用。

本文希望对大家有帮助,当然上文若有不妥之处,欢迎指正。

分享决定高度,学习拉开差距