溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

python數(shù)字圖像處理實現(xiàn)直方圖與均衡化

發(fā)布時間:2020-09-01 15:31:45 來源:腳本之家 閱讀:155 作者:denny402 欄目:開發(fā)技術(shù)

在圖像處理中,直方圖是非常重要,也是非常有用的一個處理要素。

在skimage庫中對直方圖的處理,是放在exposure這個模塊中。

1、計算直方圖

函數(shù):skimage.exposure.histogram(image,nbins=256)

在numpy包中,也提供了一個計算直方圖的函數(shù)histogram(),兩者大同小義。

返回一個tuple(hist, bins_center), 前一個數(shù)組是直方圖的統(tǒng)計量,后一個數(shù)組是每個bin的中間值

import numpy as np
from skimage import exposure,data
image =data.camera()*1.0
hist1=np.histogram(image, bins=2)  #用numpy包計算直方圖
hist2=exposure.histogram(image, nbins=2) #用skimage計算直方圖
print(hist1)
print(hist2)

輸出:

(array([107432, 154712], dtype=int64), array([ 0. , 127.5, 255. ]))
(array([107432, 154712], dtype=int64), array([ 63.75, 191.25]))

分成兩個bin,每個bin的統(tǒng)計量是一樣的,但numpy返回的是每個bin的兩端的范圍值,而skimage返回的是每個bin的中間值

2、繪制直方圖

繪圖都可以調(diào)用matplotlib.pyplot庫來進行,其中的hist函數(shù)可以直接繪制直方圖。

調(diào)用方式:

復(fù)制代碼 代碼如下:
n, bins, patches = plt.hist(arr, bins=10, normed=0, facecolor='black', edgecolor='black',alpha=1,histtype='bar')

hist的參數(shù)非常多,但常用的就這六個,只有第一個是必須的,后面四個可選

arr: 需要計算直方圖的一維數(shù)組

bins: 直方圖的柱數(shù),可選項,默認為10

normed: 是否將得到的直方圖向量歸一化。默認為0

facecolor: 直方圖顏色

edgecolor: 直方圖邊框顏色

alpha: 透明度

histtype: 直方圖類型,‘bar', ‘barstacked', ‘step', ‘stepfilled'

返回值 :

n: 直方圖向量,是否歸一化由參數(shù)normed設(shè)定

bins: 返回各個bin的區(qū)間范圍

patches: 返回每個bin里面包含的數(shù)據(jù),是一個list

from skimage import data
import matplotlib.pyplot as plt
img=data.camera()
plt.figure("hist")
arr=img.flatten()
n, bins, patches = plt.hist(arr, bins=256, normed=1,edgecolor='None',facecolor='red') 
plt.show()

python數(shù)字圖像處理實現(xiàn)直方圖與均衡化

其中的flatten()函數(shù)是numpy包里面的,用于將二維數(shù)組序列化成一維數(shù)組。

是按行序列,如

mat=[[1 2 3

    4 5 6]]

經(jīng)過 mat.flatten()后,就變成了

mat=[1 2 3 4 5 6]

3、彩色圖片三通道直方圖

一般來說直方圖都是征對灰度圖的,如果要畫rgb圖像的三通道直方圖,實際上就是三個直方圖的疊加。

from skimage import data
import matplotlib.pyplot as plt
img=data.lena()
ar=img[:,:,0].flatten()
plt.hist(ar, bins=256, normed=1,facecolor='r',edgecolor='r',hold=1)
ag=img[:,:,1].flatten()
plt.hist(ag, bins=256, normed=1, facecolor='g',edgecolor='g',hold=1)
ab=img[:,:,2].flatten()
plt.hist(ab, bins=256, normed=1, facecolor='b',edgecolor='b')
plt.show()

其中,加一個參數(shù)hold=1,表示可以疊加

python數(shù)字圖像處理實現(xiàn)直方圖與均衡化

4、直方圖均衡化

如果一副圖像的像素占有很多的灰度級而且分布均勻,那么這樣的圖像往往有高對比度和多變的灰度色調(diào)。直方圖均衡化就是一種能僅靠輸入圖像直方圖信息自動達到這種效果的變換函數(shù)。它的基本思想是對圖像中像素個數(shù)多的灰度級進行展寬,而對圖像中像素個數(shù)少的灰度進行壓縮,從而擴展取值的動態(tài)范圍,提高了對比度和灰度色調(diào)的變化,使圖像更加清晰。

from skimage import data,exposure
import matplotlib.pyplot as plt
img=data.moon()
plt.figure("hist",figsize=(8,8))

arr=img.flatten()
plt.subplot(221)
plt.imshow(img,plt.cm.gray) #原始圖像
plt.subplot(222)
plt.hist(arr, bins=256, normed=1,edgecolor='None',facecolor='red') #原始圖像直方圖

img1=exposure.equalize_hist(img)
arr1=img1.flatten()
plt.subplot(223)
plt.imshow(img1,plt.cm.gray) #均衡化圖像
plt.subplot(224)
plt.hist(arr1, bins=256, normed=1,edgecolor='None',facecolor='red') #均衡化直方圖

plt.show()

python數(shù)字圖像處理實現(xiàn)直方圖與均衡化

以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持億速云。

向AI問一下細節(jié)

免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

AI