%任务:打开一幅过度曝光图像,拉伸其图像,观察图像变换,对图像直方图均衡算法;
%本作业使用MATLAB2021A完成
clear;
clc;
close all
f=imread("4.jpeg");
g=rgb2gray(f);
%显示原图像
figure(1)
subplot(1,2,1)
imshow(g)
title('原图像');
%显示图像的灰度分布
subplot(1,2,2)
imhist(g)
title('原图像灰度分布');
显示一张过曝的图像
%利用imadjust扩大图像动态范围
figure(2)
g2=imadjust(g,[0.5,1],[]);
subplot(1,2,1)
imshow(g2)
title('imadjust调整');
subplot(1,2,2)
imhist(g2)
title('imadjust调整灰度分布');
%手动调整均衡
row=size(g,1);
col=size(g,2);
nump=zeros(1,256);
for i=1:row
for j=1:col
r=single(g(i,j)); %uint8 r的最大值为255
nump(r+1)=nump(r+1)+1;
end
end
figure(3)
subplot(2,2,1);
bar(0:255,nump);
title('灰度出现次数');
p=nump/(row*col);
subplot(2,2,2);
bar(0:255,p)
title('灰度出现频率');
s=zeros(1,256);
s(1)=p(1);
for i=1:255
s(i+1)=s(i)+p(i+1); %第i个数据代表的灰度值为i-1
end
D_S=floor(double(max(max(g))-min(min(g)))*s+0.5);
subplot(2,2,3)
bar(0:255,D_S);
title('均衡后直方图')
out = uint8(zeros(row, col));
for i = 1 : row
for j = 1 : col
out(i,j) = D_S(g(i,j));
end
end
subplot(2,2,4)
imshow(out);
如图可以看出,均衡后的灰度直方图分布更加分散,均匀
%利用histeq使图像均衡
figure(4)
subplot(1,2,1)
g3=histeq(g);
imshow(g3)
title('histeq均衡调整');
subplot(1,2,2)
imhist(g3)
title('histeq均衡调整灰度分布');
%比较原图和均衡后的图像
figure(5)
imshowpair(g,out , 'montage');
title('比较原图和均衡后的图像')
如图可以看出。均衡后的图片相比于原图片,房子的边缘有了一定的还原,人的轮廓也有一定的还原
%比较手写的算法和matlab自带的算法
figure(6)
imshowpair(out,g3, 'montage');
title('比较手写的算法和matlab自带的算法(左为手写,右为自带')
如图可以看出,matlab自带的算法会使得天空出现大量可见的噪点,并且房子的还原也十分的粗糙,但利用自写的算法,早点噪点的出现得以缓解