有沒有人可以幫助我?我正在學習影像處理,我正在寫一段代碼來做直方圖均衡化,但輸出的影像總是全黑。
import numpy as np
import scipy.misc, math
from PIL import Image
img = Image.open('/home/thaidy/Desktop/ex.jpg').conversion('L')
#轉換為ndarray。
img1 = np.asarray(img)
#converting to 1D[/span]#histogram和bins被計算。
hist, bins = np.histogram(img1,256,[0,255] )
#cdf計算了。
cdf = hist.cumsum()
#places where cdf = 0 is ignored[/span].
#其余部分存盤在cdf_m中。
cdf_m = np.ma.masked_equal(cdf,0)
#histogram eq被執行。
num_cdf_m = (cdf_m - cdf_m.min())*255。
den_cdf_m = (cdf_m.max()-cdf_m.min())*255。
cdf_m = num_cdf_m/den_cdf_m
#被掩蓋的地方現在是0。
cdf = np.ma.fill(cdf_m,0).astype('uint8')
#cdf值分配在扁平化的陣列中。
im2 = cdf[fl]
#transformin in 2D[/span]。
im3 = np.reshape(im2,img1.shape)
im4 = Image.fromarray(im3)
im4.save('/home/thaidy/Desktop/output.jpg'/span>)
im4.show()
這是我的代碼
uj5u.com熱心網友回復:
在均衡化之前,需要對cdf進行規范化處理。
一種方法是將np.histogram的可選引數density設定為True:
hist, bins = np.hist(img1,256,[0,255], density=True)
另一種方法是將計算后的cdf除以總像素數:
cdf = hist.cumsum()
cdf /= cdf[-1]
我還會把均衡部分簡單地改成:
T = (255 * cdf).astype(np.uint8)
im2 = T[fl]
維基百科建議使用這個轉換公式來代替:
T = (np.ceil(256 * cdf) - 1).astype(np.uint8)
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/331313.html
標籤:
