# kalman filter solution to some cases

Hi, i have implemented a algorithm for tracking multiple blobs, using the kalman filter. For assigning the blobs with the kalman filters i am using the hungarian algorithm. From the pairs that the hungarian algorithm is giving to me, i am keeping the good ones by determining a threshold of 50 as the maximum distance between the center point of the blob and the predicted point from the matched kalman filter. Them implementation works quite well except of two cases. For these two cases i would like you opinion, about what i can do. I will try to make it simpler using some images as well.

This image shows the case were everything works fine and my tracking works like a charm.

**case 1**

The above image refers to one of the cases that i have problem and i want to solve. As you can see one of the blobs is split into two blobs, because of an obstacle.

As i have it now, for the new blob i will create a new kalman filter (for each blob that is not assigned with a kalman filter and there is not a good free kalman filter to be assigned, i am creating a new filter).

The point is that after a while when the blob will be merged again the old kalman filter which is the correct one will be erased after some continuously bad matched pairing, since the the newer one will give me better matches through the hungarian algorithm. So, my first question is how i can avoid this, somehow.

**case 2**

The second case it can be seen in the following images

again we have a blob with the corresponding kalman filter

at some point the blob disappears, the kalman filter remains for some period active in order to check if it was just a case of short period occlusion, but at the same time another blob appears. So, in that case i have a free kalman filter, and the assignment with the new blob is good (the distance between the center point of the blob and the predicted point from the kalman filter is smaller than the threshold).

so the kalman filter will be assigned with the wrong blob, although the good blob will appear at some point.

so since the kalman filter of the first blob was assigned to another blob wrongly, and there is not a good or free kalman filter to be assigned to the old blob a new filter will be created.

As you can see from the last image the first kalman filter makes some steps backwards, furthermore my case is that i am not gonna have backward moving from my blobs.

So, although the good matching through the hungarian algorithm to the blob with the shortest distance, at the same time, because it is at the back the new point is a bad measurement for my kalman filter. My question is if there is a way to say to the kalman ...

case 1 solved, actually it was simpler than i thought. i just measuring the distance of the new blob's center point to all the other blobs that are assigned with a kalman filter and if this is lower than s threshold then i am taking into account that the new blob is the same blob as the one with the lower distance. For case 2, i would like to ask if it possible the new measurement point before to be passed to the kalman filter, to be checked if it is an outlier according to a metric like the distance (mahalanobis, chi-square, etc...) with the internal covariance matrices of the kalman filter. If this is possible which covariance matrix should i use?

In other words how can i create a validation gate?

Hello @theodore. I am trying to do something similar with opencv 3.0 righ now. I have succeded to extract blob centers with Bgs but been stuck there since I couldn't track them to do counting at some point. Could you please help me with blob identification using Hungarian algorithm or any other method that worked with you back then ? I would really appreciate it if you post some code also, since I am not that expert with opencv. Thanks very mush

Hi @opencvTn unfortunately I cannot provide any code for now, maybe in the future I will try to make a tutorial about kalman filter in addition to hungarian algorithm. However, I can point you to some links that might help you. Check this link for an implementation of kalman filter (actually it includes everything i.e. detector, tracker, hungarian algorithm, etc...), here is a video of it and in this link for an implementation of hungarian algorithm in opencv and this one for another implementation of the latter in pure c++.

@theodore That's very usefull, thank you ! I am working on it