The problem with this, though, is that it assumes the blob #10 in this array is the same, but moved blob #10 in the previous array. The way that OpenCV creates its list of blobs would most likely prevent this technique from being effective.
To keep track of blobs, you’ll want to do collision detection between blobs on this frame and the last. Each blob will need to record an ID and its last updated frame to really track it. You’ll want a basic blob structure to track this data:
Keep a vector list of active blobs. Keep a frame counter going – start at 0 and just increment by 1 on every frame. And keep track of blob IDs by creating an ID integer which you will increment every time you add a new blob.
On every frame, get blobs from your video, as usual. Iterate through all the blobs from OpenCV and collision detect them against the blobs in your list.
If an OpenCV blob is colliding with a blob in your list, copy your list blob’s x, y to its pX, pY and the OpenCV blob’s x, y (centroid) to your list blob’s x, y. Update the blob’s frame number with the application’s current frame (will explain why you need to do this in a moment).
If the OpenCV blob does not collide with anything in your list, increment your ID counter and add a new blob to the list. Be sure to assign the frame number, x & y position, and width and height (which you can get from the bounds). I like to copy the outline data, too, but keep it simple for now.
So, you now have a list complete with new and updated blobs. To remove blobs from your list that are no longer on the screen, simply iterate through and erase blobs where the “frame” property is less than the current frame. Per the techniques above, all current blobs for this frame will have a “frame” property that matches your frame counter. If a blob’s “frame” value is less than the current frame, it wasn’t updated or added on this frame – it’s not on the screen.
So NOW you have a current list of active blobs. New blobs will not have a pX, pY value, but updated blobs will. Comparing each blob’s x & y with its pX & pY will give you angle and velocity and whatever other information you can surmise from two points.
Additionally, this technique removes limits on the number of blobs you can track. If you work with a vector, you don’t have to cap it.