1

I am using the KalmanFilter Class in OpenCV to predict a Point.

I am tracking an Contour that means i recieve an x,y Point of it.

So my Code looks like that:

First I say that I got 4 dynamic parameters and 2 measurement parameters. The way i understood it is that my tracked (x,y) Positions are my measurements and with the velocity of them i got 4 dynamic parameters

 #Give Input to Kalman class
kalman = cv2.KalmanFilter(4,2)
kalman.measurementMatrix = np.array([[1,0,0,0],[0,1,0,0]],np.float32)
kalman.transitionMatrix = np.array([[1,0,1,0],[0,1,0,1],[0,0,1,0],[0,0,0,1]],np.float32)

After this i track my contour in opencv and pass the position to the class.

mp = np.array([[np.float32(center_x)],[np.float32(center_y)]])
kalman.correct(mp)
tp = kalman.predict()
PredictedPointx,PredictedPointy = int(tp[0]),int(tp[1])

And as a result i get 2 Predicted Points and my code works.

The thing is i dont know what exactly happens in this whole process.

I got some questions like

  1. Why does my transition matrix look like this

enter image description here

And not like this :

enter image description here

  1. Is it correct that i recieve the velocity after giving my 2 points?

  2. Where are all the other Kalman values and calculations. For example the Kalman Gain and so on.

Can someone explain what exactly happens after passing in my x and y position.

Thank you for reading

ShibaInu
  • 41
  • 9

1 Answers1

3

This post is too long for a comment, so I'll just parse it as an answer.

1.) What FPS are you working on and where did you find the transition matrix? If it is 1 FPS, then dt = 1 and your transition matrix is OK. If it is not 1 FPS, your velocity estimate will not be correct (or more precisely, it will not be what you'd expect). I believe the filter would still work properly, but output a velocity not measured in meters per second (or pixels per second), but instead based on meters per 1/FPS. I'm not entirely sure about that, so please leave a comment if you believe I am distributing nonsense ;-) Be aware that, as soon as you pass a process noise parameter, this starts to make a difference and you should set your dt properly.

2.) You should receive an estimate on the velocity as soon as you supply more than one point. Assuming the velocity does not change abruptly, the quality of the velocity estimate will improve with the number of points you feed to the filter.

3.) The Kalman gain is not a parameter of the Kalman filter. Given the assumptions of the filter, the optimal kalman gain is calculated during the iteration of the filter and used in the actual filtering (see K_t on the Kalman Filter Wikipedia page). The process noise and measurement noise covariance matrices are missing, but you can set them with processNoiseCov and measurementNoiseCov respectively (according to the documentation)

André
  • 1,034
  • 9
  • 19
  • Thank you for your answer. 1)So the first part is interesting ill do more research about that but thanks for your tips. 2) second part is now clear for me. Is there a way to print the velocity in the kalman class? 3) Alright thats also clear for me now. I didnt thought that the filter works without the noise. Edit: I dont if i should make a second post here, but is there a way to use control vector u () in my case ? like to implement a friction. Or is it for advanced systems? – ShibaInu Jul 11 '22 at 15:37
  • 1
    @ShibaInu Be aware that I've never worked with the library, I'm just a guy that likes filters ;-). That being said: 2.) If you print the kalman filter state `tp`, the velocities in x and y should be `tp[2]` and `tp[3]` respectively. 3.) You really need to read [the documentation](https://docs.opencv.org/3.4/dd/d6a/classcv_1_1KalmanFilter.html). I know it's annoying to crawl through these, but it basically shows you everything the library can do. To me it seems there is a control matrix you can set, and you can pass a control parameter to the `predict` call. – André Jul 12 '22 at 07:34
  • 2
    general word of caution: OpenCV's APIs aren't always "good". much of the stuff is implemented by someone who just learned it themselves (GSoC), and haven't grasped everything yet. if you're puzzled about anything in the OpenCV API, because it contradicts the theory/math, don't doubt yourself too much. OpenCV might simply need a bug report. – Christoph Rackwitz Jul 12 '22 at 10:31
  • @André You are right if, if I use the OpenCV implementation i need to get through it. I did some of the math to the filter and also calculated an example of the 1 Dimensional Kalman FIlter.But when I used it in OpenCV I got a bit confused and yeah. But you guys helped me alot. So i found something of the dt in the transition matrix in this post [link]https://stackoverflow.com/questions/55195011/how-to-define-state-transition-matrix-for-kalman-filters#:~:text=The%20transition%20model%20is%20then,variance%20of%20your%20next%20measure. Thank you again for answering – ShibaInu Jul 12 '22 at 12:00