在opencv中常常要实现对视频中的目标进行追踪,一些比较新的方法(比如MIL、KCF、TLD等)都在opencv_contrib库里,所以首先你需要下载安装opencv_contrib库,详见在win10下实现openCV3.2.0+vs2015+cmake出错解决方案 。这里需要说明的是我的系统是win10的,所以开发环境均在win10下,以下不做特殊说明均在win10下完成。
关于tracking官网提供了其参考资料Tracking API 、Introduction to OpenCV Tracker 。
opencv3.X以后将目标追踪方法集成到tracking上,集成图如下:
tracking实现的思路如下:
●创建tracker对象
●使用roiSelector函数的功能,从指定的图像中选择ROI
●在图像中跟踪特定的区域
这里我引入了一段网上的代码,也是官网的代码稍加修改:
#include <opencv2/core/utility.hpp> #include <opencv2/tracking/tracking.hpp> #include <opencv2/videoio.hpp> #include <opencv2/highgui.hpp> #include <iostream> #include <cstring> using namespace std; using namespace cv; int main(int argc, char** argv) { // declares all required variables Rect2d roi; Mat frame; // create a tracker object Ptr<Tracker> tracker = Tracker::create("KCF"); // set input video // std::string video = argv[1]; VideoCapture cap("dount.avi"); // get bounding box cap >> frame; roi = selectROI("tracker", frame); //quit if ROI was not selected if (roi.width == 0 || roi.height == 0) return 0; // initialize the tracker tracker->init(frame, roi); // perform the tracking process printf("Start the tracking process, press ESC to quit.\n"); for (;; ) { // get frame from the video cap >> frame; // stop the program if no more images if (frame.rows == 0 || frame.cols == 0) break; // update the tracking result tracker->update(frame, roi); // draw the tracked object rectangle(frame, roi, Scalar(255, 0, 0), 2, 1); // show image with the tracked object imshow("tracker", frame); //quit on ESC button if (waitKey(1) == 27)break; } return 0; }
运行这段代码有两个环境变量要配置:
1. Ptr<Tracker> tracker = Tracker::create("KCF");报错;
原因是我们要将opencv_contrib库里的tracking引入工程中,这里有两种方法:
a.直接将opencv_contrib库里tracking文件夹复制到opencv2下,并将tracking文件夹中的tracking文件夹中的内容复制到最外层文件夹里,
复制后的结果:
b.将cmake编译后的文件夹中的include目录引入工程中:VC++目录-->包含目录,添加: D:\work\opencv\myopencv\install\include
这样编译就没有错误了。
由于我用的第一种方法所以遇到了一个这样的错误:
错误 C1014:包含文件太多:深度=1024
出现这个错误可能的原因是opencv2中有文件重复。
2.编译链接时出现了无法编译的外部错误:
严重性 |
代码 |
说明 |
项目 |
文件 |
行 |
禁止显示状态 |
错误 |
LNK2019 |
无法解析的外部符号 "public: bool __cdecl cv::Tracker::init(class cv::Mat const &,class cv::Rect_<double> const &)" ([email protected]@[email protected]@[email protected]@[email protected]@[email protected]@Z),该符号在函数 main 中被引用 |
point_collect |
D:\work\project\point_collect\point_collect\质点追踪.obj |
1 |
一共有四个这里显示了一个,原因就是没引入tracking的动态链接库,就是要将cmake后的文件夹下的动态链接库引入到工程中,
然后配置输入文件的附加依赖项:
这里注意如果是
Debug:
opencv_aruco320d.lib
opencv_bgsegm320d.lib
opencv_bioinspired320d.lib
opencv_calib3d320d.lib
opencv_ccalib320d.lib
opencv_core320d.lib
opencv_datasets320d.lib
opencv_dnn320d.lib
opencv_dpm320d.lib
opencv_face320d.lib
opencv_features2d320d.lib
opencv_flann320d.lib
opencv_fuzzy320d.lib
opencv_highgui320d.lib
opencv_imgcodecs320d.lib
opencv_imgproc320d.lib
opencv_line_descriptor320d.lib
opencv_ml320d.lib
opencv_objdetect320d.lib
opencv_optflow320d.lib
opencv_phase_unwrapping320d.lib
opencv_photo320d.lib
opencv_plot320d.lib
opencv_reg320d.lib
opencv_rgbd320d.lib
opencv_saliency320d.lib
opencv_shape320d.lib
opencv_stereo320d.lib
opencv_stitching320d.lib
opencv_structured_light320d.lib
opencv_superres320d.lib
opencv_surface_matching320d.lib
opencv_text320d.lib
opencv_tracking320d.lib
opencv_video320d.lib
opencv_videoio320d.lib
opencv_videostab320d.lib
opencv_xfeatures2d320d.lib
opencv_ximgproc320d.lib
opencv_xobjdetect320d.lib
opencv_xobjdetect320d.lib
否则:
- opencv_aruco320.lib
- opencv_bgsegm320.lib
- opencv_bioinspired320.lib
- opencv_calib3d320.lib
- opencv_ccalib320.lib
- opencv_core320.lib
- opencv_datasets320.lib
- opencv_dnn320.lib
- opencv_dpm320.lib
- opencv_face320.lib
- opencv_features2d320.lib
- opencv_flann320.lib
- opencv_fuzzy320.lib
- opencv_highgui320.lib
- opencv_imgcodecs320.lib
- opencv_imgproc320.lib
- opencv_line_descriptor320.lib
- opencv_ml320.lib
- opencv_objdetect320.lib
- opencv_optflow320.lib
- opencv_phase_unwrapping320.lib
- opencv_photo320.lib
- opencv_plot320.lib
- opencv_reg320.lib
- opencv_rgbd320.lib
- opencv_saliency320.lib
- opencv_shape320.lib
- opencv_stereo320.lib
- opencv_stitching320.lib
- opencv_structured_light320.lib
- opencv_superres320.lib
- opencv_surface_matching320.lib
- opencv_text320.lib
- opencv_tracking320.lib
- opencv_video320.lib
- opencv_videoio320.lib
- opencv_videostab320.lib
- opencv_xfeatures2d320.lib
- opencv_ximgproc320.lib
- opencv_xobjdetect320.lib