title: Calculating the mean and standard deviation in C++ for single channeled histogram
body: <p>I want calculate the mean and standard deviations for a histogram of a HSV image but I only want to do this histogram and calculations for the V channel.</p>
  
  <p>I have been reading examples on how to do this for a set of channels and have tried these approaches but I am getting confused over whether my approach for initially creating the histogram is correct or not for just one channel because the program keeps crashing when i try to execute it.</p>
  
  <p>Here is what I have at the moment (The variable test is a cv::Mat image and this can be any image you wish to use to recreate the issue). I have probably missed something obvious and the for loop might not be correct in terms of the range of values but I haven't done this in C++ before.</p>
  
  <pre><code>        cv::cvtColor(test, test, CV_BGR2HSV);
  
  
          int v_bins = 50;
          int histSize[] = { v_bins };
          cv::MatND hist;
  
          float v_ranges[] = { 0, 255};
          cv::vector&lt;cv::Mat&gt; channel(3);
          split(test, channel);
  
          const float* ranges[] = { v_ranges };
          int channels[] = {0};
  
          cv::calcHist(&amp;channel[2], 1, channels, cv::Mat(), hist, 1, histSize, ranges, true, false); //histogram calculation
  
          float mean=0;
  
          float rows= hist.size().height;
          float cols = hist.size().width;
  
          for (int v = 0; v &lt; v_bins; v++)
          {
              std::cout &lt;&lt; hist.at&lt;float&gt;(v, v) &lt;&lt; std::endl;;
              mean = mean + hist.at&lt;float&gt;(v);
          }
  
          mean = mean / (rows*cols);
          std::cout &lt;&lt; mean&lt;&lt; std::endl;;
  </code></pre>