python computer vision 1.3.4 histogram equalization

A very useful example of grayscale transformation of images is histogram equalization. Histogram equalization refers to flattening the grayscale histogram of an image so that the distribution probability of each grayscale value in the transformed image is the same. Histogram equalization is usually a very good way to normalize the grayscale values ​​of an image and enhance the contrast of the image before further processing the image.
In this case, the transformation function for histogram equalization is the cumulative distribution function (cdf for short, a normalization operation that maps the range of pixel values ​​to the target range) of the pixel values ​​in the image.

The following function is a concrete implementation of histogram equalization.

def histeq ( im , nbr_bins = 256 ) :
 """ Histogram equalization for a grayscale image"""
      # Calculate the histogram of the image
 imhist,bins = histogram ( im . flatten (), nbr_bins , normed = True )        
    cdf = imhist.cumsum ( ) # cumulative distribution function
     cdf = 255 * cdf / cdf[ - 1 ] # normalization
   # use linear interpolation of the cumulative distribution function to calculate new pixel values
 ​​im2 = interp ( im . flatten (), bins[ :- 1 ],cdf)
     return im2.reshape ( im .shape ), cdf

    

The function has two input parameters, one is the grayscale image and one is the number of bins used in the histogram. The function returns the histogram equalized image and the cumulative distribution function used to map the pixel values. Note that the last element of the cumulative distribution function (subscripted -1) is used in the function to normalize it to the range 0...1.

function usage

#!/usr/bin/python
 # -*- coding: utf-8 -*-
from PIL import Image
from pylab import *
from PCV.tools import imtools

# Add Chinese font support
 from matplotlib.font_manager import FontProperties
font = FontProperties(fname=r"c:\windows\fonts\SimSun.ttc", size=14)

im = array (Image. open ( 'D:/pictures/gakki.png' ). convert ( 'L' ))   # Open the image and convert it to grayscale
 #im = array(Image. open('D:/ pictures/fanghuonv.png').convert('L'))
 im = array ( Image.open ( '../data/AquaTermi_lowcontrast.JPG ' ) . convert ( 'L' ))
im2, cdf = imtools.histeq(im)

figure ()
 subplot ( 2 , 2 , 1 )
 axis ( 'off' )
 gray ()
 title ( u'original image' , fontproperties = font)
 imshow (im)

subplot ( 2 , 2 , 2 )
 axis ( 'off' )
 title ( u'Histogram equalized image' , fontproperties = font)
 imshow (im2)

subplot(2, 2, 3)
axis('off')
title(u'原始直方图', fontproperties=font)
#hist(im.flatten(), 128, cumulative=True, normed=True)
hist(im.flatten(), 128, normed=True)

subplot ( 2 , 2 , 4 )
 axis ( 'off' )
 title ( u'equalized histogram' , fontproperties = font)
 #hist(im2.flatten(), 128, cumulative=True, normed=True)
 hist ( im2.flatten (), 128 , normed = True )

show()
 



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324651155&siteId=291194637