title: Drawing Histogram in OpenCV-Python
body: <p>I was just trying to draw histogram using new OpenCV Python interface ( cv2 ).</p>
  
  <p>Below is the code i tried:</p>
  
  <pre><code>import cv2
  import numpy as np
  import time
  
  img = cv2.imread('zzz.jpg')
  h = np.zeros((300,256,3))
  b,g,r = cv2.split(img)
  bins = np.arange(256).reshape(256,1)
  color = [ (255,0,0),(0,255,0),(0,0,255) ]
  
  for item,col in zip([b,g,r],color):
      hist_item = cv2.calcHist([item],[0],None,[256],[0,255])
      cv2.normalize(hist_item,hist_item,0,255,cv2.NORM_MINMAX)
      hist=np.int32(np.around(hist_item))
      pts = np.column_stack((bins,hist))
      cv2.polylines(h,[pts],False,col)
  
  h=np.flipud(h)
  
  cv2.imshow('colorhist',h)
  cv2.waitKey(0)
  </code></pre>
  
  <p>And it works fine. Below is the resulting histogram i obtained. </p>
  
  <p><img src="https://i.stack.imgur.com/P9LB1.jpg" alt="enter image description here"></p>
  
  <hr>
  
  <p>Then i modified the code a little bit. </p>
  
  <p>ie changed the sixth line in code <code>b,g,r = cv2.split(img)</code> to <code>b,g,r = img[:,:,0], img[:,:,1], img[:,:,2]</code> (because it works a little faster than <code>cv2.split</code>).</p>
  
  <p>Now the output is something different. Below is the output.</p>
  
  <p><img src="https://i.stack.imgur.com/Sbifa.jpg" alt="enter image description here"></p>
  
  <hr>
  
  <p>I checked the values of <code>b,g,r</code> from both the codes. They are same. </p>
  
  <p>Difference lies in the output of <code>cv2.calcHist</code>. Result of <code>hist_item</code> is different in both the cases. </p>
  
  <p><strong>Question</strong>:</p>
  
  <p>How does it happen? Why the result of <code>cv2.calcHist</code> is different when inputs are same?</p>
  
  <p><strong>EDIT</strong></p>
  
  <p>I tried a different code. Now, a numpy version of my first code.</p>
  
  <pre><code>import cv2
  import numpy as np
  
  img = cv2.imread('zzz.jpg')
  h = np.zeros((300,256,3))
  b,g,r = img[:,:,0],img[:,:,1],img[:,:,2]
  bins = np.arange(257)
  bin = bins[0:-1]
  color = [ (255,0,0),(0,255,0),(0,0,255) ]
  
  for item,col in zip([b,g,r],color):
      N,bins = np.histogram(item,bins)
      v=N.max()
      N = np.int32(np.around((N*255)/v))
      N=N.reshape(256,1)
      pts = np.column_stack((bin,N))
      cv2.polylines(h,[pts],False,col,2)
  
  h=np.flipud(h)
  
  cv2.imshow('img',h)
  cv2.waitKey(0)
  </code></pre>
  
  <p>And the output is same as first one. </p>
  
  <p><img src="https://i.stack.imgur.com/RDVoD.jpg" alt="enter image description here"></p>
  
  <p>You can get my original image here: <a href="http://imageshack.us/photo/my-images/528/zzzyj.jpg/" rel="noreferrer">zzz.jpg</a></p>
  
  <p>Thank you.</p>