首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >opencv中基于卡尔曼滤波的多目标跟踪

opencv中基于卡尔曼滤波的多目标跟踪
EN

Stack Overflow用户
提问于 2014-03-07 13:17:55
回答 1查看 10.8K关注 0票数 1

我成功地使用卡尔曼滤波器跟踪了单个目标。现在我要跟踪两个物体。我不知道如何对两个物体应用卡尔曼滤波。有谁能帮帮忙。

EN

回答 1

Stack Overflow用户

发布于 2014-03-07 14:00:39

看看我的实现。

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

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

下面是项目跟踪2个目标的main.cpp文件:

代码语言:javascript
复制
#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;


}
票数 6
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22242108

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档