title: Automatic color calibration for object tracker
body: <p>This is my first post, so forgive me if I miss something.</p>
  
  <p>I have been playing around with OpenCV2 with Visual Studio C++. I have a basic object tracker working. By applying a Gaussian Blur, Converting to HSV, Thresholding with Trackbars, Eroding then Dilating. Now I want to set up some way of easily calibrating the color to be thresholded without using the Trackbars. 
  I've tried setting up an area of interest and taking the average BGR or HSV values (I've tried both ways). Then if needed use trackbars to make finer adjustments, but it does not seem to work. Am I on the right track, or is there a better way?</p>
  
  <p>I have basically followed this video to get where I am.
  <a href="https://www.youtube.com/watch?v=bSeFrPrqZ2A" rel="nofollow">https://www.youtube.com/watch?v=bSeFrPrqZ2A</a></p>
  
  <p>I am not looking for a code to copy and paste. I am just looking for an Algorithm or explanation of a way to do it. Cheers  </p>
  
  <p><strong>EDIT</strong></p>
  
  <hr>
  
  <p>Sorry I'll try and clear it up. What I have done is written an object tracking program for a home robot vision project. I just want to make it easier to calibrate what color is to be thresholded. At the moment I use trackbars to set the min and max HSV values for thresholding. Then use Erode and Dilate to clear up the binary image. Before using cv::findConturs and cv::moments to find the centroid for the largest contour.
  What I have tried is setting a small 40x40pixel square in the center of the screen. When, for example, I hold a green ball in this square and hit spacebar. I cycle through each pixel in the square and get each separate Hue, Saturation and Value um...value. Then take the mode of each and use that to set the min and max threshold values. </p>
  
  <p>Here is a segment of the code</p>
  
  <p>if(cv::waitKey(20) == 32){              // wait for spacebar </p>
  
  <pre><code>int count = 0;
  cv::Mat roi_Crop = frame_HSV(roi);       //create cropped image from frame_HSV
  
   for(int i=0; i&lt;roi_Crop.rows; i++)    // cycle through each pixel
   {
      for(int j=0; j&lt;roi_Crop.cols; j++)
      { 
  
  Hue[count] = roi_Crop.at&lt;cv::Vec3b&gt;(i,j)[0];
  Sat[count] = roi_Crop.at&lt;cv::Vec3b&gt;(i,j)[1];
  Val[count] = roi_Crop.at&lt;cv::Vec3b&gt;(i,j)[2];
  count++;
      }
   }
  HSV_Mode[0] = findMode(Hue);
  HSV_Mode[1] = findMode(Sat);
      HSV_Mode[2] = findMode(Val);
  </code></pre>
  
  <p>}</p>
  
  <p>I hope this helps.</p>