title: Generating & Plotting Histograms of Gradient Magnitude & Orientation
body: <p>Using <code>matplotlib</code> how can I generate &amp; display a histogram of the image gradient magnitude and orientation?</p>
  
  <p>One thing I don't know is the x axis limits; for magnitude should it be 0 to 1? Ie, the strength of an edge is between 0 and 1? For orientation I assume the x axis limits would be 0 to 360 (degrees) or should I do radians?</p>
  
  <p>Below is how I generate my gradient, magnitude and orientation. How can I now show each as a histogram?</p>
  
  <pre><code>def get_gradient(src):
  
      sobelx = cv2.Sobel(src,cv2.CV_64F,1,0,ksize=5)
      sobely = cv2.Sobel(src,cv2.CV_64F,0,1,ksize=5)
  
      grad = sobelx + sobely
      mag = cv2.magnitude(sobelx, sobely)  # so my Mat element values could be anything between 0 and 1???
      ori = cv2.phase(sobelx, sobely, True) # so my Mat element values could be anything between 0 and 360 degrees???
      return [grad, mag, ori]
  
  grad_res = get_gradient(src)
  
  # number of bins is 100 from 0 to 1. Ie, 0.001, 0.002, ... 1.000 
  # am I correct?
  mag_hist = cv2.calcHist([grad_res[1]],[0],None,[100],[0,1]) 
  
  ori_hist = cv2.calcHist([grad_res[2]],[0],None,[360],[0,360]) 
  
  plt.plot(mag_hist)
  plt.xlim([0,1])
  
  plt.plot(ori_hist)
  plt.xlim([0,360])
  
  plt.show()
  </code></pre>
  
  <p><strong>Edit:</strong> Current error from the above code:</p>
  
  <blockquote>
    <p>mag_hist = cv2.calcHist([gradient[1]],[0],None,[10],[0,100])
    error: C:\projects\opencv-python\opencv\modules\imgproc\src\histogram.cpp:1422: error: (-210)  in function cv::calcHist</p>
  </blockquote>