title: opencv drawing a 2d histogram
body: <p>I'm wondering how to plot a 2d histogram of an HSV Mat in opencv c++. My current code attempting to display it fails miserably. I've looked around on how to plot histograms and all the ones I've found were those plotting them as independent 1d histograms.</p>
  
  <p>Here's my current output with the number of hue bins being 30 and saturation bins being 32:</p>
  
  <p><img src="https://i.imgur.com/2CO1j4N.png" alt=""></p>
  
  <p>Here's another output with the number of hue bins being 7 and saturaation bins being 5:</p>
  
  <p><img src="https://i.imgur.com/1xBXGsj.png" alt=""></p>
  
  <p>I would like it to look more like the result here</p>
  
  <p><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>I also noticed whenever I do cout &lt;&lt; Hist.size it gives me 50x50. Am I to understand that just means the first dimension of the array is 250 in size?</p>
  
  <p>Also, how does one sort the histogram from highest to lowest (or vice versa) value frequency? That is another problem I am trying to solve. </p>
  
  <p>My current function is as follows.</p>
  
  <pre><code>void Perform_Hist(Mat&amp; MeanShift, Mat&amp; Pyramid_Result, Mat&amp; BackProj){  
  
    Mat HSV, Hist;
  
    int histSize[] = {hbins, sbins};
    int channels[] = {0, 1};
  
    float hranges[] = {0, 180};
    float sranges[] = {0, 256};
  
    const float* ranges[] = {hranges, sranges};
  
    cvtColor(MeanShift, HSV, CV_BGR2HSV);
  
    Mat PyrGray = Pyramid_Result.clone();
  
    calcHist(&amp;HSV, 1, channels, Mat(), Hist, 2, histSize, ranges, true, false); 
    normalize(Hist, Hist, 0, 255, NORM_MINMAX, -1, Mat());  
    invert(Hist, Hist, 1);  
    calcBackProject(&amp;PyrGray, 1, channels, Hist, BackProj, ranges, 1, true);
  
    double maxVal = 0; minMaxLoc(Hist, 0, &amp;maxVal, 0, 0);
  
    int scale = 10;
    Mat histImage = Mat::zeros(sbins*scale, hbins*10, CV_8UC3);
  
    for(int i = 1; i &lt; hbins * sbins; i++){
       line(histImage,
       Point(hbins*sbins*(i-1), sbins - cvRound(Hist.at&lt;float&gt;(i-1))),
       Point(hbins*sbins*(i-1), sbins - cvRound(Hist.at&lt;float&gt;(i))),
       Scalar(255,0,0), 2, 8, 0);
    }
    imshow (HISTOGRAM, histImage);
  }
  </code></pre>