如何在 MATLAB 中求解直方图均衡数值问题?
直方图均衡是一种扩大直方图动态范围的数学技术。有时直方图跨越一个短范围,通过均衡化,直方图的跨度被加宽。在数字图像处理中,使用这种技术可以增强图像的对比度。
直方图均衡的使用:
它用于增加直方图的传播。如果直方图代表数字图像,那么通过将强度值分布在一个大的动态范围内,我们可以提高图像的对比度。
算法:
- 找出在直方图的水平轴上表示的每个值的频率,即图像中的强度。
- 计算每个强度值的概率密度函数。
- 找到 PDF 后,计算每个强度频率的累积密度函数。
- CDF 值在 0-1 范围内,因此我们将所有 CDF 值乘以强度的最大值,即 255。
- 将最终值四舍五入为整数值。
例子:
Matlab
% MATLAB code for Histogram equalisation
% function to return resultant
% matrix and summary
function [f,r]=HistEq(k,max)
Freq=zeros(1,max);
[x,y]=size(k);
% Calculate frequency of each
% intensity value.
for i=1:x
for j=1:y
Freq(k(i,j)+1)=Freq(k(i,j)+1)+1;
end
end
% Calculate PDF for each intensity value.
PDF=zeros(1,max);
Total=x*y;
for i=1:max
PDF(i)=Freq(i)/Total;
end
% Calculate the CDF for each intensity value.
CDF=zeros(1,max);
CDF(1)=PDF(1);
for i=2:max
CDF(i)=CDF(i-1)+PDF(i);
end
% Multiply by Maximum intensity value
% and round off the result.
Result=zeros(1,max);
for i=1:max
Result(i)=uint8(CDF(i)*(max-1));
end
% Compute the Equalized image/matrix.
mat=zeros(size(k));
for i=1:x
for j=1:y
mat(i,j)=Result(k(i,j)+1);
end
end
f=mat;
r=Result;
end
% Utility code here.
k=[0, 1, 1, 3, 4;
7, 2, 5, 5, 7;
6, 3, 2, 1, 1;
1, 4, 4, 2, 1];
[new_matrix, summary]=HistEq(k,8);
输出:
下面显示了一个 4×5 大小的 3 位图像。计算直方图均衡图像。0 1 1 3 4 7 2 5 5 7 6 3 2 1 1 1 4 4 2 1
脚步:
- 找到强度值的范围。
- 找出每个强度值的频率。
- 计算每个频率的概率密度函数。
- 计算每个频率的累积密度函数。
- 将 CDF 与可能的最高强度值相乘。
- 将步骤 5 中获得的值四舍五入。
Overview of calculation:
Range of intensity values = [0, 1, 2, 3, 4, 5, 6, 7]
Frequency of values = [1, 6, 3, 2, 3, 2, 1, 2]
total = 20 = 4*5
Calculate PDF = frequency of each intensity/Total sum of
all frequencies, for each i value of intensity
Calculate CDF =cumulative frequency of each intensity
value = sum of all PDF value (<=i)
Multiply CDF with 7.
Round off the final value of intensity.
此处给出了计算的表格形式: Range Frequency PDF CDF 7*CDF Round-off 0 1 0.0500 0.0500 0.3500 0 1 6 0.3000 0.3500 2.4500 2 2 3 0.1500 0.5000 3.5000 4 3 2 0.1000 0.6000 4.2000 4 4 3 0.1500 0.7500 5.2500 5 5 2 0.1000 0.8500 5.9500 6 6 1 0.0500 0.9000 6.3000 6 7 2 0.1000 1.0000 7.0000 7
Interpretation:
The pixel intensity in the image has modified.
0 intensity is replaced by 0.
1 intensity is replaced by 2.
2 intensity is replaced by 4.
3 intensity is replaced by 4.
4 intensity is replaced by 5.
5 intensity is replaced by 6.
6 intensity is replaced by 6.
7 intensity is replaced by 7.
输出:新图像如下:
0 2 2 4 5 7 4 6 6 7 6 4 4 2 2 2 5 5 4 2