#include <opencv2/core/utility.hpp>
#include "opencv2/video/tracking.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/videoio.hpp"
#include "opencv2/highgui.hpp"
#include <iostream>
#include <ctype.h>
bool backprojMode = false;
bool selectObject = false;
int trackObject = 0;
bool showHist = true;
int vmin = 10, vmax = 256, smin = 30;
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);
}
switch( event )
{
selection =
Rect(x,y,0,0);
selectObject = true;
break;
selectObject = false;
trackObject = -1;
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 << "\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"
"Usage: \n"
" ./camshiftdemo [camera number]\n";
cout << hot_keys;
}
const char* keys =
{
"{help h | | show help message}{@camera_number| 0 | camera number}"
};
int main( int argc, const char** argv )
{
int hsize = 16;
float hranges[] = {0,180};
const float* phranges = hranges;
{
help();
return 0;
}
int camNum = parser.
get<
int>(0);
{
help();
cout << "***Could not initialize capturing...***\n";
cout << "Current parameter's value: \n";
return -1;
}
cout << hot_keys;
bool paused = false;
for(;;)
{
if( !paused )
{
cap >> frame;
if( frame.empty() )
break;
}
if( !paused )
{
if( trackObject )
{
int _vmin = vmin, _vmax = vmax;
int ch[] = {0, 0};
if( trackObject < 0 )
{
Mat roi(hue, selection), maskroi(mask, selection);
calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
trackWindow = selection;
trackObject = 1;
int binW = histimg.
cols / hsize;
for( int i = 0; i < hsize; i++ )
for( int i = 0; i < hsize; i++ )
{
}
}
if( trackWindow.
area() <= 1 )
{
int cols = backproj.cols, rows = backproj.rows, r = (
MIN(cols, rows) + 5)/6;
trackWindow =
Rect(trackWindow.
x - r, trackWindow.
y - r,
trackWindow.
x + r, trackWindow.
y + r) &
}
if( backprojMode )
}
}
else if( trackObject < 0 )
paused = false;
if( selectObject && selection.
width > 0 && selection.
height > 0 )
{
Mat roi(image, selection);
}
imshow(
"CamShift Demo", image );
imshow(
"Histogram", histimg );
if( c == 27 )
break;
switch(c)
{
case 'b':
backprojMode = !backprojMode;
break;
case 'c':
trackObject = 0;
break;
case 'h':
showHist = !showHist;
if( !showHist )
else
break;
case 'p':
paused = !paused;
break;
default:
;
}
}
return 0;
}