title: Tracking of multiple objects in openCV using C++
body: <p>I am doing a project in <a href="http://opencv.org/documentation.html" rel="nofollow">OpenCV</a> on estimating the speed of moving vehicle using the video captured. Here the camera is stationary. I have estimated the speed of single object using centroid and Euclidean distance. Now the problem is, I am not getting how to do the same for multiple objects. 
  Here, I need to calculate the Euclidean distance of objects between 2 subsequent frames. 
  I am grateful if anyone would help. </p>
  
  <p>I have created the class-</p>
  
  <pre><code>class centroids
  {
      public:
          vector&lt;Point2f&gt; ce;
          vector&lt;float&gt; area;
  };
  centroids c[100];
  </code></pre>
  
  <p>And this is the code I've written. I would be grateful if anyone helped me with the code:</p>
  
  <pre><code>findContours( fgMaskMOG2, 
                contours, 
                hierarchy,
                CV_RETR_CCOMP, 
                CV_CHAIN_APPROX_SIMPLE );
  int morph_size = 6;
  Mat element = getStructuringElement( MORPH_RECT, 
                                       Size( 2*morph_size+1, 2*morph_size+1 ), 
                                       Point( morph_size, morph_size ) );
  
  
  
  Scalar color( 255, 255, 255 );  // color of the contour in the
  //Draw the contour and rectangle
  for( int i = 0; i &lt; contours.size(); i++ )
  {
      drawContours( fgMaskMOG2,
                    contours,
                    i, 
                    color, 
                    CV_FILLED,
                    8,
                    hierarchy );
  }
  
  //imshow("morpho window",dst);
  
  vector&lt;Moments&gt; mu( contours.size() );
  
  vector&lt;Point2f&gt; mc( contours.size() );
  vector&lt;Point2f&gt; m ;
  
  vector&lt;double&gt; time;
  vector&lt;Point2f&gt; centroid( mc.size() );
  
  //vector&lt; vector&lt;Point&gt; &gt;::iterator itc = contours.begin();
  // iterate through each contour.
  double time1[1000];
  
  for( int i = 0; i &lt; contours.size(); i++ )
  {
      //  Find the area of contour
      double a = contourArea( contours[i], false ); 
  
      if( a &gt; 500 )
      {
          mu[i] = moments( contours[i], false );
          mc[i] = Point2f( (mu[i].m10 / mu[i].m00), (mu[i].m01 / mu[i].m00) );
          m.push_back( mc[i] );
          Point2f diff;
          double euclidian = 0;
          for( int f = 0; f &lt; m.size(); f++ )
          {
              if( k == 1 )
              {
                  c[f].ce.push_back( m[f] );
                  cout &lt;&lt; "cen" &lt;&lt; c[f].ce &lt;&lt; endl;
                  euclidian = 0;
  
              }
              else
              {
                  c[f+1].ce.push_back( m[f] );
                  cout &lt;&lt; "cent" &lt;&lt; c[f+1].ce &lt;&lt; endl;
  
                  diff = c[f].ce[f] - c[f-1].ce[f-1];
  
                  euclidian = abs( sqrt( (diff.x*diff.x) + (diff.y*diff.y) ) );
                  cout &lt;&lt; "euclidian" &lt;&lt; euclidian &lt;&lt; endl;
              }
          }
          cout &lt;&lt; "\n centroid" &lt;&lt; m &lt;&lt; endl;
  
          circle( fgMaskMOG2, 
                  mc[i], 
                  5, 
                  Scalar( 0, 0, 255 ), 
                  1,
                  8,
                  0 );
      }
  }
  </code></pre>
  
  <p>Thanks in advance :)</p>