#include <iostream>
#include <ctype.h>
Mat image;
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);
selection.width =
std::abs(x - origin.x);
selection.height =
std::abs(y - origin.y);
selection &=
Rect(0, 0, image.cols, image.rows);
}
switch( event )
{
selection =
Rect(x,y,0,0);
selectObject = true;
break;
selectObject = false;
if( selection.width > 0 && selection.height > 0 )
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(const char** argv)
{
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\t";
cout << argv[0] << " [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 )
{
VideoCapture cap;
int hsize = 16;
float hranges[] = {0,180};
const float* phranges = hranges;
CommandLineParser parser(argc, argv, keys);
if (parser.has("help"))
{
help(argv);
return 0;
}
int camNum = parser.get<int>(0);
cap.open(camNum);
if( !cap.isOpened() )
{
help(argv);
cout << "***Could not initialize capturing...***\n";
cout << "Current parameter's value: \n";
parser.printMessage();
return -1;
}
cout << hot_keys;
Mat frame, hsv, hue,
mask, hist, histimg = Mat::zeros(200, 320,
CV_8UC3), backproj;
bool paused = false;
for(;;)
{
if( !paused )
{
cap >> frame;
if( frame.empty() )
break;
}
frame.copyTo(image);
if( !paused )
{
if( trackObject )
{
int _vmin = vmin, _vmax = vmax;
int ch[] = {0, 0};
hue.create(hsv.size(), hsv.depth());
if( trackObject < 0 )
{
Mat roi(hue, selection), maskroi(
mask, selection);
calcHist(&roi, 1, 0, maskroi, hist, 1, &hsize, &phranges);
trackWindow = selection;
trackObject = 1;
histimg = Scalar::all(0);
int binW = histimg.cols / hsize;
for( int i = 0; i < hsize; i++ )
for( int i = 0; i < hsize; i++ )
{
int val = saturate_cast<int>(hist.at<float>(i)*histimg.rows/255);
Point((i+1)*binW,histimg.rows - val),
}
}
RotatedRect trackBox =
CamShift(backproj, trackWindow,
TermCriteria( TermCriteria::EPS | TermCriteria::COUNT, 10, 1 ));
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;
histimg = Scalar::all(0);
break;
case 'h':
showHist = !showHist;
if( !showHist )
else
break;
case 'p':
paused = !paused;
break;
default:
;
}
}
return 0;
}
_Tp x
x coordinate of the top-left corner
Definition: modules/core/include/opencv2/core/types.hpp:487
void bitwise_not(InputArray src, OutputArray dst, InputArray mask=noArray())
Inverts every bit of an array.
void inRange(InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst)
Checks if array elements lie between the elements of two other arrays.
void mixChannels(const Mat *src, size_t nsrcs, Mat *dst, size_t ndsts, const int *fromTo, size_t npairs)
Copies specified channels from input arrays to the specified channels of output arrays.
void normalize(InputArray src, InputOutputArray dst, double alpha=1, double beta=0, int norm_type=NORM_L2, int dtype=-1, InputArray mask=noArray())
Normalizes the norm or value range of an array.
@ NORM_MINMAX
flag
Definition: core/include/opencv2/core/base.hpp:207
Rect2i Rect
Definition: modules/core/include/opencv2/core/types.hpp:496
Point2i Point
Definition: modules/core/include/opencv2/core/types.hpp:209
Scalar_< double > Scalar
Definition: modules/core/include/opencv2/core/types.hpp:709
Vec< uchar, 3 > Vec3b
Definition: matx.hpp:441
#define CV_8UC3
Definition: core/include/opencv2/core/hal/interface.h:90
uchar saturate_cast< uchar >(schar v)
Definition: core/include/opencv2/core/saturate.hpp:101
#define MIN(a, b)
Definition: cvdef.h:514
#define MAX(a, b)
Definition: cvdef.h:518
GMat mask(const GMat &src, const GMat &mask)
Applies a mask to a matrix.
@ EVENT_LBUTTONUP
indicates that left mouse button is released.
Definition: highgui.hpp:171
@ EVENT_LBUTTONDOWN
indicates that the left mouse button is pressed.
Definition: highgui.hpp:168
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
void destroyWindow(const String &winname)
Destroys the specified window.
void setMouseCallback(const String &winname, MouseCallback onMouse, void *userdata=0)
Sets mouse handler for the specified window.
int createTrackbar(const String &trackbarname, const String &winname, int *value, int count, TrackbarCallback onChange=0, void *userdata=0)
Creates a trackbar and attaches it to the specified window.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Converts an image from one color space to another.
@ COLOR_HSV2BGR
backward conversions HSV to RGB/BGR with H range 0..180 if 8 bit image
Definition: imgproc/include/opencv2/imgproc.hpp:609
@ COLOR_GRAY2BGR
Definition: imgproc/include/opencv2/imgproc.hpp:557
@ COLOR_BGR2HSV
convert RGB/BGR to HSV (hue saturation value) with H range 0..180 if 8 bit image, color conversions
Definition: imgproc/include/opencv2/imgproc.hpp:598
void rectangle(InputOutputArray img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple, thick, or filled up-right rectangle.
void ellipse(InputOutputArray img, Point center, Size axes, double angle, double startAngle, double endAngle, const Scalar &color, int thickness=1, int lineType=LINE_8, int shift=0)
Draws a simple or thick elliptic arc or fills an ellipse sector.
@ LINE_AA
antialiased line
Definition: imgproc/include/opencv2/imgproc.hpp:894
void calcBackProject(const Mat *images, int nimages, const int *channels, InputArray hist, OutputArray backProject, const float **ranges, double scale=1, bool uniform=true)
Calculates the back projection of a histogram.
void calcHist(const Mat *images, int nimages, const int *channels, InputArray mask, OutputArray hist, int dims, const int *histSize, const float **ranges, bool uniform=true, bool accumulate=false)
Calculates a histogram of a set of arrays.
RotatedRect CamShift(InputArray probImage, Rect &window, TermCriteria criteria)
Finds an object center, size, and orientation.
int main(int argc, char *argv[])
Definition: highgui_qt.cpp:3
Definition: core/include/opencv2/core.hpp:107
static uchar abs(uchar a)
Definition: cvstd.hpp:66