 |
OpenCV
4.2.0
Open Source Computer Vision
|
An example using the mean-shift tracking algorithm
#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);
}
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;
if (parser.has("help"))
{
help();
return 0;
}
int camNum = parser.get<int>(0);
{
help();
cout << "***Could not initialize capturing...***\n";
cout << "Current parameter's value: \n";
parser.printMessage();
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++ )
{
int val = saturate_cast<int>(hist.
at<
float>(i)*histimg.
rows/255);
}
}
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;
}
Vec< uchar, 3 > Vec3b
Definition: matx.hpp:401
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition: mat.hpp:2086
static Scalar_< double > all(double v0)
returns a scalar with all elements set to v0
The class defining termination criteria for iterative algorithms.
Definition: types.hpp:852
Rect2i Rect
Definition: types.hpp:462
flag
Definition: base.hpp:207
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 cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
convert RGB/BGR to HSV (hue saturation value), color conversions
Definition: imgproc.hpp:585
static MatExpr zeros(int rows, int cols, int type)
Returns a zero array of the specified size and type.
void inRange(InputArray src, InputArray lowerb, InputArray upperb, OutputArray dst)
Checks if array elements lie between the elements of two other arrays.
uchar saturate_cast< uchar >(schar v)
Definition: saturate.hpp:100
_Tp & at(int i0=0)
Returns a reference to the specified array element.
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.
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:608
int waitKey(int delay=0)
Waits for a pressed key.
the maximum number of iterations or elements to compute
Definition: types.hpp:860
the desired accuracy or change in parameters at which the iterative algorithm stops
Definition: types.hpp:862
_Tp y
y coordinate of the point
Definition: types.hpp:187
_Tp x
x coordinate of the point
Definition: types.hpp:186
void namedWindow(const String &winname, int flags=WINDOW_AUTOSIZE)
Creates a window.
indicates that the left mouse button is pressed.
Definition: highgui.hpp:207
void destroyWindow(const String &winname)
Destroys the specified window.
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.
backward conversions to RGB/BGR
Definition: imgproc.hpp:596
Template class for short numerical vectors, a partial case of Matx.
Definition: matx.hpp:332
int cols
Definition: mat.hpp:2086
#define CV_8UC3
Definition: interface.h:90
MatSize size
Definition: mat.hpp:2108
#define MIN(a, b)
Definition: cvdef.h:453
_Tp y
y coordinate of the top-left corner
Definition: types.hpp:454
static uchar abs(uchar a)
Definition: cvstd.hpp:66
virtual bool isOpened() const
Returns true if video capturing has been initialized already.
The class represents rotated (i.e. not up-right) rectangles on a plane.
Definition: types.hpp:503
Template class for 2D rectangles.
Definition: types.hpp:420
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
_Tp width
width of the rectangle
Definition: types.hpp:455
_Tp area() const
area (width*height) of the rectangle
RotatedRect CamShift(InputArray probImage, Rect &window, TermCriteria criteria)
Finds an object center, size, and orientation.
Scalar_< double > Scalar
Definition: types.hpp:669
_Tp height
height of the rectangle
Definition: types.hpp:456
void setMouseCallback(const String &winname, MouseCallback onMouse, void *userdata=0)
Sets mouse handler for the specified window.
Point2i Point
Definition: types.hpp:194
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.
n-dimensional dense array class
Definition: mat.hpp:791
void copyTo(OutputArray m) const
Copies the matrix to another one.
Designed for command line parsing.
Definition: utility.hpp:796
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.
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:51
#define MAX(a, b)
Definition: cvdef.h:457
void create(int rows, int cols, int type)
Allocates new array data if needed.
indicates that left mouse button is released.
Definition: highgui.hpp:210
void bitwise_not(InputArray src, OutputArray dst, InputArray mask=noArray())
Inverts every bit of an array.
GMat mask(const GMat &src, const GMat &mask)
Applies a mask to a matrix.
static Vec< _Tp, cn > normalize(const Vec< _Tp, cn > &v)
int depth() const
Returns the depth of a matrix element.
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.
Definition: imgproc.hpp:544
_Tp x
x coordinate of the top-left corner
Definition: types.hpp:453
antialiased line
Definition: imgproc.hpp:807
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.