opencv-with me. Histogram equalization of Digital image processing

Source: Internet
Author: User

Starting with this blog post, the niche is formally transferred from a irrelevant professional to digital image processing. Nonsense not much to say, talk was cheap. Show me the code.

    • The purpose of histogram equalization

Because some image grayscale distribution is too concentrated, this will cause the image hierarchy is not clear, histogram equalization is to make the image gray distribution more evenly, the image of a more hierarchical sense.

    • Mathematical Principles

Derivation of conclusion based on continuous gray scale distribution

Histogram equalization belongs to the content of gray-scale transformation (intensity transformation) in Digital image processing, and the aim of gray-scale transformation is to find a suitable mapping function s=t (R). To map the gray value of the original image to the new image, we have achieved the goal of optimizing the image.

It is assumed that the gray-scale statistical histogram of the original image is normalized to PR (r). The original image has a grayscale range (0~L-1). So the histogram equalization is one such mapping function:

The gray distribution of the mapped image is PS (s), which is based on the theory of probability theory (the relationship between probability density of random variable function and probability density of random variable):

Derivation of both sides of a mapping function

So we can get the transformed image histogram distribution to

We can see that the transformed Image gray histogram distribution constant is 1/(L-1), which achieves the above purpose, so that the gray distribution of the image is more uniform, more hierarchical sense.

Note: In grayscale transformations, the transform function T (r) needs to meet the following two-point requirement,

    1. When 0≤r≤l-1, T (R) is a strictly incrementing function.
    2. When 0≤r≤l-1, 0≤t (R) ≤l-1.

The 1th requirement is that the light and dark order of the pixel grayscale after the change before and after the transformation cannot be changed, and the reason for this is to be strictly incremented to ensure that the pixels can correspond to each other before and after the change.

The reason for the 2nd requirement is that the transformed image cannot exceed the original grayscale progression.

It is not difficult to find, in fact, the histogram equalization process does not necessarily meet the condition 1. So the transformation is irreversible.

Discretization of formulas

Set the original image grayscale level 0, 1, 2 ... L-1. The mapping formula after discretization is

When using the above formula to calculate, we need to calculate the result s (r), approximate to the nearest integer.

    • Histogram equalization based on OPENCV

OpenCV has a special histogram equalization function, equalizehist, the definition of the header file in Imaproc/imaproc.hpp.

Histogram equalization Code snippet based on OPENCV:

1 //load the original image and show2 Mat src,dst_1;3src = imread ("test.jpg",0);4Namedwindow ("Originalgrayimage");5Imshow ("Originalgrayimage", SRC);6 7 //Use the OpenCV measure do histogram equalization8 equalizehist (src,dst_1);9Namedwindow ("Histogram EQUALIZATION_OPENCV");TenImshow ("Histogram EQUALIZATION_OPENCV", dst_1);

Simulation results:

Original:

Results with Equalizehist equalization:

    • According to the derivation process, the histogram equalization is written by oneself

The code snippet is as follows:

1 //rewrite the histogram algorithm2 //get some needed information3 intNR =src.rows;4 intNC =Src.cols;5 intn = nr*NC;6 Mat dst_2 (nr,nc,cv_8u);7 8 //get the histogram of original image9Uchar *p_1 =NULL;TenUnsignedinthist[ the] = {0}; One  for(intI=0; i<nr;i++) A { -P_1 = src.ptr<uchar>(i); -      for(intj=0; j<nc;j++) the     { -HIST[P_1[J]] = hist[p_1[j]]+1; -     } - } +  - //calculate the equalized histogram +Uchar transf_fun[ the] = {0}; Atransf_fun[0] = (UCHAR) (255*hist[0]/n); at  for(intI=1;i< the; i++) - { -Hist[i] = hist[i-1]+Hist[i]; -Transf_fun[i] = (UCHAR) (255*hist[i]/n); - } -     in //pad dst_2 The equalized values -Uchar *p_2 =NULL; to  for(intI=0; i<nr;i++) + { -p_2 = dst_2.ptr<uchar>(i); theP_1 = src.ptr<uchar>(i); *      for(intj=0; j<nc;j++) $     {Panax NotoginsengP_2[J] =Transf_fun[p_1[j]]; -     } the } +  A //Show the results of our own histogram algorithm theNamedwindow ("Histogram Equalization_own"); +Imshow ("Histogram Equalization_own", dst_2)

Operation Result:

opencv-with me. Histogram equalization of Digital image processing

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.