2013-02-10 27 views
5

Tôi có thói quen này để làm biểu đồ cân bằng của một bức ảnh:Histogram cân bằng cho python

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") 

Khi tôi chạy này với picture từ trang wiki cho histogram equalization nó kết quả trong này: enter image description here

Thay vì điều chỉnh đúng độ tương phản của hình ảnh thành một cái gì đó dọc theo các dòng this. Tôi đang làm gì sai?

+0

Tại sao downvote? – Nick

+0

Dunno. Không thực sự quan trọng khi có sáu upvotes mặc dù :) –

Trả lời

5

Bạn có chắc chắn mình không hiển thị với bản đồ sai không? Hãy thử

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

Hoặc

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

Genius! haha thật dễ. Và cú pháp chính xác là 'cmap = plt.cm.gray' không phải màu xám. Cảm ơn! – Nick