OpenCV 5.0.0
Open Source Computer Vision
読み込み中...
検索中...
見つかりません
🤖 AIによる機械翻訳(非公式) — これは OpenCV 5.0.0 公式リファレンス(英語)を AI (Claude) で自動翻訳したものです。訳に誤りを含む場合があります。正確な情報は 公式英語版(原文) を参照してください。
CNトラッカーのカスタマイズ

目的

このチュートリアルでは、次のことを学ぶ

  • CNトラッカーのカスタム引数を設定する。
  • CNトラッカーに独自の特徴抽出関数を使用する。

このドキュメントには cv::TrackerKCF のチュートリアルが含まれている。

ソースコード

3#include <opencv2/videoio.hpp>
4#include <opencv2/highgui.hpp>
5#include <iostream>
6#include <cstring>
7#include "samples_utility.hpp"
8
9using namespace std;
10using namespace cv;
11
12// prototype of the functino for feature extractor
13void sobelExtractor(const Mat img, const Rect roi, Mat& feat);
14
15int 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
28 Rect roi;
29 Mat frame;
30
33 param.desc_pca = TrackerKCF::GRAY | TrackerKCF::CN;
34 param.desc_npca = 0;
35 param.compress_feature = true;
36 param.compressed_size = 2;
38
39 // create a tracker object
41 Ptr<TrackerKCF> tracker = TrackerKCF::create(param);
43
45 tracker->setFeatureExtractor(sobelExtractor);
47
48 // set input video
49 std::string video = argv[1];
50 VideoCapture cap(video);
51
52 // get bounding box
53 cap >> frame;
54 roi=selectROI("tracker",frame);
55
56 //quit if ROI was not selected
57 if(roi.width==0 || roi.height==0)
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
77 rectangle( frame, roi, Scalar( 255, 0, 0 ), 2, 1 );
78
79 // show image with the tracked object
80 imshow("tracker",frame);
81
82 //quit on ESC button
83 if(waitKey(1)==27)break;
84 }
85
86 return 0;
87}
88
89void sobelExtractor(const Mat img, const Rect roi, Mat& feat){
90 Mat sobel[2];
91 Mat patch;
92 Rect region=roi;
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;}
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();
105 cvtColor(patch,patch, COLOR_BGR2GRAY);
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;
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
115 copyMakeBorder(patch,patch,addTop,addBottom,addLeft,addRight,BORDER_REPLICATE);
117
119 Sobel(patch, sobel[0], CV_32F,1,0,1);
120 Sobel(patch, sobel[1], CV_32F,0,1,1);
121
122 merge(sobel,2,feat);
124
126 feat=feat/255.0-0.5; // normalize to range -0.5 .. 0.5
128}
Comma-separated Matrix Initializer.
Definition mat.hpp:964
CV_NODISCARD_STD Mat clone() const
Creates a full copy of the array and the underlying data.
int cols
Definition mat.hpp:2488
int rows
the number of rows and columns or (-1, -1) when the matrix has more than 2 dimensions
Definition mat.hpp:2488
Template class for 2D rectangles.
Definition types.hpp:447
_Tp x
x coordinate of the top-left corner
Definition types.hpp:490
_Tp y
y coordinate of the top-left corner
Definition types.hpp:491
_Tp width
width of the rectangle
Definition types.hpp:492
_Tp height
height of the rectangle
Definition types.hpp:493
Class for video capturing from video files, image sequences or cameras.
Definition videoio.hpp:790
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.
std::shared_ptr< _Tp > Ptr
Definition cvstd_wrapper.hpp:23
#define CV_32F
Definition interface.h:59
void sobel(InputArray _src, OutputArray _dx, OutputArray _dy, int kernel_size, int borderType, int borderValue)
Creates a 2D gradient image from source luminance data without normalization. Calculate X direction 1...
void cvtColor(InputArray src, OutputArray dst, int code, int dstCn=0, AlgorithmHint hint=cv::ALGO_HINT_DEFAULT)
Converts an image from one color space to another.
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
Definition core.hpp:107
STL namespace.
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

解説

この部分では、CNトラッカーのカスタム引数を設定し、独自の特徴抽出関数を使用する方法を説明する。cv::Tracker の使用に関するより詳細な情報が必要な場合は、OpenCV Tracker入門 を参照すること。

  1. カスタム引数の設定

    param.desc_pca = TrackerKCF::GRAY | TrackerKCF::CN;
    param.desc_npca = 0;
    param.compress_feature = true;
    param.compressed_size = 2;

    カスタム引数を設定するには、オブジェクトを作成する必要がある。各トラッカーアルゴリズムは独自の引数形式を持つ。したがって、この場合はこのトラッカーアルゴリズムの引数を変更することに関心があるため、cv::TrackerKCF の引数を使用するべきである。

    cv::TrackerKCF::Params で説明されているように、設定可能な引数がいくつかある。このチュートリアルでは、特徴抽出関数に焦点を当てた。

    cv::TrackerKCF ではいくつかの特徴の種類を利用できる。ここでは、グレースケール値 (1次元) と color-names 特徴 (10次元) を結合して11次元の特徴とし、その後コード中で指定したように2次元に圧縮する。

    別の種類の定義済み特徴抽出関数を使いたい場合は、cv::TrackerKCF::MODE を確認するとよい。ここではカスタマイズした関数を使いたいので、非圧縮の特徴は0のままにしておく。

  2. カスタム関数を使う

    CNトラッカー用に独自の特徴抽出関数を定義できる。ただし、いくつか注意すべき点がある。

    • 抽出される特徴は、与えられたバウンディングボックスのサイズ (幅と高さ) と同じサイズでなければならない。チャンネル数については cv::Mat の制約を確認するとよい。
    • ユークリッド距離で比較できる特徴のみ使用できる。local binary pattern (LBP) のような特徴はハミング距離で比較すべきものなので、適していない可能性がある。

    抽出される特徴のサイズは与えられたバウンディングボックスと同じサイズである必要があるため、バウンディングボックスが部分的に範囲外にある場合には注意が必要である。この場合、下のコードスニペットに示すように、バウンディングボックスに含まれる画像の一部をコピーすればよい。

    // extract patch inside the image
    if(roi.x<0){region.x=0;region.width+=roi.x;}
    if(roi.y<0){region.y=0;region.height+=roi.y;}
    if(roi.x+roi.width>img.cols)region.width=img.cols-roi.x;
    if(roi.y+roi.height>img.rows)region.height=img.rows-roi.y;
    if(region.width>img.cols)region.width=img.cols;
    if(region.height>img.rows)region.height=img.rows;

    コピーした画像が与えられたバウンディングボックスより小さい場合は、バウンディングボックスが部分的にフレーム外にある側にパディングを与える必要がある。

    // add some padding to compensate when the patch is outside image border
    int addTop,addBottom, addLeft, addRight;
    addTop=region.y-roi.y;
    addBottom=(roi.height+roi.y>img.rows?roi.height+roi.y-img.rows:0);
    addLeft=region.x-roi.x;
    addRight=(roi.width+roi.x>img.cols?roi.width+roi.x-img.cols:0);
    copyMakeBorder(patch,patch,addTop,addBottom,addLeft,addRight,BORDER_REPLICATE);
  3. 特徴の定義

    このチュートリアルでは、抽出される特徴はx方向とy方向のSobelフィルタの応答である。これらのSobelフィルタの応答を連結し、2チャンネルの特徴となる。

    Sobel(patch, sobel[0], CV_32F,1,0,1);
    Sobel(patch, sobel[1], CV_32F,0,1,1);
    merge(sobel,2,feat);
  4. 後処理

    特徴を -0.5 から 0.5 の範囲に正規化することを忘れないこと

    feat=feat/255.0-0.5; // normalize to range -0.5 .. 0.5