7 #include "samples_utility.hpp" 13 void sobelExtractor(
const Mat img,
const Rect roi,
Mat& feat);
15 int main(
int argc,
char** argv ){
19 " Usage: tracker <video_name>\n" 21 " example_tracking_kcf Bolt/img/%04d.jpg\n" 22 " example_tracking_kcf faceocc2.webm\n" 33 param.
desc_pca = TrackerKCF::GRAY | TrackerKCF::CN;
45 tracker->setFeatureExtractor(sobelExtractor);
49 std::string video = argv[1];
61 tracker->init(frame,roi);
64 printf(
"Start the tracking process, press ESC to quit.\n");
70 if(frame.rows==0 || frame.cols==0)
74 tracker->update(frame,roi);
89 void sobelExtractor(
const Mat img,
const Rect roi,
Mat& feat){
96 if(roi.
x<0){region.
x=0;region.
width+=roi.
x;}
97 if(roi.
y<0){region.
y=0;region.
height+=roi.
y;}
104 patch=img(region).
clone();
109 int addTop,addBottom, addLeft, addRight;
110 addTop=region.
y-roi.
y;
112 addLeft=region.
x-roi.
x;
Scalar_< double > Scalar
Definition: types.hpp:669
convert between RGB/BGR and grayscale, color conversions
Definition: imgproc.hpp:536
aaaaaa|abcdefgh|hhhhhhh
Definition: base.hpp:270
void convertTo(OutputArray m, int rtype, double alpha=1, double beta=0) const
Converts an array to another data type with optional scaling.
_Tp x
x coordinate of the top-left corner
Definition: types.hpp:453
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions ...
Definition: mat.hpp:2083
int desc_pca
compressed descriptors of TrackerKCF::MODE
Definition: tracker.hpp:1251
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)
Selects ROI on the given image. Function creates a window and allows user to select a ROI using mouse...
Class for video capturing from video files, image sequences or cameras.
Definition: videoio.hpp:612
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:454
int desc_npca
non-compressed descriptors of TrackerKCF::MODE
Definition: tracker.hpp:1252
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...
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:456
int cols
Definition: mat.hpp:2083
bool compress_feature
activate the pca method to compress the features
Definition: tracker.hpp:1248
#define CV_64F
Definition: interface.h:79
Template class for 2D rectangles.
Definition: types.hpp:420
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:455
int compressed_size
feature size after compression
Definition: tracker.hpp:1250
Mat clone() const CV_NODISCARD
Creates a full copy of the array and the underlying data.
n-dimensional dense array class
Definition: mat.hpp:788
Definition: tracker.hpp:1222
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.
Set Custom Parameters
TrackerKCF::Params param;
param.
desc_pca = TrackerKCF::GRAY | TrackerKCF::CN;
param.desc_npca = 0;
param.compress_feature = true;
param.compressed_size = 2;
To set custom paramters, an object should be created. Each tracker algorithm has their own parameter format. So, in this case we should use parameter from cv::TrackerKCF since we are interested in modifying the parameter of this tracker algorithm.
There are several parameters that can be configured as explained in cv::TrackerKCF::Params. For this tutorial, we focussed on the feature extractor functions.
Several feature types can be used in cv::TrackerKCF. In this case, the grayscale value (1 dimension) and color-names features (10 dimension), will be merged as 11 dimension feature and then compressed into 2 dimension as specified in the code.
If you want to use another type of pre-defined feature-extractor function, you can check in cv::TrackerKCF::MODE. We will leave the non-compressed feature as 0 since we want to use a customized function.
Using a custom function
You can define your own feature-extractor function for the CN tracker. However, you need to take care about several things:
- The extracted feature should have the same size as the size of the given bounding box (width and height). For the number of channels you can check the limitation in cv::Mat.
- You can only use features that can be compared using Euclidean distance. Features like local binary pattern (LBP) may not be suitable since it should be compared using Hamming distance.
Since the size of the extracted feature should be in the same size with the given bounding box, we need to take care whenever the given bounding box is partially out of range. In this case, we can copy part of image contained in the bounding box as shown in the snippet below.
if(roi.
x<0){region.
x=0;region.
width+=roi.
x;}
if(roi.
y<0){region.
y=0;region.
height+=roi.
y;}
Whenever the copied image is smaller than the given bounding box, padding should be given to the sides where the bounding box is partially out of frame.
int addTop,addBottom, addLeft, addRight;
Defining the feature
In this tutorial, the extracted feature is reponse of the Sobel filter in x and y direction. Those Sobel filter responses are concatenated, resulting a feature with 2 channels.
Post processing
Make sure to convert the feature into cv::CV_64F data format and normalize its value with range -0.5 to 0.5