资源简介
基于Camshift和Kalman的多目标跟踪算法,当被跟踪目标被遮挡时,通过kalman进行位置估计
代码片段和文件信息
// multitracking camshift kalman.cpp: 定义控制台应用程序的入口点。
//
#include “stdafx.h“
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
using namespace cv;
Mat image;
bool backprojMode = false;
bool selectobject = false;
int trackobject = 0;
bool showHist = true;
Point origin;
Rect selection;
vector trackpoint1 trackpoint2;
int vmin = 10 vmax = 256 smin = 30;
// User draws box around object to track. This triggers CAMShift to start tracking
static void onMouse(int event int x int y int void*)
{
if (selectobject)
{
selection.x = MIN(x origin.x);
selection.y = MIN(y origin.y);
selection.width = std::abs(x - origin.x);
selection.height = std::abs(y - origin.y);
selection &= Rect(0 0 image.cols image.rows);
}
switch (event)
{
case EVENT_LBUTTONDOWN:
origin = Point(x y);
selection = Rect(x y 0 0);
selectobject = true;
break;
case EVENT_LBUTTONUP:
selectobject = false;
if (selection.width > 0 && selection.height > 0)
trackobject = -1; // Set up CAMShift properties in main() loop
break;
}
}
int main(int argc const char** argv)
{
//kalman filter setup
//const int stateNum = 4;
const int stateNum = 6;
const int measureNum = 2;
const float T = 0.9;
KalmanFilter KF(stateNum measureNum 0);
Mat state(stateNum 1 CV_32F);
Mat processNoise(stateNum 1 CV_32F);
Mat measurement = Mat::zeros(measureNum 1 CV_32F);
Rect trackWindow;
RotatedRect trackBox;
float dx dy;
int hsize = 255;
float hranges[] = { 0255 };
const float* phranges = hranges;
int hsize2 = 255;
float hranges2[] = { 0255 };
const float* phranges2 = hranges;
Mat hsv hue mask hist hist2 histimg = Mat::zeros(200 320 CV_8UC3) histimg2 = Mat::zeros(200 320 CV_8UC3) backproj;
float Threshold = 0.6;
bool paused = false;
namedWindow(“Histogram“ 0);
namedWindow(“CamShift Demo“ 0);
setMouseCallback(“CamShift Demo“ onMouse 0);
createTrackbar(“Vmin“ “CamShift Demo“ &vmin 256 0);
createTrackbar(“Vmax“ “CamShift Demo“ &vmax 256 0);
createTrackbar(“Smin“ “CamShift Demo“ &smin 256 0);
KF.transitionMatrix = (Mat_(6 6) << 1 0 T 0 (T*T) / 2 0
0 1 0 T 0 (T*T) / 2
0 0 1 0 T 0
0 0 0 1 0 T
0 0 0 0 1 0
0 0 0 0 0 1);
setIdentity(KF.measurementMatrix);
setIdentity(KF.processNoiseCov Scalar::all(1e-5));
setIdentity(KF.measurementNoiseCov Scalar::all(1e-1));
setIdentity(KF.errorCovPost Scalar::all(1));
KF.statePost.at(0) = selection.x + selection.width / 2;
KF.statePost.at(1) = selection.y + selection.heig
- 上一篇:特殊矩阵和压缩存储
- 下一篇:人工智能实验报告以及C语言源程序
评论
共有 条评论