这是基于颜色识别的物体追踪
不废话
直接看代码
这是Opencv3的代码
//---------------------------------【头文件、命名空间包含部分】----------------------------
// 描述:包含程序所使用的头文件和命名空间
//-------------------------------------------------------------------------------------------------
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <iostream>
#include <ctype.h> using namespace cv;
using namespace std; //-----------------------------------【全局变量声明】-----------------------------------------
// 描述:声明全局变量
//-------------------------------------------------------------------------------------------------
Mat image;
bool backprojMode = false;
bool selectObject = false;
int trackObject = ;
bool showHist = true;
Point origin;
Rect selection;
int vmin = , vmax = , smin = ; //--------------------------------【onMouse( )回调函数】------------------------------------
// 描述:鼠标操作回调
//-------------------------------------------------------------------------------------------------
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(, , image.cols, image.rows);
} switch( event )
{
//此句代码的OpenCV2版为:
//case CV_EVENT_LBUTTONDOWN:
//此句代码的OpenCV3版为:
case EVENT_LBUTTONDOWN:
origin = Point(x,y);
selection = Rect(x,y,,);
selectObject = true;
break;
//此句代码的OpenCV2版为:
//case CV_EVENT_LBUTTONUP:
//此句代码的OpenCV3版为:
case EVENT_LBUTTONUP:
selectObject = false;
if( selection.width > && selection.height > )
trackObject = -;
break;
}
} //--------------------------------【help( )函数】----------------------------------------------
// 描述:输出帮助信息
//-------------------------------------------------------------------------------------------------
static void ShowHelpText()
{
cout << "\n\n\t此Demo显示了基于均值漂移的追踪(tracking)技术\n"
"\t请用鼠标框选一个有颜色的物体,对它进行追踪操作\n"; cout << "\n\n\t操作说明: \n"
"\t\t用鼠标框选对象来初始化跟踪\n"
"\t\tESC - 退出程序\n"
"\t\tc - 停止追踪\n"
"\t\tb - 开/关-投影视图\n"
"\t\th - 显示/隐藏-对象直方图\n"
"\t\tp - 暂停视频\n";
} const char* keys =
{
"{1| | 0 | camera number}"
}; //-----------------------------------【main( )函数】--------------------------------------------
// 描述:控制台应用程序的入口函数,我们的程序从这里开始
//-------------------------------------------------------------------------------------------------
int main( int argc, const char** argv )
{
ShowHelpText(); VideoCapture cap;
Rect trackWindow;
int hsize = ;
float hranges[] = {,};
const float* phranges = hranges; cap.open(); if( !cap.isOpened() )
{
cout << "不能初始化摄像头\n";
} namedWindow( "Histogram", );
namedWindow( "CamShift Demo", );
setMouseCallback( "CamShift Demo", onMouse, );
createTrackbar( "Vmin", "CamShift Demo", &vmin, , );
createTrackbar( "Vmax", "CamShift Demo", &vmax, , );
createTrackbar( "Smin", "CamShift Demo", &smin, , ); Mat frame, hsv, hue, mask, hist, histimg = Mat::zeros(, , CV_8UC3), backproj;
bool paused = false; for(;;)
{
if( !paused )
{
cap >> frame;
if( frame.empty() )
break;
} frame.copyTo(image); if( !paused )
{
cvtColor(image, hsv, COLOR_BGR2HSV); if( trackObject )
{
int _vmin = vmin, _vmax = vmax; inRange(hsv, Scalar(, smin, MIN(_vmin,_vmax)),
Scalar(, , MAX(_vmin, _vmax)), mask);
int ch[] = {, };
hue.create(hsv.size(), hsv.depth());
mixChannels(&hsv, , &hue, , ch, ); if( trackObject < )
{
Mat roi(hue, selection), maskroi(mask, selection);
calcHist(&roi, , , maskroi, hist, , &hsize, &phranges);
//此句代码的OpenCV3版为:
normalize(hist, hist, , , NORM_MINMAX);
//此句代码的OpenCV2版为:
//normalize(hist, hist, 0, 255, CV_MINMAX); trackWindow = selection;
trackObject = ; histimg = Scalar::all();
int binW = histimg.cols / hsize;
Mat buf(, hsize, CV_8UC3);
for( int i = ; i < hsize; i++ )
buf.at<Vec3b>(i) = Vec3b(saturate_cast<uchar>(i*./hsize), , ); //此句代码的OpenCV3版为:
cvtColor(buf, buf, COLOR_HSV2BGR);
//此句代码的OpenCV2版为:
//cvtColor(buf, buf, CV_HSV2BGR); for( int i = ; i < hsize; i++ )
{
int val = saturate_cast<int>(hist.at<float>(i)*histimg.rows/);
rectangle( histimg, Point(i*binW,histimg.rows),
Point((i+)*binW,histimg.rows - val),
Scalar(buf.at<Vec3b>(i)), -, );
}
} calcBackProject(&hue, , , hist, backproj, &phranges);
backproj &= mask;
RotatedRect trackBox = CamShift(backproj, trackWindow, //此句代码的OpenCV3版为:
TermCriteria( TermCriteria::EPS | TermCriteria::COUNT, , ));
//此句代码的OpenCV2版为:
//TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 )); if( trackWindow.area() <= )
{
int cols = backproj.cols, rows = backproj.rows, r = (MIN(cols, rows) + )/;
trackWindow = Rect(trackWindow.x - r, trackWindow.y - r,
trackWindow.x + r, trackWindow.y + r) &
Rect(, , cols, rows);
} if( backprojMode )
cvtColor( backproj, image, COLOR_GRAY2BGR ); //此句代码的OpenCV3版为:
ellipse( image, trackBox, Scalar(,,), , LINE_AA );
//此句代码的OpenCV2版为:
//ellipse( image, trackBox, Scalar(0,0,255), 3, CV_AA ); }
}
else if( trackObject < )
paused = false; if( selectObject && selection.width > && selection.height > )
{
Mat roi(image, selection);
bitwise_not(roi, roi);
} imshow( "CamShift Demo", image );
imshow( "Histogram", histimg ); char c = (char)waitKey();
if( c == )
break;
switch(c)
{
case 'b':
backprojMode = !backprojMode;
break;
case 'c':
trackObject = ;
histimg = Scalar::all();
break;
case 'h':
showHist = !showHist;
if( !showHist )
destroyWindow( "Histogram" );
else
namedWindow( "Histogram", );
break;
case 'p':
paused = !paused;
break;
default:
;
}
} return ;
}
拜拜啦 兄弟们