Graphic histogram equalization and Python implementation, graphic histogram python

Source: Internet
Author: User

Graphic histogram equalization and Python implementation, graphic histogram python

In the process of understanding the histogram equalization, I have referenced some books and blogs. What is confusing is that the author's understanding of the histogram remains on the surface and does not have a deep understanding of its meaning. Therefore, this article will describe the concept of histogram in combination with images, give its Python implementation, and finally share some of her own views on the scientific thinking behind it.

What is a histogram?

For a gray image I, each of her pixel I (x, y) has a gray value, generally, there may be 2 ^ 8 = 256 gray-scale values ,..., 255 ). If we calculate the number of times r appears in I and normalize it (n/n, N is the total number of times all gray values appear ), in this way, we can obtain the probability p (r) of pixel r in I ). If we do the same processing for every possible gray value r, we can get the probability distribution curve shown on the left side of 1, which is our histogram.

Figure 1 target of histogram equalization

What is histogram equalization?

Normally, the probability of r appearing for each gray value is not equal on the left side of the histogram 1 of image I, which leads to insufficient detail information of the image, while histogram equalization is to perform the following Transformation s = T (r) for the gray value r, so that the right side of the transformed gray distribution 1 is shown (that is, the probability that each gray value appears is the same. This allows you to discover details that are hard to be found by the naked eye, as shown in figure 2 (you can understand them yourself ). Speaking of this, it is generally over, but do we really understand it? How can we better understand it? The following is a brief introduction to my understanding.

 

Suppose we have four gray levels a, B, c, and d. In an analogy, we can think of them as four words that describe emotions: Joy, anger, sorrow, and joy. Generally, the histogram we obtain is p (a) = 0.5, p (B) = 0.5, p (c) = 0, p (d) = 0, the histogram after equalization is p (a) = 0.25, p (B) = 0.25, p (c) = 0.25, p (d) = 0.25. if we describe people's emotions with words before equalization, there will be only happiness and anger, while the words after equalization will be joy, anger, sorrow, and joy. Which situations can describe more subtle emotional changes? This is also the reason why I believe that more image details can be described after histogram equalization.

 

Furthermore, if the histogram we obtain is p (a) = 0.4, p (B) = 0.4, p (c) = 0.1, p (d) = 0.1, what does this mean by our understanding? It means that in most cases, people's emotions are described with joy and anger, and sorrow and joy are almost unnecessary. Of course, it is best to use happiness, anger, sorrow, and joy to describe people's emotions.

Figure 2 histogram equalization (before and after left conversion)

Mathematical Principles of histogram equalization

I used to be a mathematical controller. I always thought that everything should be based on the basic principle of mathematics. However, I used mathematical reasoning to draw the expected conclusion. However, at this moment, I feel that my thoughts are fundamentally wrong. The following describes the mathematical principle of histogram equalization.

How can we achieve the goal 1 of histogram equalization? What are the constraints in this process (there is no absolute freedom in the world )?

 

The gray level of the original r image can be set. After s is balanced, the gray level of the image is not lost. It can be assumed that the r value range is [0, 1]. So what are the constraints that must be met? The meaning of gray scale before and after equalization cannot be changed, that is to say, the meaning of gray scale before transformation is from black to white, so it should be the same after transformation. In addition, it is recommended that the gray-level values before and after the change be consistent. In order to keep the gray scale after the transformation from black to white in a single change order, and the transformation range is consistent with the original, to avoid overall brightness or darkness. It must be specified that:

(1) The transformation function T (r) is in the range of [0, 1] and is a single value or monotonic increasing function;

(2) For r belonging to [], s = T (r) also belongs to []

Figure 3 principle of histogram equalization

These two rules are mathematical descriptions that meet our balanced requirements (if we want to use mathematics, we should convert our requirements and objectives into mathematical languages for description ).

Histogram balancing is to use the gray-scale transformation function s = T (r) to change the original image histogram p (r) to the even distribution histogram p (s ). As shown in figure 3, when the two rules are met, the s = T (r) (that is, r is determined, and s is determined) can be seen by combining probability theory.P (r) dr = p (s) ds.BecauseAfter histogram equalization, there are:P (s) = 1, sBelongs to [0, 1]Therefore, ds = p (r) dr. Points on both sides are obtained.

In this way, we can find the ing between s and r.

Histogram equalization PythonImplementation

Mappings between s and r

We can see that for an input grayscale image I, we first calculate its p (r), and then calculate the corresponding.

Def histeq (img, nbr_bins = 256): "" Histogram equalization of a grayscale image ."""
# Obtain the histogram p (r)
Imhist, bins = histogram (img. flatten (), nbr_bins, normed = True)
# Getting T (r)
Cdf = imhist. cumsum () # cumulative distribution function cdf = 255 * cdf/cdf [-1] # Get s and replace the gray value result = interp (img. flatten (), bins [:-1], cdf) return result. reshape (img. shape), cdf

 

Some Thoughts

I used to have such a misunderstanding: I thought that all theories could be derived from basic mathematical principles, so when I encountered a new problem, I always try to figure out the sources and physical meanings of various mathematical formulas, and ignore the purpose behind these mathematical operations! One disadvantage is that the theory that can be understood today will not be understood in a few days.

Some of my experiences are as follows ): first, find out the source of the problem, then know the purpose of solving the problem, then describe the problem in Mathematics (always pay attention to the constraints), and finally solve the mathematical problem.

 

Summary: In general, if we can finish the first three steps and solve the final problem through the help of existing software and algorithms, it should be a matter of course. (I am not a major in mathematics. I usually only care about input, output, and constraints on how to solve a mathematical problem)

 

References:

Programming Computer Vision with Python

 

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.