IKH

Tracking pseudo code- I

Here, we will go through the pseudo code of the tracking algorithm to give you the intuition. The actual code will be explained by Anand later in the segment. Let’s revise what you have learned from the previous session.

You can download the notebook below

So previously, we took the difference between 2 images to find the motion. Later, we had done preprocessing like blurring, thresholding and morphological transformation. Then all possible vehicles were identified using contours and hulls, calculated some of their properties, stored them in a Blob class and these blobs were validated using like area, aspect ratio etc. You had also seen that these blob objects were stored in a list of currentFrameBlobs(). Let’s see how to track the vehicles.

So you learnt that you do tracking to count a vehicle only once, while it crosses the lane. To track, we use 2 type of lists: currentFrameBlob() and existingBlobs().

Suppose we are at 501th timestep. currentFrameBlob() will contain the list of all the blobs identified between 501th and 500th frame. Suppose you identified 10 blobs at 501th  timestep, so 10 blobs will be there in the currentFrameBlob[] . But between 0  to 500th frame, many vehicles must have entered and left the frame and the number will be much bigger than 10. So, existingBlobs[] will contain the list of all those vehicles up to the previous time step (500th timestep). But why do we need all the blobs present in existingBlobs[]? Let’s see why. 

You have seen that there are only a few blobs, tagged as ‘valid’ blobs, which need to be tracked. The other ‘invalid’ blobs for which we have stopped tracking (most probably they have left the frame after passing the lane) will remain on the list as dormant. 

Intuition in tracking

For every blob that you find at the current time step (present in currentFrameBlobs[]), you want to know whether that blob was also present in the previous time frame or not. So, if that blob is present in the previous time step (blobs from the previous timesteps are present in existingBlobs[]), you do not count it again as a separate vehicle in the current time step.

Coming to tracking, we first extrapolate the position of the ‘valid’ blobs in existingBlobs[] to predict their next position in the current time step. If you look at the code, the variable centreposition in the class Blob(), it’s defined as a list which contains previously tracked centre positions. Using those, extrapolation will be done. 

Problem: How will you track the blobs at current 501thtimestep?

Suppose, up to the 500th timestep, there are 100 blobs in the existingBlobs[] list but only 10 blobs are ‘valid’. So, extrapolation will be done only on those 10 blobs to find their predicted position at the 501th time step. 

Now, if there are 11 blobs at 501th time step (current timestep) in currentFrameBlobs[], for each of those 11 blobs, you will find distance with each of the10 blobs in existingBlobs[]. Then find the minimum of those distances. If this minimum distance is less than a ‘threshold’ distance, we say that the blob is being tracked. 

In the next segment, you will continue looking at the pseudo code. 

Report an error