使用opencv中的kalman过滤器跟踪多个对象

时间:2014-03-07 05:17:55

标签: opencv

我成功地使用卡尔曼滤波器跟踪单个对象。现在我想跟踪两个对象。我不知道如何为两个对象应用卡尔曼滤波器。任何人都可以帮忙。

1 个答案:

答案 0 :(得分:6)

看看我的实施情况。

https://www.youtube.com/watch?v=2fW5TmAtAXM

GitHub上的消息来源:https://github.com/Smorodov/Multitarget-tracker

这是项目的main.cpp文件,用于跟踪2个目标:

#include "opencv2/opencv.hpp"
//#include "BackgroundSubtract.h"
//#include "Detector.h"

#include <opencv2/highgui/highgui_c.h>
#include "CTracker.h"
#include <iostream>
#include <vector>

using namespace cv;
using namespace std;

float X=0,Y=0;
float Xmeasured=0,Ymeasured=0;
RNG rng;
//-----------------------------------------------------------------------------------------------------
// Mouse callback
//-----------------------------------------------------------------------------------------------------
void mv_MouseCallback(int event, int x, int y, int /*flags*/, void* /*param*/)
{
    if(event == cv::EVENT_MOUSEMOVE)
    {
        X=(float)x;
        Y=(float)y;
    }
}

int main(int ac, char** av)
{
    int k=0;
    // Track colors
    Scalar Colors[]={Scalar(255,0,0),Scalar(0,255,0),Scalar(0,0,255),Scalar(255,255,0),Scalar(0,255,255),Scalar(255,255,255)};
    namedWindow("Video");
    Mat frame=Mat(800,800,CV_8UC3);

    VideoWriter vw=VideoWriter::VideoWriter("output.mpeg", CV_FOURCC('P','I','M','1'), 20, frame.size());

    // Attach mouse callback to window
    setMouseCallback("Video",mv_MouseCallback,0);

    CTracker tracker(0.2,0.5,60.0,25,25);
    float alpha=0;
    while(k!=27)
    {
        frame=Scalar::all(0);

        // add some noise (simulation of real measurement)
        Xmeasured=X+rng.gaussian(2.0);
        Ymeasured=Y+rng.gaussian(2.0);

        // Add tracking targets
        // sin and cos added for more fun :)
        vector<Point2d> pts;
        pts.push_back(Point2d(Xmeasured+100.0*sin(-alpha),Ymeasured+100.0*cos(-alpha))); // 1-st target coords
        pts.push_back(Point2d(Xmeasured+100.0*sin(alpha),Ymeasured+100.0*cos(alpha)));   // 2-nd target coords 
        //pts.push_back(Point2d(Xmeasured+100.0*sin(alpha/2.0),Ymeasured+100.0*cos(alpha/2.0)));
        //pts.push_back(Point2d(Xmeasured+100.0*sin(alpha/3.0),Ymeasured+100.0*cos(alpha/1.0)));
        alpha+=0.05;

    // Draw targets
    for(int i=0; i<pts.size(); i++)
    {
    circle(frame,pts[i],3,Scalar(0,255,0),1,CV_AA);
    }

    // Update tracks
        tracker.Update(pts);

    //  cout << tracker.tracks.size()  << endl;

    // Draw tracks
        for(int i=0;i<tracker.tracks.size();i++)
        {
            if(tracker.tracks[i]->trace.size()>1)
            {
                for(int j=0;j<tracker.tracks[i]->trace.size()-1;j++)
                {
                    line(frame,tracker.tracks[i]->trace[j],tracker.tracks[i]->trace[j+1],Colors[i%6],2,CV_AA);
                }
            }
        }
        imshow("Video",frame);

        // write videoframe to file
        // vw << frame;
        k=waitKey(10);
    }
    vw.release();
    destroyAllWindows();
    return 0;


}