直方圖均衡化

直方圖均衡化Histogram Equalization

理論Theory

圖像的直方圖是什么What is an Image Histogram?

  • 直方圖是圖像中像素強(qiáng)度分布的圖形表達(dá)方式It is a graphical representation of the intensity distribution of an image。
  • 它統(tǒng)計(jì)了每一個(gè)強(qiáng)度值所具有的像素個(gè)數(shù)It quantifies the number of pixels for each intensity value considered。


    圖示

    如上圖的灰度圖,右圖統(tǒng)計(jì)顯示了左圖中所有像素值的分布。

直方圖均衡化是什么?

  • 直方圖均衡化是通過(guò)拉伸像素強(qiáng)度分布范圍來(lái)增強(qiáng)圖像對(duì)比度的一種方法It is a method that improves the contrast in an image, in order to stretch out the intensity range。
  • 說(shuō)得更清楚一些, 以上面的直方圖為例, 你可以看到像素主要集中在中間的一些強(qiáng)度值上. 直方圖均衡化要做的就是 拉伸 這個(gè)范圍. 見(jiàn)下面左圖: 綠圈圈出了 少有像素分布其上的 強(qiáng)度值. 對(duì)其應(yīng)用均衡化后, 得到了中間圖所示的直方圖. 均衡化的圖像見(jiàn)下面右圖To make it clearer, from the image above, you can see that the pixels seem clustered around the middle of the available range of intensities. What Histogram Equalization does is to stretch out this range. Take a look at the figure below: The green circles indicate the underpopulated intensities. After applying the equalization, we get an histogram like the figure in the center. The resulting image is shown in the picture at right。
    圖示

直方圖均衡化是怎樣做到的?How does it work?

  • 均衡化指的是把一個(gè)分布 (給定的直方圖) 映射 到另一個(gè)分布 (一個(gè)更寬更統(tǒng)一的強(qiáng)度值分布), 所以強(qiáng)度值分布會(huì)在整個(gè)范圍內(nèi)展開(kāi)Equalization implies mapping one distribution (the given histogram) to another distribution (a wider and more uniform distribution of intensity values) so the intensity values are spreaded over the whole range。
  • 要想實(shí)現(xiàn)均衡化的效果, 映射函數(shù)應(yīng)該是一個(gè) 累積分布函數(shù) (cdf) (更多細(xì)節(jié), 參考學(xué)習(xí)OpenCV). 對(duì)于直方圖H(i) , 它的 累積分布 H'(i)是:To accomplish the equalization effect, the remapping should be the cumulative distribution function (cdf) (more details, refer to Learning OpenCV). For the histogram H(i), its cumulative distribution H′(i) is:
    圖示

理解

直方圖均衡化的作用是圖像增強(qiáng)。其過(guò)程是將圖像的像素分布通過(guò)一種方法映射到另外一種分布上去,在該映射過(guò)程中主要使用了累積分布函數(shù)。累積分布函數(shù)用于描述隨機(jī)變量的概率分布(F(x)=P(X<x)),該函數(shù)是遞增的,并且值域分布范圍是0-1,假如我們要處理的圖像是灰度圖,它的像素值分布在0-255,可以與0-1對(duì)應(yīng)起來(lái)。因此使用累積分布函數(shù)按照某種方法來(lái)映射可以保證原來(lái)的大小關(guān)系不變,較亮的區(qū)域,依舊是較亮的,較暗依舊暗,只是對(duì)比度增大,絕對(duì)不會(huì)明暗顛倒。
先來(lái)看一個(gè)實(shí)例。
假設(shè)有如下圖像:



得圖像的統(tǒng)計(jì)信息如下圖所示,并根據(jù)統(tǒng)計(jì)信息完成灰度值映射:


映射后的圖像如下所示:


由上述實(shí)例我們可以看到,將像素值轉(zhuǎn)換為另一個(gè)像素值,并且原來(lái)的大小關(guān)系并沒(méi)有改變,但是直畫(huà)出方圖的話,會(huì)發(fā)現(xiàn)直方圖變緩了。

例程Code

  • 這個(gè)例程是用來(lái)干嘛的?What does this program do?
    加載源圖像Loads an image
    把源圖像轉(zhuǎn)為灰度圖Convert the original image to grayscale
    使用OpenCV函數(shù) EqualizeHist 對(duì)直方圖均衡化Equalize the Histogram by using the OpenCV function cv::equalizeHist
    在窗體中顯示源圖像和均衡化后圖像Display the source and equalized images in a window.
  • 例程一瞥Code at glance:
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace cv;
using namespace std;
int main( int, char** argv )
{
    Mat src, dst;
    const char* source_window = "Source image";
    const char* equalized_window = "Equalized Image";
    src = imread( argv[1], IMREAD_COLOR );
    if( src.empty() )
    { 
        cout<<"Usage: ./EqualizeHist_Demo <path_to_image>"<<endl;
        return -1;
    }
    cvtColor( src, src, COLOR_BGR2GRAY );
    equalizeHist( src, dst );
    namedWindow( source_window, WINDOW_AUTOSIZE );
    namedWindow( equalized_window, WINDOW_AUTOSIZE );
    imshow( source_window, src );
    imshow( equalized_window, dst );
    waitKey(0);
    return 0;
}

說(shuō)明

1、聲明原圖和目標(biāo)圖以及窗體名稱:Declare the source and destination images as well as the windows names:

Mat src, dst;
char* source_window = "Source image";
char* equalized_window = "Equalized Image";

2、加載源圖像:Load the source image:

src = imread( argv[1], 1 );
if( !src.data )
{ 
    cout<<"Usage: ./Histogram_Demo <path_to_image>"<<endl;
    return -1;
}

3、轉(zhuǎn)為灰度圖:Convert it to grayscale:

cvtColor( src, src, CV_BGR2GRAY );

4、利用函數(shù) equalizeHist 對(duì)上面灰度圖做直方圖均衡化:Apply histogram equalization with the function cv::equalizeHist :

equalizeHist( src, dst );

可以看到, 這個(gè)操作的參數(shù)只有源圖像和目標(biāo) (均衡化后) 圖像.As it can be easily seen, the only arguments are the original image and the output (equalized) image。

5、顯示這兩個(gè)圖像 (源圖像和均衡化后圖像) :Display both images (original and equalized) :

namedWindow( source_window, CV_WINDOW_AUTOSIZE );
namedWindow( equalized_window, CV_WINDOW_AUTOSIZE );
imshow( source_window, src );
imshow( equalized_window, dst );

6、等待用戶案件退出程序Wait until user exists the program

waitKey(0);
return 0;

結(jié)果

1、為了更好地觀察直方圖均衡化的效果, 我們使用一張對(duì)比度不強(qiáng)的圖片作為源圖像輸入, 如下圖:To appreciate better the results of equalization, let's introduce an image with not much contrast, such as:

它的直方圖為:which, by the way, has this histogram:

注意到像素大多集中在直方圖中間的強(qiáng)度上.notice that the pixels are clustered around the center of the histogram.

2、使用例程進(jìn)行均衡化后, 我們得到下面的結(jié)果After applying the equalization with our program, we get this result:

這幅圖片顯然對(duì)比度更強(qiáng). 再驗(yàn)證一下均衡化后圖片的直方圖this image has certainly more contrast. Check out its new histogram like this:


注意到現(xiàn)在像素在整個(gè)強(qiáng)度范圍內(nèi)均衡分布Notice how the number of pixels is more distributed through the intensity range.

翻譯者

zhlifly@ OpenCV中文網(wǎng)站 <zhlifly@gmail.com>

參考

直方圖均衡化的數(shù)學(xué)原理

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 圖像直方圖(英語(yǔ):Image Histogram)是用以表示數(shù)字圖像中亮度分布的直方圖,標(biāo)繪了圖像中每個(gè)亮度值的像...
    fengzhizi715閱讀 10,305評(píng)論 1 6
  • about 本文參考了 sunny2038同學(xué)的OpenCV Python教程(1、圖像的載入、顯示和保存) jn...
    龐貝船長(zhǎng)閱讀 22,296評(píng)論 0 5
  • 之前在用LBP做單樣本人臉識(shí)別的時(shí)候,對(duì)于光照角度太大或者光線太暗的情況并不是很好,出錯(cuò)的概率還是很大,老師推薦用...
    Mikito_k閱讀 891評(píng)論 0 1
  • 概念:通過(guò)拉伸像素強(qiáng)度分布范圍來(lái)增強(qiáng)圖像對(duì)比度的一種方法,如下圖,將左邊直方圖的中間的一些強(qiáng)度值拉伸,對(duì)綠色橢圓部...
    凍凍妖閱讀 767評(píng)論 0 0
  • 先來(lái)看看直方圖均衡化的效果圖。 直觀感受,均衡化后的圖像明暗對(duì)比更明顯。亮的地方更亮,暗的地方更暗,拉開(kāi)了差距。1...
    ck2016閱讀 10,365評(píng)論 0 4

友情鏈接更多精彩內(nèi)容