一、实验目的与要求
1.掌握图像空域点处理增强方法,包括图像求反,线性灰度变换,以及直方图均衡化方法;
2.熟练掌握空域滤波增强方法,包括平滑滤波器及锐化滤波器。
二、实验内容及步骤
1.图像的求反、线性灰度变换、直方图均衡化:
(1)使用imread()函数读取一副图像,名为lena.bmp;
(2)实现图像求反运算(t=L−1−s);

clc;clear;close all;
img1 = imread("lena.bmp");
img2 = imcomplement(img1);
subplot(1,2,1);imshow(img1);title('原始图像');
subplot(1,2,2);imshow(img2);title('求反图像');
线性灰度变换(小于30的灰度值不变,将30~150的灰度值拉伸到30~200,同时压缩150~255的灰度值到200与255之间);

clc;clear;close all;
img1 = imread("lena.bmp");
[M,N]=size(img1);
figure;
subplot(1,2,1);imshow(img1);title('原始图像');
for i=1:M
for j=1:N
if img1(i,j)<30
img1(i,j)=img1(i,j);
elseif img1(i,j)<150
img1(i,j)=(200-30)/(150-30)*(img1(i,j)-30)+30;
else
img1(i,j)=(255-200)/(255-150)*(img1(i,j)-150)+200;
end
end
end
subplot(1,2,2);imshow(img1);title('线性灰度变换图像');
实现直方图均衡化(imhist( ) :显示图像直方图; histeq( ):实现对输入图像的直方图均衡化)


clc;clear;close all;
img1 = imread("lena.bmp");
img2 = histeq(img1);
figure;
subplot(1,2,1);imshow(img1);title('原始图像');
subplot(1,2,2);imshow(img2);title('均衡化后图像');
figure;
subplot(1,2,1);imhist(img1);title('图像直方图');
subplot(1,2,2);imhist(img2);title('直方图均衡化');
2. 平滑滤波器:
(1)使用imread()函数读入图像lena.bmp;
(2)利用imnoise 命令在图像lena.bmp上添加椒盐噪声;

clc;clear;close all;
img1 = imread("lena.bmp");
subplot(1,2,1),imshow(img1),title('原始图像');
img2=imnoise(img1,'salt & pepper',0.02);
subplot(1,2,2),imshow(img2),title('加入噪声密度:0.02的椒盐噪声');
进行3*3模板平滑滤波以及5*5模板平滑滤波;

clc;clear;close all;
img1 = imread("lena.bmp");
img2=imnoise(img1,'salt & pepper',0.02);
img3=filter2(fspecial('average',3),img2)/255;
img4=filter2(fspecial('average',5),img2)/255;
subplot(1,3,1),imshow(img2),title('0.02的椒盐噪声图像');
subplot(1,3,2),imshow(img3),title('3*3模板平滑滤波');
subplot(1,3,3),imshow(img4),title('5*5模板平滑滤波');
3. 锐化滤波器:
(1)使用imread()函数读入图像lena.bmp;
(2)采用3×3的拉普拉斯算子w = [ 1, 1, 1; 1 – 8 1; 1, 1, 1]滤波;
(3)联合使用figure, subplot(), imshow()函数显示原始图像以及锐化滤波的效果图。

clc;clear;close all;
img1 = imread("lena.bmp");
F = im2double(img1);
[m,n]=size(F);
G=zeros(m,n);
w=[ 1, 1, 1; 1 -8 1; 1, 1, 1];
for x=1:m
for y=1:n
if (x==1||y==1||x==m||y==n)
G(x,y)=F(x,y);
else
G(x,y)=w(1,1)*F(x-1,y-1)+w(1,2)*F(x-1,y)+w(1,3)*F(x-1,y+1)...
+w(2,1)*F(x,y-1)+w(2,2)*F(x,y)+w(2,3)*F(x,y+1)...
+w(3,1)*F(x+1,y+1)+w(3,2)*F(x+1,y)+w(3,3)*F(x+1,y+1);
end
end
end
F=im2uint8(F);
G=im2uint8(G);
figure
subplot(121)
imshow(F)
title('原始图像');
subplot(122)
imshow(G);
title('拉普拉斯算子处理后的图像');