Python alignment histogram

I have this procedure for aligning the histogram of a photo:

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

When I run this with a picture on the wiki page for histogram alignment , this leads to the following: enter image description here

Instead of properly adjusting the contrast of the image to something along the lines of this . What am I doing wrong?

+5
source share
1 answer

Are you sure you are not using rendering with the wrong color palette? Try

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

or

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

All Articles