title: Tracking multiple moving objects with KalmanFilter in OpenCV/C++ - How to assign tracks individually to detected objects
body: <p>I am working on realtime movement detection and object tracking in OpenCV/C++ and am currently stuck at the tracking part.</p>
  
  <p><strong>Matlab-Example of what I want to do: <a href="http://www.mathworks.de/de/help/vision/examples/motion-based-multiple-object-tracking.html" rel="noreferrer">http://www.mathworks.de/de/help/vision/examples/motion-based-multiple-object-tracking.html</a></strong> (I'm troubled with the tracking part and how to transfer it to C++/OpenCV)</p>
  
  <p>My motion part works with OpenCVs BackgroundSubtractor MOG2 which makes it possible to find contours and filter smaller contours out.</p>
  
  <p>For tracking I am currently trying to use the KalmanFilter (with a similar implementation to <a href="https://github.com/screename/Kalman-Filter-Tracker" rel="noreferrer">this</a>) which right now is getting called in every frame if a moving object was found and draws a line on it's path.
  My Detection &amp; Tracking part looks something like this:</p>
  
  <pre><code>BackgroundSubtractorMOG2 bg;
  bg.operator()(frame, threshold);
  bg.getBackgroundImage(background);
  ...  //morphological operations to remove noise etc.
  findContours(threshold, ...);
  ...  //filtering to reject contours which are too smalle/too big
  
  for(int i = 0; i &lt; contours.size(); i++){
  approxPolyDP(...);
  boundRect = boundingRect(...);
  x = boundRect.x + boundRect.width/2;
  y = boundRect.y + boundRect.height/2;}
  
  kalmanFilter.track(x,y);
  kalmanFilter.draw(frame);
  </code></pre>
  
  <p><em>Current Problem:</em> 
  I have a scene with 0 moving objects, then 1 object moves in, gets detected through contours and gets tracked. Then a 2nd object moves in sight, gets detected and makes the tracker jump to it instead of following the first or <strong>marking both individually</strong> (which I want).</p>
  
  <p>The current tracker takes x &amp; y coordinates of the found object. Like this, once another object gets detected, the <strong>tracker still assumes it is the same object</strong> but with other coordinates than anticipated.</p>
  
  <p>As one can see, there's <strong>no function to assign "tracks" to a certain object</strong> which is probably the biggest problem. I read about the hungarian algorithm but am not quite sure on how to implement it within my functions.</p>
  
  <p><strong>What would be a good method to make the tracking work for multiple objects?</strong> </p>
  
  <p>My idea was that if I had <strong>each object uniquely identified</strong> I could check if the ID is still the same and if not <strong>let the tracker know that it's a new object</strong> to track separately from the other. Not sure if this is necessary or even useful though and if so, how to do it.</p>