python - How to properly use matplotlib render an image histogram? -


i'm trying write python small program obtain grayscale histogram image. i'm using opencv , matplotlib i'm not getting results when trying display graph.

this i'm doing:

  1. convert grayscale.
  2. create 256 length list of zeros.
  3. iterate on each pixel, grayscale value , increment in 1 value of list @ same position in list of pixel grayscale value.
  4. later, try use matplotlib hist function. i'm not getting expected result.

this i'm trying:

img = cv2.imread(sys.argv[1], 0) # cv2.imshow("imagen",img) height, width = img.shape print height print width hist = [0] * 256 print hist y in xrange(height):     x in xrange(width):         pixel = img[y,x]         hist[pixel] = hist[pixel] + 1 print hist print max(hist) pyplot.hist(hist,256, range=(0,255)) pyplot.show() 

i printed min , max value , max 4001, , i'm obtaining.

enter image description here

how can obtain proper plot?

note: know there , other implementations opencv in case have code myself.

you don't want pyplot.hist(hist,256, range=(0,255)), since trying aggregate data you, you've aggregated yourself.

try this

pyplot.plot(hist) 

Comments

Popular posts from this blog

python - Creating a new virtualenv gives a permissions error -

facebook - android ACTION_SEND to share with specific application only -

go - Idiomatic way to handle template errors in golang -