下面是自己在看论文和这些大牛的分析过程中,对代码进行了一些理解,但是由于自己接触图像处理和机器视觉没多久,另外由于自己编程能力比较弱,所以分析过程可能会有不少的错误,希望各位不吝指正。而且,因为编程很多地方不懂,所以注释得非常乱,还海涵。
TLD.cpp
- /*
- * TLD.cpp
- */
- #include <TLD.h>
- #include <stdio.h>
- using namespace cv;
- using namespace std;
- TLD::TLD()
- {
- }
- TLD::TLD(const FileNode& file){
- read(file);
- }
- void TLD::read(const FileNode& file){
- /// 边界框参数
- min_win = (int)file["min_win"];
- /// 发电机参数
- // 初步参数为正例
- patch_size = (int)file["patch_size"];
- num_closest_init = (int)file["num_closest_init"];
- num_warps_init = (int)file["num_warps_init"];
- noise_init = (int)file["noise_init"];
- angle_init = (float)file["angle_init"];
- shift_init = (float)file["shift_init"];
- scale_init = (float)file["scale_init"];
- // 更新参数为正例
- num_closest_update = (int)file["num_closest_update"];
- num_warps_update = (int)file["num_warps_update"];
- noise_update = (int)file["noise_update"];
- angle_update = (float)file["angle_update"];
- shift_update = (float)file["shift_update"];
- scale_update = (float)file["scale_update"];
- // 参数为负例
- bad_overlap = (float)file["overlap"];
- bad_patches = (int)file["num_patches"];
- classifier.read(file);
- }
- //此函数完成准备工作
- void TLD::init(const Mat& frame1, const Rect& box, FILE* bb_file){
- //bb_file = fopen("bounding_boxes.txt","w");
- // 获取边框
- //此函数根据传入的box(目标边界框)在传入的图像frame1中构建全部的扫描窗口,并计算重叠度
- buildGrid(frame1, box);
- printf("Created %d bounding boxes\n",(int)grid.size()); //vector的成员size()用于获取向量元素的个数
- /// 制备
- // 分配
- //积分图像,用以计算2bitBP特征(类似于haar特征的计算)
- //Mat的创建,方式有两种:1.调用create(行,列,类型)2.Mat(行,列,类型(值))。
- iisum.create(frame1.rows+1, frame1.cols+1, CV_32F);
- iisqsum.create(frame1.rows+1, frame1.cols+1, CV_64F);
- //Detector data中定义:std::vector<float> dconf; 检测确信度??
- //vector 的reserve增加了vector的capacity,但是它的size没有改变!而resize改变了vector
- //的capacity同时也增加了它的size!reserve是容器预留空间,但在空间内不真正创建元素对象,
- //所以在没有添加新的对象之前,不能引用容器内的元素。
- //不管是调用resize还是reserve,二者对容器原有的元素都没有影响。
- //myVec.reserve( 100 ); // 新元素还没有构造, 此时不能用[]访问元素
- //myVec.resize( 100 ); // 用元素的默认构造函数构造了100个新的元素,可以直接操作新元素
- dconf.reserve(100);
- dbb.reserve(100);
- bbox_step =7;
- //以下在Detector data中定义的容器都给其分配grid.size()大小(这个是一幅图像中全部的扫描窗口个数)的容量
- //Detector data中定义TempStruct tmp;
- //tmp.conf.reserve(grid.size());
- tmp.conf = vector<float>(grid.size());
- tmp.patt = vector<vector<int> >(grid.size(), vector<int>(10,0));
- //tmp.patt.reserve(grid.size());
- dt.bb.reserve(grid.size());
- good_boxes.reserve(grid.size());
- bad_boxes.reserve(grid.size());
- //TLD中定义:cv::Mat pEx; //positive NN example 大小为15*15图像片
- pEx.create(patch_size, patch_size, CV_64F);
- //Init Generator
- //TLD中定义:cv::PatchGenerator generator; //PatchGenerator类用来对图像区域进行仿射变换
- /*
- cv::PatchGenerator::PatchGenerator (
- double _backgroundMin,
- double _backgroundMax,
- double _noiseRange,
- bool _randomBlur = true,
- double _lambdaMin = 0.6,
- double _lambdaMax = 1.5,
- double _thetaMin = -CV_PI,
- double _thetaMax = CV_PI,
- double _phiMin = -CV_PI,
- double _phiMax = CV_PI
- )
- 一般的用法是先初始化一个PatchGenerator的实例,然后RNG一个随机因子,再调用()运算符产生一个变换后的正样本。
- */
- generator = PatchGenerator (0,0,noise_init,true,1-scale_init,1+scale_init,-angle_init*CV_PI/180,
- angle_init*CV_PI/180,-angle_init*CV_PI/180,angle_init*CV_PI/180);
- //此函数根据传入的box(目标边界框),在整帧图像中的全部窗口中寻找与该box距离最小(即最相似,
- //重叠度最大)的num_closest_init个窗口,然后把这些窗口 归入good_boxes容器
- //同时,把重叠度小于0.2的,归入 bad_boxes 容器
- //首先根据overlap的比例信息选出重复区域比例大于60%并且前num_closet_init= 10个的最接近box的RectBox,