Histogram equalization of OpenCV

Source: Internet
Author: User
This article code uses the OPENCV version: 2.4.13 This code is tested under Win10+visual Studio Update 3

Two blogs, "OpenCV's histogram stretching" and "OpenCV look-up table and histogram stretching", describe the histogram of stretched images to enhance contrast. However, in most cases, the visual defect of an image does not originate in the use of a narrow intensity range, but rather because some color values occur more frequently than others. In fact, we can assume that a high-quality image should average all the pixel strength, which is the idea behind the histogram equalization (histogram equalization), even if the histogram of the image is as flat as possible. Can be represented by the following image:

The method of image contrast enhancement can be divided into two kinds, one is direct contrast enhancement method and the other is indirect contrast enhancement method. Histogram stretching and histogram equalization are two of the most common indirect contrast enhancement methods. OPENCV Implementation

The OPENCV provides an interface for histogram equalization:

void Equalizehist (Inputarray src, outputarray DST);

Here's how to use it:

///OPENCV Implement histogram equalization example
#include "opencv2/core/core.hpp"
#include " Opencv2/highgui/highgui.hpp "
#include" opencv2/imgproc/imgproc.hpp "

int main ()
{  
    //read in the image, The RGB image of the 3 channel is now
    cv::mat image = Cv::imread ("g:/dataset/lena512.bmp");
    if (Image.empty ())
    {
        return-1;
    }

    Convert to single-channel grayscale image
    Cv::mat grayimage;
    Cv::cvtcolor (image, Grayimage, cv::color_bgr2gray);

    Histogram equalization of
    Cv::mat Resultimage;
    Cv::equalizehist (Grayimage, resultimage);

    Displays the original image and the histogram-balanced image
    Cv::imshow ("Original image", grayimage);
    Cv::imshow ("Result image", resultimage);

    Cv::waitkey ();
    return 0;
}

The original image and its histogram are as follows:


The histogram-balanced image and its histogram are as follows:


From the image, the contrast is really enhanced, and from the histogram it does use more gray levels and is relatively flat. It should be noted that histogram equalization can only get a relatively flat histogram due to the discretization of gray scale. Mathematical Principles

Histogram equalization is actually a remapping of grayscale, mapping the original gray value to a new grayscale value. To get this mapping function, you must start with the continuous grayscale. Continuous Grayscale

Considering the continuous grayscale value, R represents the grayscale of the original image, and its value range is [0, L-1], where 0 is black and white, and L-1 is the color. Using S represents the grayscale of an image that has been balanced by a histogram, that is, when r is mapped or transformed in grayscale, there is a transform function:

S=t (R), 0≤r≤l−1 s = T (R), 0 \le R \le L-1

Consider this transformation. Since we end up using all shades of gray, the value of the transformed range should be [0, L-1]. At the same time, in order to ensure that the transformation does not produce gray-scale inversion, the larger gray-scale r must be mapped to a larger gray-level z, that is, the transformation T (r) must be monotonically increasing.

These two conditions are restated below:

(a) T (R) is a monotonically increasing function on the interval [0, L-1]; if the inverse function of T (r) is needed, it is required to be a strictly monotonically incrementing function.

(b) When 0≤r≤l−1 0 \le R \le L-1, 0≤t (R) ≤l−1 0 \le T (r) \le L-1. Continuous random variables

In order to get the function relation further, we consider the gray level of an image as a random variable within the interval [0, L-1]. The basic descriptor describing the stochastic transformation is its probability density function (probability Density function, PDF). Make the PR (r) P_r (R) and PS (s) p_s (s) represent the PDF of the random variables R and s respectively. Knowledge of the basic probability theory can be known that if the PR (r) P_r (R) and T (R) are known, and the T (r) is continuous and micro in the area of interest, then a PDF of the transformed variable s can be obtained by the following simple formula:

PS (s) =pr (R) |drds|=pr (r) |drdt (r) | p_s (s) = P_r (r) |\frac{dr}{ds}| = P_r (R) |\frac{dr}{dt (r)}|

Since the histogram to be balanced is evenly distributed, its PDF is known, i.e.

PS (s) =1l−1 p_s (s) = \frac{1}{l-1}

So there are:

PR (R) |drdt (r) |=1l−1

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.