#include <iostream>
#include <ctype.h>
using namespace std;
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);
}
switch( event )
{
case EVENT_LBUTTONDOWN:
selection =
Rect(x,y,0,0);
selectObject = true;
break;
case EVENT_LBUTTONUP:
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(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 )
{
int hsize = 16;
float hranges[] = {0,180};
const float* phranges = hranges;
if (parser.has("help"))
{
help(argv);
return 0;
}
int camNum = parser.get<int>(0);
{
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;
}
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;
histimg = Scalar::all(0);
int binW = histimg.
cols / hsize;
for( int i = 0; i < hsize; i++ )
buf.at<
Vec3b>(i) =
Vec3b(saturate_cast<uchar>(i*180./hsize), 255, 255);
for( int i = 0; i < hsize; i++ )
{
int val = saturate_cast<int>(hist.
at<
float>(i)*histimg.
rows/255);
}
}
backproj &= mask;
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 )
cvtColor( backproj, image, COLOR_GRAY2BGR );
}
}
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;
}
Designed for command line parsing.
Definition utility.hpp:890
n-dimensional dense array class
Definition mat.hpp:950
MatSize size
Definition mat.hpp:2447
void copyTo(OutputArray m) const
Copies the matrix to another one.
void create(int rows, int cols, int type)
Allocates new array data if needed.
int depth() const
Returns the depth of a matrix element.
_Tp & at(int i0=0)
Returns a reference to the specified array element.
int cols
Definition mat.hpp:2424
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition mat.hpp:2424
_Tp y
y coordinate of the point
Definition types.hpp:202
_Tp x
x coordinate of the point
Definition types.hpp:201
Template class for 2D rectangles.
Definition types.hpp:447
_Tp area() const
area (width*height) of the rectangle
_Tp x
x coordinate of the top-left corner
Definition types.hpp:490
_Tp y
y coordinate of the top-left corner
Definition types.hpp:491
_Tp width
width of the rectangle
Definition types.hpp:492
_Tp height
height of the rectangle
Definition types.hpp:493
The class represents rotated (i.e. not up-right) rectangles on a plane.
Definition types.hpp:541
The class defining termination criteria for iterative algorithms.
Definition types.hpp:896
Template class for short numerical vectors, a partial case of Matx.
Definition matx.hpp:379
Class for video capturing from video files, image sequences or cameras.
Definition videoio.hpp:727
virtual bool open(const String &filename, int apiPreference=CAP_ANY)
Opens a video file or a capturing device or an IP video stream for video capturing.
virtual bool isOpened() const
Returns true if video capturing has been initialized already.
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.
#define CV_8UC3
Definition interface.h:101
#define MIN(a, b)
Definition cvdef.h:527
#define MAX(a, b)
Definition cvdef.h:531
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.
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.
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