5#include <iostream>
6#include <cstring>
7#include "samples_utility.hpp"
8
11
12
13void sobelExtractor(
const Mat img,
const Rect roi,
Mat& feat);
14
15int main(
int argc,
char** argv ){
16
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
30
38
39
43
45 tracker->setFeatureExtractor(sobelExtractor);
47
48
49 std::string video = argv[1];
51
52
53 cap >> frame;
55
56
57 if(roi.width==0 || roi.height==0)
58 return 0;
59
60
61 tracker->init(frame,roi);
62
63
64 printf("Start the tracking process, press ESC to quit.\n");
65 for ( ;; ){
66
67 cap >> frame;
68
69
70 if(frame.rows==0 || frame.cols==0)
71 break;
72
73
74 tracker->update(frame,roi);
75
76
78
79
81
82
84 }
85
86 return 0;
87}
88
89void sobelExtractor(
const Mat img,
const Rect roi,
Mat& feat){
93
95
96 if(roi.x<0){region.x=0;region.width+=roi.x;}
97 if(roi.y<0){region.y=0;region.height+=roi.y;}
98 if(roi.x+roi.width>img.
cols)region.width=img.
cols-roi.x;
99 if(roi.y+roi.height>img.
rows)region.height=img.
rows-roi.y;
100 if(region.width>img.
cols)region.width=img.
cols;
101 if(region.height>img.
rows)region.height=img.
rows;
103
104 patch=img(region).
clone();
106
108
109 int addTop,addBottom, addLeft, addRight;
110 addTop=region.y-roi.y;
111 addBottom=(roi.height+roi.y>img.
rows?roi.height+roi.y-img.
rows:0);
112 addLeft=region.x-roi.x;
113 addRight=(roi.width+roi.x>img.
cols?roi.width+roi.x-img.
cols:0);
114
117
121
124
126 feat=feat/255.0-0.5;
128}
n-dimensional dense array class
Definition mat.hpp:812
CV_NODISCARD_STD Mat clone() const
Creates a full copy of the array and the underlying data.
int cols
Definition mat.hpp:2138
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition mat.hpp:2138
@ CN
Definition tracking.hpp:113
@ GRAY
Definition tracking.hpp:112
static Ptr< TrackerKCF > create(const TrackerKCF::Params ¶meters=TrackerKCF::Params())
Create KCF tracker instance.
Class for video capturing from video files, image sequences or cameras.
Definition videoio.hpp:731
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.
void merge(const Mat *mv, size_t count, OutputArray dst)
Creates one multi-channel array out of several single-channel ones.
@ BORDER_REPLICATE
aaaaaa|abcdefgh|hhhhhhh
Definition base.hpp:270
Rect2i Rect
Definition types.hpp:489
std::shared_ptr< _Tp > Ptr
Definition cvstd_wrapper.hpp:23
Scalar_< double > Scalar
Definition types.hpp:702
#define CV_32F
Definition interface.h:78
void imshow(const String &winname, InputArray mat)
Displays an image in the specified window.
int waitKey(int delay=0)
Waits for a pressed key.
Rect selectROI(const String &windowName, InputArray img, bool showCrosshair=true, bool fromCenter=false, bool printNotice=true)
Allows users to select a ROI on the given image.
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0)
Converts an image from one color space to another.
@ COLOR_BGR2GRAY
convert between RGB/BGR and grayscale, color conversions
Definition imgproc.hpp:555
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 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.
int main(int argc, char *argv[])
Definition highgui_qt.cpp:3
"black box" representation of the file storage associated with a file on disk.
Definition core.hpp:102
Definition tracking.hpp:118
bool compress_feature
activate the pca method to compress the features
Definition tracking.hpp:130
int desc_pca
compressed descriptors of TrackerKCF::MODE
Definition tracking.hpp:133
int desc_npca
non-compressed descriptors of TrackerKCF::MODE
Definition tracking.hpp:134
int compressed_size
feature size after compression
Definition tracking.hpp:132
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.