title: How do I plot a 32-Bin Histogram for a Grayscale Image in Python using OpenCV
body: <p>I have been having difficulty trying to generate a histogram for a 640x480 grayscale image I am working with.</p>
  
  <p>I am using Python 2.7.3, OpenCV 2.4.6 (Python bindings) and Numpy</p>
  
  <p>The image below was generated from the same image, using an executable software tool (programmed in C++)</p>
  
  <p>The properties for this histogram were:</p>
  
  <pre><code>bins = 50
  hist_width = 250
  normalised_height_max = 50
  </code></pre>
  
  <p><img src="https://i.imgur.com/0SIcikf.png" alt="BW Histo" title="Grayscale Histogram"></p>
  
  <p>The image specs are therefore 250x50</p>
  
  <p>I have consulted this documentation:</p>
  
  <p>Histogram Calculation in OpenCV
  <a href="http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html" rel="nofollow noreferrer">http://docs.opencv.org/doc/tutorials/imgproc/histograms/histogram_calculation/histogram_calculation.html</a></p>
  
  <p>Hist.py - OpenCV Python Samples
  <a href="https://github.com/Itseez/opencv/blob/master/samples/python2/hist.py" rel="nofollow noreferrer">https://github.com/Itseez/opencv/blob/master/samples/python2/hist.py</a></p>
  
  <p>The code in the second reference compiles fine, yet I have tried to edit it to get these block style columns rather than the thin lines and I can't seem to get it right.</p>
  
  <pre><code>import cv2
  import numpy as np
  
  cv2.namedWindow('colorhist', cv2.CV_WINDOW_AUTOSIZE)
  
  img = cv2.imread('sample_image.jpg')
  h = np.zeros((50,256))
  
  bins = np.arange(32).reshape(32,1)
  hist_item = cv2.calcHist([img],0,None,[32],[0,256])
  cv2.normalize(hist_item,hist_item,64,cv2.NORM_MINMAX)
  hist=np.int32(np.around(hist_item))
  pts = np.column_stack((bins,hist))
  cv2.polylines(h,[pts],False,(255,255,255))
  
  h=np.flipud(h)
  
  cv2.imshow('colorhist',h)
  cv2.waitKey(0)
  </code></pre>
  
  <p>I am aiming to make my histogram with the following specs:</p>
  
  <pre><code>bins = 32
  hist_width = 256
  normalised_height_max = 64
  </code></pre>
  
  <p>How can I fix this code in order to achieve a histogram like the one above with the specs specified?</p>