Title: Color target tracking technology in OPENCV
Full code:
#include "opencv2/video/tracking.hpp" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp"
#include <iostream> #include <ctype.h> using namespace CV;
using namespace Std;
Mat image;
BOOL Backprojmode = false;
BOOL SelectObject = false;
int trackobject = 0;
bool Showhist = true;
Point origin;
Rect selection;
int vmin = ten, Vmax = n, 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) {Case Event_lbuttondown:origin = point (x, y);
Selection = Rect (x, y, 0, 0);
SelectObject = true;
Break
Case event_lbuttonup:selectobject = false;
if (selection.width > 0 && selection.height > 0) trackobject = 1;
Break }} static void Showhelptext () {cout << "\n\n\t This demo shows a tracking (tracking) technology based on mean drift \ n" "\ t use the mouse box to select a colored object and track it \ n"; cout << "\n\n\t: \ n" "\t\t initialize trace with the mouse box selected \ n" "\t\tesc-Exit program \ n" "\T\TC-stop tracking \ n" "\T\TB-on/off-projected view \ n" "\
T\th-Show/Hide-object histogram \ n "" \t\tp-pause video \ n "; } const char* keys = {"{1| | 0 |
Camera number} "};
int main (int argc, const char** argv) {showhelptext ();
Videocapture cap;
Rect Trackwindow;
int hsize = 16;
Float hranges[] = {0,180};
Const float* phranges = hranges;
Cap.open (0);
if (!cap.isopened ()) {cout << "cannot initialize camera \ n";
} namedwindow ("Histogram", 0);
Namedwindow ("Camshift Demo", 0);
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);
Mat frame, HSV, hue, Mask, hist, histimg = Mat::zeros ($, +, CV_8UC3), backproj;
BOOL paused = false;
for (;;) {if (!paused) {cap >> frame;
if (Frame.empty ()) break;
} frame.copyto (image);
if (!paused) {Cvtcolor (image, HSV, COLOR_BGR2HSV);
if (trackobject) {int _vmin = vmin, _vmax = Vmax;
InRange (HSV, scalar (0, smin, MIN (_vmin, _vmax)), scalar (n, N, MAX (_vmin, _vmax)), mask);
int ch[] = {0, 0};
Hue.create (Hsv.size (), hsv.depth ());
Mixchannels (&HSV, 1, &hue, 1, CH, 1);
if (Trackobject < 0) {Mat roi (hue, selection), Maskroi (mask, selection);
Calchist (&roi, 1, 0, Maskroi, hist, 1, &hsize, &phranges);
Normalize (hist, hist, 0, 255, Norm_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, 2
55);
Cvtcolor (buf, buf, COLOR_HSV2BGR); for (int i = 0; i < HSIze
i++) {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<ve
C3b> (i)),-1, 8);
}} calcbackproject (&hue, 1, 0, Hist, backproj, &phranges);
Backproj &= Mask; 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) & Rect
(0, 0, cols, rows);
} if (Backprojmode) Cvtcolor (backproj, image, COLOR_GRAY2BGR);
Ellipse (image, Trackbox, Scalar (0, 0, 255), 3, LINE_AA);
}} else if (Trackobject < 0) paused = false; if (SelectObject && selection.Width > 0 && selection.height > 0) {Mat ROI (image, selection);
Bitwise_not (ROI, ROI);
} imshow ("Camshift Demo", image);
Imshow ("Histogram", histimg);//histogram char C = (char) waitkey (10);
if (c = =) break;
Switch (c) {case ' B ': Backprojmode =!backprojmode;
Break
Case ' C ': trackobject = 0;
histimg = Scalar::all (0);
Break
Case ' h ': showhist =!showhist;
if (!showhist) DestroyWindow ("histogram");
else Namedwindow ("histogram", 1);
Break
Case ' P ': paused =!paused;
Break
Default:;
}} return 0; }
Operation effect: Cough, this is Bo master himself, not fond of light spray