title: OpenCV : How to find the center of mass/centroid for motion information
body: <p>The thing is I am unable to implement the center of mass with the existing code, which image object to use etc after the detected object is bounded by the rectangle so that I may get the trajectory of the path.
      I am using <code>Opencv2.3</code> .I found out there are 2 methods - <a href="https://stackoverflow.com/questions/11542121/finding-an-images-contours-centroid">Link1</a> and <a href="https://stackoverflow.com/questions/9074202/opencv-2-centroid">Link2</a> talk about the usage of moments. And the other method is to use the information of the bounding box <a href="https://stackoverflow.com/questions/10942742/opencv-c-usage-of-cvmoments">Link3</a>. The method of moments requires image thresholding. However, when using SURF the image is in gray scale. So, on passing a gray image for thresholding displays a white image! Now, I am having a tough time in understanding how I should calculate the centroid using the code below (esp what should I use instead of <code>points[i].x</code> since I am using </p>
  
  <pre><code>obj.push_back( kp_object[ good_matches[i].queryIdx ].pt );
  scene.push_back( kp_image[ good_matches[i].trainIdx ].pt )
  </code></pre>
  
  <p>where in my case <code>numPoints=good_matches.size()</code>, denoting the number of feature points)  as mentioned in the documentation. If anyone can put up an implementation of how to use SURF with centroid then it will be helpful.</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;iostream&gt;
  #include "opencv2/core/core.hpp"
  #include "opencv2/features2d/features2d.hpp"
  #include "opencv2/highgui/highgui.hpp"
  #include "opencv2/imgproc/imgproc.hpp"
  #include "opencv2/calib3d/calib3d.hpp"
  
  using namespace cv;
  
  int main()
  {
      Mat object = imread( "object.png", CV_LOAD_IMAGE_GRAYSCALE );
  
      if( !object.data )
      {
          std::cout&lt;&lt; "Error reading object " &lt;&lt; std::endl;
          return -1;
      }
  
      //Detect the keypoints using SURF Detector
      int minHessian = 500;
  
      SurfFeatureDetector detector( minHessian );
      std::vector&lt;KeyPoint&gt; kp_object;
  
      detector.detect( object, kp_object );
  
      //Calculate descriptors (feature vectors)
      SurfDescriptorExtractor extractor;
      Mat des_object;
  
      extractor.compute( object, kp_object, des_object );
  
      FlannBasedMatcher matcher;
  
      VideoCapture cap(0);
  
      namedWindow("Good Matches");
  
      std::vector&lt;Point2f&gt; obj_corners(4);
  
      //Get the corners from the object
      obj_corners[0] = cvPoint(0,0);
      obj_corners[1] = cvPoint( object.cols, 0 );
      obj_corners[2] = cvPoint( object.cols, object.rows );
      obj_corners[3] = cvPoint( 0, object.rows );
  
      char key = 'a';
      int framecount = 0;
      while (key != 27)
      {
          Mat frame;
          cap &gt;&gt; frame;
  
          if (framecount &lt; 5)
          {
              framecount++;
              continue;
          }
  
          Mat des_image, img_matches;
          std::vector&lt;KeyPoint&gt; kp_image;
          std::vector&lt;vector&lt;DMatch &gt; &gt; matches;
          std::vector&lt;DMatch &gt; good_matches;
          std::vector&lt;Point2f&gt; obj;
          std::vector&lt;Point2f&gt; scene;
          std::vector&lt;Point2f&gt; scene_corners(4);
          Mat H;
          Mat image;
  
          cvtColor(frame, image, CV_RGB2GRAY);
  
          detector.detect( image, kp_image );
          extractor.compute( image, kp_image, des_image );
  
          matcher.knnMatch(des_object, des_image, matches, 2);
  
          for(int i = 0; i &lt; min(des_image.rows-1,(int) matches.size()); i++) //THIS LOOP IS SENSITIVE TO SEGFAULTS
          {
              if((matches[i][0].distance &lt; 0.6*(matches[i][4].distance)) &amp;&amp; ((int) matches[i].size()&lt;=2 &amp;&amp; (int) matches[i].size()&gt;0))
              {
                  good_matches.push_back(matches[i][0]);
              }
          }
  
          //Draw only "good" matches
          drawMatches( object, kp_object, image, kp_image, good_matches, img_matches, Scalar::all(-1), Scalar::all(-1), vector&lt;char&gt;(), DrawMatchesFlags::NOT_DRAW_SINGLE_POINTS );
  
          if (good_matches.size() &gt;= 4)
          {
              for( int i = 0; i &lt; good_matches.size(); i++ )
              {
                  //Get the keypoints from the good matches
                  obj.push_back( kp_object[ good_matches[i].queryIdx ].pt );
                  scene.push_back( kp_image[ good_matches[i].trainIdx ].pt );
              }
  
              H = findHomography( obj, scene, CV_RANSAC );
  
              perspectiveTransform( obj_corners, scene_corners, H);
  
              //Draw lines between the corners (the mapped object in the scene image )
              line( img_matches, scene_corners[0] + Point2f( object.cols, 0), scene_corners[1] + Point2f( object.cols, 0), Scalar(0, 255, 0), 4 );
              line( img_matches, scene_corners[1] + Point2f( object.cols, 0), scene_corners[2] + Point2f( object.cols, 0), Scalar( 0, 255, 0), 4 );
              line( img_matches, scene_corners[2] + Point2f( object.cols, 0), scene_corners[3] + Point2f( object.cols, 0), Scalar( 0, 255, 0), 4 );
              line( img_matches, scene_corners[3] + Point2f( object.cols, 0), scene_corners[0] + Point2f( object.cols, 0), Scalar( 0, 255, 0), 4 );
          }
  
          //Show detected matches
          imshow( "Good Matches", img_matches );
  
          key = waitKey(1);
      }
      return 0;
  }
  </code></pre>