Goal
In this tutorial you will learn how to
- Set custom parameters for CN tracker.
- Use your own feature-extractor function for the CN tracker.
This document contains tutorial for the cv::TrackerKCF.
Source Code
Explanation
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.
// extract patch inside the imageWhenever 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.
// add some padding to compensate when the patch is outside image borderint addTop,addBottom, addLeft, addRight;copyMakeBorder(patch,patch,addTop,addBottom,addLeft,addRight,BORDER_REPLICATE);Defining the feature
In this tutorial, the extracted feature is response 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 normalize the feature with range -0.5 to 0.5
feat=feat/255.0-0.5; // normalize to range -0.5 .. 0.5