[OpenCV Getting Started Guide] Article 9 grayscale histogram equalization

Source: Internet
Author: User

In the previous article "OpenCV eighth grayscale histogram", we introduced the grayscale histogram. This article describes the histogram equalization, which is a common method for image enhancement. The Mathematical Principle of histogram equalization is not introduced here. If you are interested, you can refer to professional books. Let's take a look at the function of grayscale histogram equalization-cvEqualizeHist

 

I. cvEqualizeHist

Function: histogram equalization. This function can normalize the brightness and contrast of an image.

Function prototype:

/* Equalizes histogram of 8-bit single-channel image */

CVAPI (void) cvEqualizeHist (const CvArr * src, CvArr * dst );

The first parameter indicates the input image, which must be a grayscale image (8-bit, single-channel image ).

The second parameter indicates the output image.

Function Description:

This function uses the following rule to perform histogram equalization for the input image:

1: Calculate the histogram H of the input image.

2: histogram normalization. Therefore, the sum of straight squares is 255.

3: Calculate the histogram integral, H' (I) = Sum (H (j) (0 <= j <= I ).

4: Use H' as the query table: dst (x, y) = H' (src (x, y) for image transformation.

 

On Wikipedia, there is a good comparison of grayscale histogram equalization, see (URL: http://zh.wikipedia.org/zh-cn/%E7%9B%B4%E6%96%B9%E5%9B%BE%E5%9D%87%E8%A1%A1%E5%8C%96)

It can be seen that the histogram equalization has a very good effect on Image Enhancement and can highlight the image details. Next we will write a program for Grayscale histogram equalization by ourselves. The Code is as follows:

// Grayscale histogram equalization of images // By MoreWindows (http://blog.csdn.net/MoreWindows) # include <opencv2/opencv. hpp> # include <opencv2/legacy/compat. hpp> using namespace std; # pragma comment (linker, "/subsystem: \" windows \ "/entry: \" mainCRTStartup \ "") void FillWhite (IplImage * pImage) {cvRectangle (pImage, cvPoint (0, 0), cvPoint (pImage-> width, pImage-> height), CV_RGB (255,255,255), CV_FILLED );} // create the histogram CvHistogram * CreateGr Of The grayscale image AyImageHist (IplImage ** ppImage) {int nHistSize = 256; float fRange [] = {0,255}; // float * pfRanges [] = {fRange }; cvHistogram * pcvHistogram = cvCreateHist (1, & nHistSize, CV_HIST_ARRAY, pfRanges); cvCalcHist (ppImage, pcvHistogram); return pcvHistogram ;} // create a histogram image IplImage * CreateHisogramImage (int nImageWidth, int nScale, int nImageHeight, CvHistogram * pcvHistogram) {IplImage * pHistImage = cvCr EateImage (cvSize (nImageWidth * nScale, nImageHeight), histogram, 1); FillWhite (pHistImage); // maximum straight square float fMaxHistValue = 0 in the histogram; cvGetMinMaxHistValue (pcvHistogram, NULL, & fMaxHistValue, NULL, NULL); // draw the values of each straight square to the int I; for (I = 0; I <nImageWidth; I ++) {float fHistValue = cvQueryHistValue_1D (pcvHistogram, I); // int nRealHeight = cvRound (fHistValue/fMaxHistValue) * nImageHeight) in pixels of I; // you must Drawn height cvRectangle (pHistImage, cvPoint (I * nScale, nImageHeight-1), cvPoint (I + 1) * nScale-1, nImageHeight-nRealHeight), cvScalar (I, 0, 0, 0), CV_FILLED);} return pHistImage;} int main (int argc, char ** argv) {const char * pstrWindowsSrcTitle = "source image (http://blog.csdn.net/MoreWindows )"; const char * pstrWindowsGrayTitle = "grayscale (http://blog.csdn.net/MoreWindows)"; const char * pstrWindowsHistTitle = "histogram (Http://blog.csdn.net/MoreWindows) "; const char * pstrWindowsGrayEqualizeTitle =" grayscale-after equalization (http://blog.csdn.net/MoreWindows) "; const char * pstrWindowsHistEqualizeTitle =" histogram-after equalization (http://blog.csdn.net/MoreWindows )"; // load the source image from the file: IplImage * pSrcImage = cvLoadImage ("013.jpg", watermark); IplImage * pGrayImage = cvCreateImage (cvGetSize (pSrcImage), watermark, 1); IplImage * pGrayEqualizeImage VCreateImage (cvGetSize (pSrcImage), watermark, 1); // grayscale cvCvtColor (pSrcImage, pGrayImage, CV_BGR2GRAY); // histogram image data int nHistImageWidth = 255; int nHistImageHeight = 150; int nScale = 2; // grayscale histogram and histogram image CvHistogram * pcvHistogram = watermark (& pGrayImage); IplImage * pHistImage = CreateHisogramImage (nHistImageWidth, nScale, nHistImageHeight, pcvHistogram ); // balance cvEqualizeHist (pGrayImage, pGrayEq UalizeImage); // grayscale histogram and histogram image after equalization CvHistogram * watermark = CreateGrayImageHist (& pGrayEqualizeImage); IplImage * watermark = CreateHisogramImage (nHistImageWidth, nScale, nHistImageHeight, watermark ); // display code .... CvWaitKey (0); // reclaim resource code... Return 0 ;}

The running result is as follows:

It can be seen that the grayscale histogram equalization significantly enhances the grayscale image. Readers can try to add the grayscale histogram equalization to the sixth section of OpenCV contour detection, see how to improve the contour detection after equalization.

 

 

Because the human eyes are more sensitive to color, the next section "OpenCV 11th color histogram equalization" will perform histogram equalization for color images, so that you can have a more intuitive understanding of histogram equalization.

 

Articles in the OpenCV Getting Started Guide series:

Http://blog.csdn.net/morewindows/article/category/1291764

Reprinted please indicate the source, original address: http://blog.csdn.net/morewindows/article/details/8364690

Welcome to Weibo: http://weibo.com/MoreWindows

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.