fromPILImportImage fromPylabImport* fromNumPyImport*defHisteq (Im,nbr_bins = 256): """histogram equalization of a grayscale image""" #calculate the histogram of an image #in NumPy, a function histogram () that calculates the histogram is also provided, the first of which is the histogram statistic, and the second is the median value for each binsImhist,bins = Histogram (Im.flatten (), nbr_bins,normed=True) CDF= Imhist.cumsum ()#CDF = 255.0 * cdf/cdf[-1] #calculates a new pixel value using the linear interpolation of the cumulative distribution functionIM2 = Interp (Im.flatten (), bins[:-1],cdf)returnIm2.reshape (im.shape), Cdfpil_im= Image.open ('E:\Python\\fanwei.jpg')#Open OriginalPil_im_gray = Pil_im.convert ('L')#Convert to Grayscale imagePil_im_gray.show ()#displaying grayscale imagesim= Array (Image.open ('E:\Python\\fanwei.jpg'). CONVERT ('L'))#Figure ()#hist (Im.flatten (), ()IM2,CDF=Histeq (IM)#Figure ()#hist (Im2.flatten (), ()#Show ()im2=Image.fromarray (Uint8 (IM2)) im2.show ()#Print (CDF)#plot (CDF)Im2.save ("junheng.jpg")
Figure 1: Grayscale image of the original
Figure 2: Image after histogram equalization
Figure 3: Histogram of the original grayscale image
Figure 4: Histogram equalization
Figure 5: Grayscale transformation function
Histogram equalization of python--