Title: "Python Algorithm for image histogram equalization"
date:2018-06-12t17:10:48+08:00
Tags: [""]
Categories: ["Python"]
Code
#!/usr/bin/env Python3# Coding=utf-8ImportMatplotlib.image asMpimg fromMatplotlibImportPyplot asPltImportSysImportNumPy asNpdefEqualization (Gray_value):"""Pass in gray value, equalize the gray value, do not need to return, directly modify the parameters passed in:p Aram Gray_value: """ # Statistical Grayscale histogramGray=Np.zeros ( the) row, column=Gray_value.shape forIinch Range(ROW): forJinch Range(column): Gray[gray_value[i][j]]+= 1 # calculate grayscale ratioGray/=(Row*Column# show Grayscale histogramPlt.subplot (2,2,2) Plt.plot (Gray) Cumsum=Np.cumsum (Gray)# Calculate cumulative and # Equalization # EQUA_T[I]=J indicates that the original gray value I is converted to gray value after equalization J # 255x cumulative and rounded to int typeequa_t=Np.array ((255 *Cumsum+ 0.5). Astype (Np.int32)# Number of shades of gray after statistics equalizationEqua_gray=Np.zeros ( the) forIinch Range( the): Equa_gray[equa_t[i]]+=Gray[i]# Show the histogram after equalizationPlt.subplot (2,2,4) Plt.plot (Equa_gray)# Equalization of the original Gray matrix forIinch Range(ROW): forJinch Range(column): Gray_value[i][j]=EQUA_T[GRAY_VALUE[I][J]]defRun (Img_path): Img_array=Mpimg.imread (Img_path) Plt.subplot (2,2,1) Plt.imshow (Img_array) Img_array*= 255Img_array=Img_array.astype (Np.int32) equalization (img_array[:,:,0]) Equalization (img_array[:,:,1]) Equalization (img_array[:,:,2]) Img_array=Img_array.astype (Np.float64) Img_array/= 255Plt.subplot (2,2,3) Plt.imshow (Img_array)if __name__ == "__main__":ifSYS.ARGV.__len__()<= 1: PNG= input("Please enter the name of the picture to be processed:\ n")Else: PNG=sys.argv[1] Run (PNG) plt.show ()
Python algorithm for Image histogram equalization