Histogram Equalization Algorithm Implementation

Source: Internet
Author: User
Histogram

Histogram is a grayscale function that represents the number of pixels in an image with each gray level,

Displays the distribution of various gray values in the source image.

As shown in, the abscissa of a grayscale histogram is a grayscale level, the ordinate is the occurrence frequency of the grayscale level, and is the most basic statistical feature of the image.


The above is the standard histogram.

Cumulative histogram of grayscale statistics:

H (K) = Σ Ni (I <= K)


The height in column K in the cumulative histogram is the number of pixels with all gray values in the image <= K

Implementation of a gray histogram Algorithm
Void gethistogram (byte * image_src, int width, int height, unsigned long * histogram) {int pixelcount = width * height; // imagesize-> pixelcountmemset (histogram, 0,256*4 ); // note that the last parameter is the array size (in bytes) for (INT I = 0; I <= pixelcount-1; ++ I) {int gray = image_src [0]; histogram [Gray] ++; // The next pixel image_src + = 1 ;}}


Histogram features

A histogram has many advantages. A Histogram can reflect the overall appearance of an image,

  • There are several types of targets in the image, and how are the targets and backgrounds distributed;

  • Histogram can be used to directly calculate the maximum brightness, minimum brightness, average brightness, contrast, and intermediate brightness of an image.

  • You can use histograms to complete image segmentation and target retrieval. Because different targets have different color distributions. The normalized histogram is used for target matching. It is not subject to the influence of target flip and target size change.

  • In the image query system, histograms are widely used,Using it to store the target features occupies a small amount of space, and the execution speed is fast.

  • Disadvantages: because it does not record location information, different images will have the same or similar histograms. The histogram after an image is rotated and flipped is the same; the histogram after enlargement and reduction is similar.

Histogram equalization

Principle

To enhance the overall comparison effect of the image and increase the dynamic range of the gray value

According to the image vertex operation, the image enhancement formula can be expressed

G (x, y) = f (g (x, y ))

Here, because we want to enhance the comparison effect
Here we assume that the grayscale range of the source image is normalized [0, 1].

  • F () is an incremental function within the entire gray scale range [0, 1] (because of the need to enhance the comparison)

  • The value range of F () is also [0, 1].

It can be proved that the cumulative distribution function meets the requirements of the preceding transformation function. In the image, this cumulative distribution function is the cumulative histogram of the original image.
F () is usually used as the Lut of the query table. Because it is discrete, the cumulative Histogram can also be used as the LUT.

In fact, the cumulative distribution histogram is used as an image transformation function (essentially LUT ). This will enhance the comparison effect of the original image. Normally, the image transformation function is discrete and implemented through LUT. Therefore, histogram equalization can be achieved through the histogram of accumulative distribution.

 

The entropy principle can also be used to explain the equilibrium principle:

Entropy (entropy) is a measure of the amount of information, which is defined:

650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4B/17/wKioL1QoBbWzyk1IAAAcYO8-5EQ815.jpg "style =" float: none; "Title =" 2014-09-28_205719.jpg "alt =" wKioL1QoBbWzyk1IAAAcYO8-5EQ815.jpg "/>

Pi indicates the probability of the occurrence of symbol I. In the image, 650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M01/4B/15/wKiom1QoBeaT19-8AAAei2E3AtA404.jpg "Title =" 2014-09-28_205753.jpg "alt =" wKiom1QoBeaT19-8AAAei2E3AtA404.jpg "/> PR is the probability that Gray Level R appears.

It can be proved that when p0 = p1 = P2 =... = P255 = 1/256, H takes the maximum value, that is, the maximum image information.

According to entropy theory, when H [0], H [1]…, When H [n-1] is equal, the maximum amount of image information-the purpose of equalization is to make each Hi equal, that is, to convert the histogram of the original graph into a uniform distribution form, in this way, the range of pixel values is increased, and the image comparison effect is enhanced.


For the concept of entropy, I also spent a lot of effort to understand what he meant:

The greater the entropy, the greater the uncertainty of the system. The more chaotic the system, the larger the information.

For a single piece of information, it can be understood that the smaller the probability of occurrenceGreater entropy

For a system, if the system consists of multiple parts, it can be understood that each part has the highest entropy when the probability of occurrence is basically equal.

For more information about entropy, see Wu Jun's interpretation of entropy in the beauty of mathematics.


Histogram Equalization Algorithm Implementation

Void gethistogramequalize (byte * image_src, byte * image_dst, int width, int height) {// ------- step 1. calculate the grayscale histogram unsigned long historgam [256]; gethistogram (image_src, width, height, historgam); // ------ step 2. calculate the histogram of cumulative distribution (gray-scale transformation function, LUT) // the histogram of cumulative distribution meets the requirement of the enhanced contrast function int LUT [256]; LUT [0] = historgam [0]; int sum = historgam [0]; for (INT I = 1; I <= 255; ++ I) {sum + = historgam [I]; lut [I] = 255 * sum/(width * Height);} // ---- step 3. int pixelcount = width * height; For (INT I = 0; I <= pixelcount-1; ++ I) {int gray = image_src [I]; image_dst [I] = LUT [Gray];}

(Using opencv to display images)


650) This. width = 650; "src =" http://s3.51cto.com/wyfs02/M02/4B/15/wKioL1QoA3KABweyAAXY_lr1bhE981.jpg "Title =" image5.png "style =" float: none; "alt =" wkiol1qoa3kabweyaaxy_lr1bhe981.jpg "/>



This article from "QQ" blog, please be sure to keep this source http://qianqing13579.blog.51cto.com/5255432/1559162

Histogram Equalization Algorithm Implementation

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.