5 #include <iostream>
6 #include <cstring>
7 #include "samples_utility.hpp"
8
9 using namespace std;
11
12 // prototype of the functino for feature extractor
13 void sobelExtractor(
const Mat img,
const Rect roi,
Mat& feat);
14
15 int main( int argc, char** argv ){
16 // show help
17 if(argc<2){
18 cout<<
19 " Usage: tracker <video_name>\n"
20 " examples:\n"
21 " example_tracking_kcf Bolt/img/%04d.jpg\n"
22 " example_tracking_kcf faceocc2.webm\n"
23 << endl;
24 return 0;
25 }
26
27 // declares all required variables
30
33 param.
desc_pca = TrackerKCF::GRAY | TrackerKCF::CN;
38
39 // create a tracker object
43
45 tracker->setFeatureExtractor(sobelExtractor);
47
48 // set input video
49 std::string video = argv[1];
51
52 // get bounding box
53 cap >> frame;
55
56 //quit if ROI was not selected
58 return 0;
59
60 // initialize the tracker
61 tracker->init(frame,roi);
62
63 // perform the tracking process
64 printf("Start the tracking process, press ESC to quit.\n");
65 for ( ;; ){
66 // get frame from the video
67 cap >> frame;
68
69 // stop the program if no more images
70 if(frame.rows==0 || frame.cols==0)
71 break;
72
73 // update the tracking result
74 tracker->update(frame,roi);
75
76 // draw the tracked object
78
79 // show image with the tracked object
81
82 //quit on ESC button
84 }
85
86 return 0;
87 }
88
89 void sobelExtractor(
const Mat img,
const Rect roi,
Mat& feat){
93
95 // extract patch inside the image
96 if(roi.
x<0){region.
x=0;region.
width+=roi.
x;}
97 if(roi.
y<0){region.
y=0;region.
height+=roi.
y;}
103
104 patch=img(region).
clone();
106
108 // add some padding to compensate when the patch is outside image border
109 int addTop,addBottom, addLeft, addRight;
110 addTop=region.
y-roi.
y;
112 addLeft=region.
x-roi.
x;
114
117
121
124
126 feat=feat/255.0-0.5; // normalize to range -0.5 .. 0.5
128 }
Scalar_< double > Scalar
Definition: types.hpp:691
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc.hpp:551
aaaaaa|abcdefgh|hhhhhhh
Definition: base.hpp:270
_Tp x
x coordinate of the top-left corner
Definition: types.hpp:475
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions ...
Definition: mat.hpp:2116
int desc_pca
compressed descriptors of TrackerKCF::MODE
Definition: tracking.hpp:133
std::shared_ptr< _Tp > Ptr
Definition: cvstd_wrapper.hpp:23
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 imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
"black box" representation of the file storage associated with a file on disk.
Definition: affine.hpp:51
Rect selectROI(const String &windowName, InputArray img, bool showCrosshair=true, bool fromCenter=false)
Allows users to select a ROI on the given image.
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:682
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
_Tp y
y coordinate of the top-left corner
Definition: types.hpp:476
int desc_npca
non-compressed descriptors of TrackerKCF::MODE
Definition: tracking.hpp:134
void Sobel(InputArray src, OutputArray dst, int ddepth, int dx, int dy, int ksize=3, double scale=1, double delta=0, int borderType=BORDER_DEFAULT)
Calculates the first, second, third, or mixed image derivatives using an extended Sobel operator...
CV_NODISCARD_STD Mat clone() const
Creates a full copy of the array and the underlying data.
void copyMakeBorder(InputArray src, OutputArray dst, int top, int bottom, int left, int right, int borderType, const Scalar &value=Scalar())
Forms a border around an image.
#define CV_32F
Definition: interface.h:78
_Tp height
height of the rectangle
Definition: types.hpp:478
int cols
Definition: mat.hpp:2116
bool compress_feature
activate the pca method to compress the features
Definition: tracking.hpp:130
Template class for 2D rectangles.
Definition: types.hpp:438
void merge(const Mat *mv, size_t count, OutputArray dst)
Creates one multi-channel array out of several single-channel ones.
_Tp width
width of the rectangle
Definition: types.hpp:477
int compressed_size
feature size after compression
Definition: tracking.hpp:132
n-dimensional dense array class
Definition: mat.hpp:810
Definition: tracking.hpp:117
int waitKey(int delay=0)
Waits for a pressed key.
This part explains how to set custom parameters and use your own feature-extractor function for the CN tracker. If you need a more detailed information to use cv::Tracker, please refer to Introduction to OpenCV Tracker.