Python實現映像長條圖均衡化演算法

來源:互聯網
上載者:User

標籤:矩陣   /usr   圖片   value   float   sha   title   長條圖   TE   

title: "Python實現映像長條圖均衡化演算法"
date: 2018-06-12T17:10:48+08:00
tags: [""]
categories: ["python"]

代碼
#!/usr/bin/env python3# coding=utf-8import matplotlib.image as mpimgfrom matplotlib import pyplot as pltimport sysimport numpy as npdef equalization(gray_value):    """    傳入灰階值,對灰階值做均衡化,不需要返回,直接修改傳入的參數    :param gray_value:    """    # 統計灰階長條圖    gray = np.zeros(256)    row, column = gray_value.shape    for i in range(row):        for j in range(column):            gray[gray_value[i][j]] += 1    # 計算灰階佔比    gray /= (row * column)    # 顯示灰階長條圖    plt.subplot(2, 2, 2)    plt.plot(gray)    cumsum = np.cumsum(gray)  # 計算累積和    # 均衡化    # equa_t[i]=j表示原灰階值i經過均衡化後轉化為灰階值j    # 255×累積和四捨五入為int型    equa_t = np.array((255 * cumsum + 0.5)).astype(np.int32)    # 統計均衡化後的灰階數量    equa_gray = np.zeros(256)    for i in range(256):        equa_gray[equa_t[i]] += gray[i]    # 顯示均衡化後的長條圖    plt.subplot(2, 2, 4)    plt.plot(equa_gray)    # 對原灰階矩陣做均衡化    for i in range(row):        for j in range(column):            gray_value[i][j] = equa_t[gray_value[i][j]]def run(img_path):    img_array = mpimg.imread(img_path)    plt.subplot(2, 2, 1)    plt.imshow(img_array)    img_array *= 255    img_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 /= 255    plt.subplot(2, 2, 3)    plt.imshow(img_array)if __name__ == "__main__":    if sys.argv.__len__() <= 1:        png = input("請輸入要處理的圖片名:\n")    else:        png = sys.argv[1]    run(png)    plt.show()

Python實現映像長條圖均衡化演算法

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.