Path to mathematics-python computing practice (14)-machine vision-image enhancement (histogram equalization)-python Histogram

Source: Internet
Author: User

Path to mathematics-python computing practice (14)-machine vision-image enhancement (histogram equalization)-python Histogram

Let's look at a grayscale image to show the number of gray-scale occurrences. In this way, the probability of gray-scale pixels in the image is

It is the number of gray scales in the image. It is the number of pixels in the image. It is actually the histogram of the image and is normalized.

The aggregate probability function is defined:

Is the cumulative normalized histogram of the image.

We create a form of change that generates one for each value in the original image, so that the cumulative probability function can be linear within the range of all values. The conversion formula is defined:

Note that T maps different levels to domains. to map these values back to their original domains, you need to apply the following simple transformation in the results:

The histogram equalization method is used for Grayscale Images. However, this method can be used for the red, green, and blue components of RGB color values.


Python:   Cv2. EqualizeHist (Src [, dst] )→ Dst
C:  Void CvEqualizeHist (Const CvArr * Src, CvArr * Dst )
Parameters:
  • Src-Source 8-bit single channel image.
  • Dst-Destination image of the same size and typeSrc.

The function equalizes the histogram of the input image using the following algorithm:

  1. Calculate the histogramSrc.

  2. Normalize the histogram so that the sum of histogram bins is 255.

  3. Compute the integral of the histogram:

  4. Transform the image using as a look-up table:

The algorithm normalizes the brightness and increases the contrast of the image.

# -*- coding: utf-8 -*-   #code:myhaspl@myhaspl.comimport cv2fn="test1.jpg"myimg=cv2.imread(fn)img=cv2.cvtColor(myimg,cv2.COLOR_BGR2GRAY)newimg=cv2.equalizeHist(img)cv2.imshow('src',img)cv2.imshow('dst',newimg)cv2.waitKey()cv2.destroyAllWindows()


All content in this blog is original. If it is reproduced, please indicate the source http://blog.csdn.net/myhaspl/the right image below is a reinforced image.

Histogram equalization is usually used to increase the global contrast of many images, especially when the contrast of useful data is very close. In this way, the brightness can be better distributed on the histogram. This method can be used to enhance the local contrast without affecting the overall contrast.
All the content in this blog is original. If you reprint the content, please indicate the source http://blog.csdn.net/myhaspl/the following example code verifies the algorithm.
#-*-Coding: UTF-8-*-# code: myhaspl@myhaspl.com # histogram equalization import cv2import numpy as npfn = "test5.jpg" myimg = cv2.imread (fn) img = cv2.cvtColor (myimg, cv2.COLOR _ BGR2GRAY) h = img. shape [0] w = img. shape [1] newimg = np. zeros (h, w), np. uint8) scount = 0.0 # original image gray scale scol ={}# target image gray scale dcol ={}# original image frequency Ps ={}# cumulative probability Cs ={}# count original image gray scale m in xrange (h): for n in xrange (w): scol [img [m, n] = scol. setdefault (img [m, n], 0) + 1 scount + = 1


Source image on the left and histogram equalization on the right




How do I know the original image after histogram equalization? Do not use the MATLAB program for the image after histogram equalization. I only understand the principle.

Is the histogram comparison before and after balancing.
The purpose of histogram equalization is to make every brightness point as much as possible, so that the grayscale details of the image are displayed as much as possible in front of the human eye, in line with the pattern of the human eye to view the image.
Therefore, the histogram of the old graph is not flat. After the histogram is balanced, the histogram tends to be flat.
Take the preceding two histograms as an example:
Assume that the brightness of an image is at least 0, and the brightness is at most 255 (I will not mention grayscale)
1. Assume that the image is a M * M pixel image.
2. After the conversion, assume that the brightness of the old graph is between 0 and 10, which is equal to the number of points between 0 and 30 of the New Graph (because many points of the old graph are crowded between 0 and 10, the new graph should open these points)
3. Because the new graph is flat, it is easy to assume that the brightness is 30 points, accounting for 1/256 Of the total points, that is, (M * M)/256.
4. Therefore, in the new graph, the number of points between 0 and 30 is 30 * (M * M)/256.
5. Based on the equation in the relation column 2 and 4 above
30 = (256/(M * M) * (total number of points with brightness ranging from 0 to 10)
6. Change 10 to p and 30 to q. In this way, the brightness p of the New Graph corresponding to p in any old graph can be calculated. The ing relationship between the old image and the new image comes out.
The above calculation is based on the assumption: (3) the histogram of the new graph is flat.


Reference: www.mathworks.cn/..q.html


Why does histogram equalization effectively enhance images?

After histogram equalization, the gray scale of a relatively small number of pixels will be allocated to another gray scale. The pixels are relatively concentrated. After processing, the gray scale range increases, the contrast increases, and the definition increases, therefore, images can be effectively enhanced.

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.