2013-02-10 121 views
5

直方圖均衡化我有這個程序做照片的直方圖均衡:爲蟒蛇

def histeq(im,nbr_bins=256): 

    #get image histogram 
    imhist,bins = histogram(im.flatten(),nbr_bins,normed=True) 
    cdf = imhist.cumsum() #cumulative distribution function 
    cdf = 255 * cdf/cdf[-1] #normalize 

    #use linear interpolation of cdf to find new pixel values 
    im2 = interp(im.flatten(),bins[:-1],cdf) 

    return im2.reshape(im.shape), cdf 

#im = array(Image.open('AquaTermi_lowcontrast.jpg').convert('L')) 
im = array(Image.open('Unequalized.jpg').convert('L')) 
#Image.open('plant4.jpg').convert('L').save('inverted.jpg') 

im2,cdf = histeq(im) 

plt.imshow(im2) 
plt.savefig("outputhisto.jpg") 

當我運行這個與picture來自維基頁面histogram equalization它會導致這樣的: enter image description here

而不是正確調整圖像的對比度,沿着this的行。我究竟做錯了什麼?

+0

爲什麼downvote? – Nick 2013-02-10 20:30:17

+0

鄧諾。當有六個upvotes時,無所謂:) – 2016-11-11 15:26:01

回答

5

您確定只是沒有使用錯誤的顏色表進行渲染嗎?嘗試

plt.imshow(im2, cmap=plt.cm.gray) 

或者

plt.imshow(im2, cmap=plt.get_cmap('gray')) 
+1

天才!哈哈,這很容易。確切的語法是'cmap = plt.cm.gray'而不是灰色。謝謝! – Nick 2013-02-10 20:13:56