(1)利用histeq()函数自动进行图像均衡化
histeq(I,n)
I:输入图像
n:表示图像的灰度级数目,是一个可选参数,默认值64
>> a = imread('D:\MatlabWorkspce\test-images\Lena512.png');
>> c = histeq(a);
>> figure,subplot(2,2,1),imshow(a);
>> subplot(2,2,2),imshow(c);
>> subplot(2,2,3),imhist(a);
subplot(2,2,4),imhist(c)
(2)自行编程实现直方图均衡化过程
clear all;
close all;
a=imread('2.jpg');
b=a;
[m,n]=size(b);
%进行像素灰度统计;
h=zeros(1,256);%统计各灰度数目,共256个灰度级
for i=1:m
for j=1:n
h(b(i,j) + 1) = h(b(i,j) + 1) + 1;%对应灰度值像素点数量增加一
end
end
%计算灰度分布密度
hs=zeros(1,256);
for i=1:256
hs(i)=h(i)/(m*n);
end
%计算累计直方图分布
hp=zeros(1,256);
for i=1:256
for j=1:i
hp(i)=hs(j)+hp(i);
end
end
%累计分布取整
g=zeros(1,256);
for i=1:256
g(i)= uint8(255*hp(i));
end
%对灰度值进行映射(均衡化)
for i=1:m
for j=1:n
c(i,j)=g(b(i,j));
end
end
figure,subplot(2,2,1),imshow(b);
subplot(2,2,2),imshow(c,[]);
subplot(2,2,3),imhist(b);
subplot(2,2,4),imhist(uint8(c));
I = imread('D:\MatlabWorkspce\test-images\Lena512.png');
>> hgram = 50:2:250;
>> subplot(1,2,1);
>> J=histeq(I,hgram);
>> imshow(J);
>> subplot(1,2,2);
>> imhist(J,64);
>>