数字图像基础---直方图修正

图像的直方图修正方法主要有直方图均衡化和直方图规定化
直方图修正的目的是,使修正后的图像的灰度间距拉开或者是图像灰度分布均匀,从而增大反差,使图像细节清晰,从而达到图像增强的目的。

直方图均衡化---自动增强整幅图像的对比度

从理论上来说,直方图均衡化就是通过变换函数将原来的直方图调整为具有平坦倾向的直方图,然后用均衡直方图校正图像。

(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));

直方图规定化---有选择的增强某个灰度值范围内的对比度

关键就是灰度映像函数
(1)histeq()函数实现
g = histeq(I,hgram)
I:输入图像
hgram:一个由指定值构成的行向量,是输出图像的直方图具有Length(hgram)个灰度级
g:输出的图像
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);
>> 


猜你喜欢

转载自blog.csdn.net/u011296723/article/details/59696987