Python algorithm for Image histogram equalization

Source: Internet
Author: User

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

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.