2011-05-21 20:32:34 +02:00
|
|
|
#include "opencv2/video/tracking.hpp"
|
|
|
|
#include "opencv2/imgproc/imgproc.hpp"
|
|
|
|
#include "opencv2/highgui/highgui.hpp"
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2011-06-09 14:01:47 +02:00
|
|
|
#include <iostream>
|
2010-05-11 19:44:00 +02:00
|
|
|
#include <ctype.h>
|
|
|
|
|
2010-11-28 20:41:55 +01:00
|
|
|
using namespace cv;
|
|
|
|
using namespace std;
|
|
|
|
|
2010-11-30 01:19:34 +01:00
|
|
|
void help()
|
|
|
|
{
|
2011-06-09 14:01:47 +02:00
|
|
|
cout << "\nThis is a demo that shows mean-shift based tracking\n"
|
|
|
|
<< "You select a color objects such as your face and it tracks it.\n"
|
|
|
|
<< "This reads from video camera (0 by default, or the camera number the user enters\n"
|
|
|
|
<< "Call:\n"
|
|
|
|
<< "\n./camshiftdemo [camera number]"
|
|
|
|
<< "\n" << endl;
|
|
|
|
|
|
|
|
cout << "\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"
|
|
|
|
"To initialize tracking, select the object with mouse\n" << endl;
|
2010-11-30 01:19:34 +01:00
|
|
|
}
|
|
|
|
|
2010-11-28 20:41:55 +01:00
|
|
|
Mat image;
|
|
|
|
|
|
|
|
bool backprojMode = false;
|
|
|
|
bool selectObject = false;
|
|
|
|
int trackObject = 0;
|
|
|
|
bool showHist = true;
|
|
|
|
Point origin;
|
|
|
|
Rect selection;
|
2010-05-11 19:44:00 +02:00
|
|
|
int vmin = 10, vmax = 256, smin = 30;
|
|
|
|
|
2010-12-05 02:19:36 +01:00
|
|
|
void onMouse( int event, int x, int y, int, void* )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2010-11-28 00:15:16 +01:00
|
|
|
if( selectObject )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2010-11-28 00:15:16 +01:00
|
|
|
selection.x = MIN(x, origin.x);
|
|
|
|
selection.y = MIN(y, origin.y);
|
2010-11-28 20:41:55 +01:00
|
|
|
selection.width = std::abs(x - origin.x);
|
|
|
|
selection.height = std::abs(y - origin.y);
|
|
|
|
|
|
|
|
selection &= Rect(0, 0, image.cols, image.rows);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch( event )
|
|
|
|
{
|
|
|
|
case CV_EVENT_LBUTTONDOWN:
|
2010-11-28 00:15:16 +01:00
|
|
|
origin = Point(x,y);
|
|
|
|
selection = Rect(x,y,0,0);
|
|
|
|
selectObject = true;
|
2010-05-11 19:44:00 +02:00
|
|
|
break;
|
|
|
|
case CV_EVENT_LBUTTONUP:
|
2010-11-28 00:15:16 +01:00
|
|
|
selectObject = false;
|
2010-05-11 19:44:00 +02:00
|
|
|
if( selection.width > 0 && selection.height > 0 )
|
2010-11-28 00:15:16 +01:00
|
|
|
trackObject = -1;
|
2010-05-11 19:44:00 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-30 01:19:34 +01:00
|
|
|
|
|
|
|
|
2011-06-09 14:01:47 +02:00
|
|
|
int main( int argc, char** argv )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2010-11-28 20:41:55 +01:00
|
|
|
VideoCapture cap;
|
|
|
|
Rect trackWindow;
|
|
|
|
RotatedRect trackBox;
|
|
|
|
int hsize = 16;
|
|
|
|
float hranges[] = {0,180};
|
|
|
|
const float* phranges = hranges;
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2011-06-09 14:01:47 +02:00
|
|
|
if( argc == 1 || (argc == 2 && strlen(argv[1]) == 1 && isdigit(argv[1][0])))
|
|
|
|
cap.open(argc == 2 ? argv[1][0] - '0' : 0);
|
|
|
|
else if( argc == 2 )
|
|
|
|
cap.open(argv[1]);
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2010-11-28 20:41:55 +01:00
|
|
|
if( !cap.isOpened() )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2010-11-30 01:19:34 +01:00
|
|
|
help();
|
2011-06-09 14:01:47 +02:00
|
|
|
cout << "***Could not initialize capturing...***\n";
|
2010-11-28 20:41:55 +01:00
|
|
|
return 0;
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
2011-06-09 14:01:47 +02:00
|
|
|
help();
|
|
|
|
|
2010-11-28 20:41:55 +01:00
|
|
|
namedWindow( "Histogram", 1 );
|
|
|
|
namedWindow( "CamShift Demo", 1 );
|
|
|
|
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 );
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2010-11-28 20:41:55 +01:00
|
|
|
Mat hsv, hue, mask, hist, histimg = Mat::zeros(200, 320, CV_8UC3), backproj;
|
|
|
|
|
2010-05-11 19:44:00 +02:00
|
|
|
for(;;)
|
|
|
|
{
|
2010-11-28 20:41:55 +01:00
|
|
|
Mat frame;
|
|
|
|
cap >> frame;
|
|
|
|
if( frame.empty() )
|
2010-05-11 19:44:00 +02:00
|
|
|
break;
|
|
|
|
|
2010-11-28 20:41:55 +01:00
|
|
|
frame.copyTo(image);
|
|
|
|
cvtColor(image, hsv, CV_BGR2HSV);
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2010-11-28 20:41:55 +01:00
|
|
|
if( trackObject )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
|
|
|
int _vmin = vmin, _vmax = vmax;
|
|
|
|
|
2010-11-28 20:41:55 +01:00
|
|
|
inRange(hsv, Scalar(0, smin, MIN(_vmin,_vmax)),
|
|
|
|
Scalar(180, 256, MAX(_vmin, _vmax)), mask);
|
|
|
|
int ch[] = {0, 0};
|
|
|
|
hue.create(hsv.size(), hsv.depth());
|
|
|
|
mixChannels(&hsv, 1, &hue, 1, ch, 1);
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2010-11-28 20:41:55 +01:00
|
|
|
if( trackObject < 0 )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2010-11-28 20:41:55 +01:00
|
|
|
Mat roi(hue, selection), maskroi(mask, selection);
|
|
|
|
calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
|
|
|
|
normalize(hist, hist, 0, 255, CV_MINMAX);
|
|
|
|
|
|
|
|
trackWindow = selection;
|
|
|
|
trackObject = 1;
|
|
|
|
|
|
|
|
histimg = Scalar::all(0);
|
|
|
|
int binW = histimg.cols / hsize;
|
|
|
|
Mat buf(1, hsize, CV_8UC3);
|
|
|
|
for( int i = 0; i < hsize; i++ )
|
|
|
|
buf.at<Vec3b>(i) = Vec3b(saturate_cast<uchar>(i*180./hsize), 255, 255);
|
|
|
|
cvtColor(buf, buf, CV_HSV2BGR);
|
|
|
|
|
|
|
|
for( int i = 0; i < hsize; i++ )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2010-11-28 20:41:55 +01:00
|
|
|
int val = saturate_cast<int>(hist.at<float>(i)*histimg.rows/255);
|
|
|
|
rectangle( histimg, Point(i*binW,histimg.rows),
|
|
|
|
Point((i+1)*binW,histimg.rows - val),
|
|
|
|
Scalar(buf.at<Vec3b>(i)), -1, 8 );
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-28 20:41:55 +01:00
|
|
|
calcBackProject(&hue, 1, 0, hist, backproj, &phranges);
|
|
|
|
backproj &= mask;
|
|
|
|
RotatedRect trackBox = CamShift(backproj, trackWindow,
|
|
|
|
TermCriteria( CV_TERMCRIT_EPS | CV_TERMCRIT_ITER, 10, 1 ));
|
|
|
|
|
|
|
|
if( backprojMode )
|
|
|
|
cvtColor( backproj, image, CV_GRAY2BGR );
|
|
|
|
ellipse( image, trackBox, Scalar(0,0,255), 3, CV_AA );
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
2010-11-28 20:41:55 +01:00
|
|
|
if( selectObject && selection.width > 0 && selection.height > 0 )
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
2010-11-28 20:41:55 +01:00
|
|
|
Mat roi(image, selection);
|
|
|
|
bitwise_not(roi, roi);
|
2010-05-11 19:44:00 +02:00
|
|
|
}
|
|
|
|
|
2010-11-28 20:41:55 +01:00
|
|
|
imshow( "CamShift Demo", image );
|
|
|
|
imshow( "Histogram", histimg );
|
2010-05-11 19:44:00 +02:00
|
|
|
|
2010-11-28 20:41:55 +01:00
|
|
|
char c = (char)waitKey(10);
|
|
|
|
if( c == 27 )
|
2010-05-11 19:44:00 +02:00
|
|
|
break;
|
2010-11-28 20:41:55 +01:00
|
|
|
switch(c)
|
2010-05-11 19:44:00 +02:00
|
|
|
{
|
|
|
|
case 'b':
|
2010-11-28 20:41:55 +01:00
|
|
|
backprojMode = !backprojMode;
|
2010-05-11 19:44:00 +02:00
|
|
|
break;
|
|
|
|
case 'c':
|
2010-11-28 20:41:55 +01:00
|
|
|
trackObject = 0;
|
|
|
|
histimg = Scalar::all(0);
|
2010-05-11 19:44:00 +02:00
|
|
|
break;
|
|
|
|
case 'h':
|
2010-11-28 20:41:55 +01:00
|
|
|
showHist = !showHist;
|
|
|
|
if( !showHist )
|
|
|
|
destroyWindow( "Histogram" );
|
2010-05-11 19:44:00 +02:00
|
|
|
else
|
2010-11-28 20:41:55 +01:00
|
|
|
namedWindow( "Histogram", 1 );
|
2010-05-11 19:44:00 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|