Math Road-python Computing (14)-Machine vision-image enhancement (histogram equalization)

Source: Internet
Author: User

Let's look at a grayscale image that shows the number of times the grayscale appears, so that the probability of the occurrence of gray-scale pixels in the image is

Is the total number of gray in the image, is the total number of pixels in the image, in fact, the histogram of the image, normalized to.

The cumulative probability function as corresponding to is defined as:

is the cumulative normalized histogram of the image.

We create a change of form, for each value in the original image it produces one, so that the cumulative probability function can be linearized within the full range of values, the conversion formula is defined as:

Note T maps different levels to the domain. To map these values back to their original domain, you need to apply the following simple transformations on the results:

The above description describes the method of using histogram equalization on grayscale images. However, the color image can also be processed by using this method for the red, green, and blue components of the image RGB color values respectively.


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 type as src .

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

  1. Calculate the histogram for src .

  2. Normalize the histogram so, 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: [Email protected]import 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 ()


the whole content of this blog is original, if reproduced please indicate the sourcehttp://blog.csdn.net/myhaspl/the image below is an enhanced graph

watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvbxloyxnwba==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "/>

histogram equalization Pass is often used to add a lot of images to the globalControl degree, especially when the contrast of the practical data of the image is quite close.

In this way, the brightness can be better distributed on the histogram. This can be used to enhance the local control without affecting the overall control degree.
the whole content of this blog is original, if reproduced please indicate the source http://blog.csdn.net/myhaspl/the following part of the code to verify the implementation of the algorithm

#-*-Coding:utf-8-*-   #code: [email protected] #直方图均衡化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 level scol={}# Target image Gray level dcol={} #原始图像频度Ps ={} #累计概率Cs ={} #统计原始图像灰度级for m in Xrange (h): For    N in Xrange (w):        scol[img[m,n]]= Scol.setdefault (img[m,n],0) +1        scount+=1


Left is the source graph. The graph on the right is the histogram equalization



Math Road-python Computing (14)-Machine vision-image enhancement (histogram equalization)

Related Article

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.