在opencv中使用卡尔曼过滤器跟踪多个对象 [英] tracking multiple objects using kalman filter in opencv

查看:102
本文介绍了在opencv中使用卡尔曼过滤器跟踪多个对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我能够使用卡尔曼滤波器成功跟踪单个对象.现在我要跟踪两个对象.我不知道如何对两个对象应用卡尔曼滤波器.谁能帮忙.

I am successfully able to track single object using kalman filter. Now i want track two objects. I have no idea how to apply kalman filter for two objects. Can anyone please help.

推荐答案

看看我的实现.

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

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

Sources on GitHub: https://github.com/Smorodov/Multitarget-tracker

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

Here is main.cpp file for the project to track 2 targets:

#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;


}

这篇关于在opencv中使用卡尔曼过滤器跟踪多个对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆