資源簡介
基于camshift跟蹤算法,使用鼠標(biāo)框選多個目標(biāo)進行跟蹤
代碼片段和文件信息
#include?
#include?“opencv2/video/tracking.hpp“
#include?“opencv2/imgproc.hpp“
#include?“opencv2/videoio.hpp“
#include?“opencv2/highgui.hpp“
#include?
#include?
#include?“Car.h“
using?namespace?cv;
using?namespace?std;
Mat?image;
bool?backprojMode?=?false;
bool?selectobject?=?false;
int?trackobject?=?0;
int?count1?=?0;
bool?showHist?=?true;
Point?origin;
Rect?selection?selection2;
int?vmin?=?10?vmax?=?256?smin?=?30;
Car*?car;
//?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)
{
if?(count1?==?1)?{
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);
}
if?(count1?==?2)?{
selection2.x?=?MIN(x?origin.x);
selection2.y?=?MIN(y?origin.y);
selection2.width?=?std::abs(x?-?origin.x);
selection2.height?=?std::abs(y?-?origin.y);
//selection2?&=?Rect(0?0?image.cols?image.rows);
}
}
switch?(event)
{
case?EVENT_LBUTTONDOWN:
origin?=?Point(x?y);
if?(count1?==?0)
selection?=?Rect(x?y?0?0);
if?(count1?==?1)
selection2?=?Rect(x?y?0?0);
selectobject?=?true;
count1++;
break;
case?EVENT_LBUTTONUP:
selectobject?=?false;
if?(selection.width?>?0?&&?selection.height?>?0?&&?count1?==?2)
trackobject?=?-1;???//?Set?up?CAMShift?properties?in?main()?loop
break;
}
}
string?hot_keys?=
“\n\nHot?keys:?\n“
“\tESC?-?quit?the?program\n“
“\tc?-?stop?the?tracking\n“
“\tb?-?switch?to/from?backprojection?view\n“
“\th?-?show/hide?object?histogram\n“
“\tp?-?pause?video\n“
“To?initialize?tracking?select?the?object?with?mouse\n“;
static?void?help()
{
cout?<“a?little?demo?for?the?car?detection“;
cout?<}
const?char*?keys?=
{
“{help?h?|?|?show?help?message}{@camera_number|?0?|?camera?number}“
};
int?main(int?argc?const?char**?argv)
{
VideoCapture?cap;
VideoWriter?video;
Rect?trackWindow;
int?hsize?=?16;
float?hranges[]?=?{?0180?};
const?float*?phranges?=?hranges;
CommandLineParser?parser(argc?argv?keys);
if?(parser.has(“help“))
{
help();
return?0;
}
int?camNum?=?parser.get(0);
cap.open(camNum);
if?(!cap.isOpened())
{
help();
cout?<“***Could?not?initialize?capturing...***\n“;
cout?<“Current?parameter‘s?value:?\n“;
parser.printMessage();
return?-1;
}
Size?S?=?Size((int)cap.get(CAP_PROP_frame_WIDTH)????//?Acquire?input?size
(int)cap.ge
評論
共有 條評論