直方图均衡化
clear;
clc;
img = imread('lena.jpg');
[a,b] = imhist(img);
[M,N] = size(img);
new_img = zeros(M,N);
stem(b,a/(M*N));
% 下面开始统计概率密度分布
t(1) = a(1);
for i=2:256
t(i) = a(i)+t(i-1);
end
for m=1:M
for n=1:N
new_img(m,n) = t(img(m,n)+1)/(M*N)*255;
end
end
figure,imhist(uint8(new_img))
figure,imshow(uint8(new_img))
figure,imshow(img)
直方图规范化,即直方图匹配
clear;
img = imread('lena.jpg');
match_img = imread('rice.png');
[M,N] = size(img);
img_hist = imhist(img);
match_img_hist = imhist(match_img);
figure,stem(img_hist)
title('原始图像的直方图')
figure,stem(match_img_hist);
title('匹配的直方图')
% 下面计算每幅图像的累积概率密度分布
img_cal = cumsum(img_hist)/numel(img);
match_img_cal = cumsum(match_img_hist)/numel(match_img);
new_img = ones(M,N);
% 计算匹配结果
t=zeros(1,256);
for i=1:256
[tmp,index] = min(abs(img_cal(i)-match_img_cal))
t(i) = index-1;
end
% 遍历图像,计算每个像素对应的匹配像素值
for j=1:M
for k=1:N
new_img(j,k) = t(img(j,k)+1);
end
end
new_img_hist = imhist(uint8(new_img));
figure,stem(new_img_hist);
title('新图像的直方图')
代码是看了很多别人的博客,又加入了一些自己的想法写出来的,主要是为了数字图像处理的考试
写写自己的感受吧:图像直方图的均衡化是将原始图像的灰度进行了拉伸,而直方图匹配,这个“匹配”两个字用的实在是太准确了,至于中间用到的求累计概率密度分布,细想一下还是很有必要的操作