RLL Computer Vision Code: code/armour_tracker.cpp 源文件

RLL Computer Vision

RLL Computer Vision Code  1.0
江苏科技大学HLL战队机器视觉代码
armour_tracker.cpp
浏览该文件的文档.
1 #include "armour_tracker.h"
2 
3 namespace HCVC
4 {
6 {
7 }
8 
9 void ArmourTracker::init(const Mat &srcImage, Rect2d armourBlock)
10 {
11  TrackerKCF::Params param;
12  param.desc_pca = TrackerKCF::GRAY | TrackerKCF::CN;
13  param.desc_npca = 0;
14  param.compress_feature = true;
15  param.compressed_size = 2;
16 
17  tracker = TrackerKCF::create(param);
18 
19  //tracker->setFeatureExtractor(sobelExtractor);
20 
21  tracker->init(srcImage, armourBlock);
22 }
23 
24 bool ArmourTracker::track(Mat srcImage)
25 {
26  Rect2d armourBlock;
27  //追踪目标区域
28  if(tracker->update(srcImage, armourBlock) == false)
29  {
30  return false;
31  }
32 
33  //画出追踪的区域
34  rectangle(srcImage, armourBlock, Scalar(255, 0, 0), 2, 1);
35 
36  return true;
37 }
38 
39 void ArmourTracker::sobelExtractor(const Mat img, const Rect roi, Mat& feat)
40 {
41  Mat sobel[2];
42  Mat patch;
43  Rect region=roi;
44 
46  // extract patch inside the image
47  if(roi.x<0){region.x=0;region.width+=roi.x;}
48  if(roi.y<0){region.y=0;region.height+=roi.y;}
49  if(roi.x+roi.width>img.cols)region.width=img.cols-roi.x;
50  if(roi.y+roi.height>img.rows)region.height=img.rows-roi.y;
51  if(region.width>img.cols)region.width=img.cols;
52  if(region.height>img.rows)region.height=img.rows;
54 
55  patch=img(region).clone();
56  cvtColor(patch,patch, CV_BGR2GRAY);
57 
59  // add some padding to compensate when the patch is outside image border
60  int addTop,addBottom, addLeft, addRight;
61  addTop=region.y-roi.y;
62  addBottom=(roi.height+roi.y>img.rows?roi.height+roi.y-img.rows:0);
63  addLeft=region.x-roi.x;
64  addRight=(roi.width+roi.x>img.cols?roi.width+roi.x-img.cols:0);
65 
66  copyMakeBorder(patch,patch,addTop,addBottom,addLeft,addRight,BORDER_REPLICATE);
68 
70  Sobel(patch, sobel[0], CV_32F,1,0,1);
71  Sobel(patch, sobel[1], CV_32F,0,1,1);
72 
73  merge(sobel,2,feat);
75 
77  feat.convertTo(feat,CV_64F);
78  feat=feat/255.0-0.5; // normalize to range -0.5 .. 0.5
80 }
81 }
ArmourTracker()
初始化
HLL Computer Vision Code namepace.
static void sobelExtractor(const Mat img, const Rect roi, Mat &feat)
sobel特征提取器
void init(const Mat &srcImage, Rect2d armourBlock)
创建图像追踪器并设定参数
bool track(Mat srcImage)
追踪目标区域
Ptr< TrackerKCF > tracker
kcf匹配算法图像追踪器
制作者   doxygen 1.8.13