Search code examples
opencvkalman-filter

control matrix for opencv predict() in Kalman filter implementation for trajectory estimation?


I am trying to estimate the position based on current position (x,y) and create a trajectory.For this I am using kalman filter.I am using opencv based kalman filter implementation in my code.I'm not getting info about what should I take value for 'control matrix' and 'control vector' to avoid collision case for this trajectory estimation based on position vector.

My Transition matrix is position [x,y], measurement matrix is position [x,y].Then what about control matrix? Can anybody help me out to get all these matrix value if I have only position [x,y] as input.

As I found here , control we are passing to predict() function as argument but where it's updating ?

Thanks in advance.


Solution

  • For the simplest case you don't need a control matrix (to make it sure you need to provide more information on your model).

    My Transition matrix is position [x,y]

    It sounds strange for me. I think you meant your state vector. If so I would suggest you add more state variables to be able to predict you state during the prediction step. Depending on your system dynamic you could just add the velocities (vx, vy) or maybe both velocities and accelerations (vx, vy, ax, ay).

    Your state vector would look like

    x = [px py vx vy]'

    The state variables will be updated according to your transition matrix, even if you have no measurement for the velocities and accelerations.

    Am example for an appropriate control vector in this case could be a reading from brakes (if you model a vehicle). Without knowing the brake force your system will predict the motion according to the velocity estimation to the current time step and will predict further moving although the system decelerates.

    You can have a look at this post Kalman filter with varying timesteps. The filter has only measurements of X and Y position, but the state vector incorporates the velocities.

    UPDATE

    Assume your model needs a control input.

    As seen in the OpenCV code, you have following functions in your Kalman Filter Library:

    void KalmanFilter::init(int DP, int MP, int CP, int type)
    
    const Mat& KalmanFilter::predict(const Mat& control)
    
    const Mat& KalmanFilter::correct(const Mat& measurement)
    

    To be able to use the control input you need to pass a correct matrix dimension CP when initializing the filter (zero would mean you don't have a control matrix).

    Your Filtering Code would look like this:

    init(...)
    
    predict(...)
    correct(...)
    
    predict(...)
    correct(...)
    
    and so on
    

    At each call you will get some measurements from your environment. If we stick with the vehicle example and involve break readings as a control input, then each time calling predict() you need to pass actual break force into the prediction function. If there is no break activity, the matrix will be empty and the control input will not be applied at the current call.

    Here you can see a code example with a control input. The guy had an error in his init() call (he initialized CP=0 instead of CP=3) but the code looks ok.

    For more help I need to know more about your model. From what you've posted so far I cannot see any need in control input.

    UPDATE

    To your question in the comments:

    I want to use my kalman when detection lost.I don't want to use correct since start of frame when detection happening because I have some other algorithm for this and when object is lost then I want to enable kalman for tracking and show.How can I approach it?

    Generally you have a free choice between kalman sub-functions predict() and correct() during the estimation. It depends on how your algorithm is triggered: with a constant call rate (let's say every 100ms) or on measurement event.

    In the case of the constant call rate you check if you have an appropriate measurement and decide if you need a correction or not. If you don't need it you just perform predict(), otherwise predict(), correct(). So it's possible to have no corrections for a while.

    If the filter is triggered through measurement events, you can perform predict() and correct() at each call, but you need to recalculate your F and Q matrices for the prediction depending on the time interval dt since last correction.