赞
踩
追踪器选择方法:
如果追求高准确度,又能忍受慢一些的速度,那么就用CSRT;
如果对准确度的要求不苛刻,想追求速度,那么就选KCF;
纯粹想节省时间就用MOSSE。
#include<iostream> #include<string> #include<opencv2/opencv.hpp> #include<opencv2/tracking.hpp> using namespace std; using namespace cv; //鼠标交互获得矩形 Rect2d bbox; bool draw; void on_mouse(int event,int x,int y,int flags,void *param) { Mat& image = *(Mat*)param; switch (event) { case EVENT_LBUTTONDOWN: { draw = true; bbox = Rect2d(x,y,0,0); } break; case EVENT_MOUSEMOVE: { if (draw) { bbox.width = x - bbox.x; bbox.height = y - bbox.y; } } break; case EVENT_LBUTTONUP: { draw = false; if (bbox.width < 0) { bbox.x += bbox.width; bbox.width *= -1; } if (bbox.height<0) { bbox.y += bbox.height; bbox.height *= -1; } rectangle(image,bbox,Scalar(0,0,255),1,8); } break; default: break; } } int main() { //跟踪算法常用类型 string trackerTypes[3] = { "KCF","MOSSE","CSRT" }; //创建跟踪算法 string trackerType = trackerTypes[2]; Ptr<Tracker> tracker; if (trackerType == "KCF") tracker = TrackerKCF::create(); if (trackerType == "MOSSE") tracker = TrackerMOSSE::create(); if (trackerType == "CSRT") tracker = TrackerCSRT::create(); //制作目标对象选框 VideoCapture capture(0); if (!capture.isOpened()) { cout << "Could not read video file" << endl; return -1; } Mat frame; capture >> frame; Mat src_one; frame.copyTo(src_one); imshow("用来制作矩形选框的图像", frame); //鼠标交互获得矩形 namedWindow("选择目标矩形",WINDOW_AUTOSIZE); setMouseCallback("选择目标矩形",on_mouse,&src_one); while (true) { Mat temp_image; src_one.copyTo(temp_image); if (draw) { rectangle(temp_image,bbox,Scalar(0,0,255),1,8); } imshow("选择目标矩形",temp_image); if (waitKey(10) == 27)break; } //初始化跟踪器 tracker->init(src_one,bbox); //追踪视频目标 while (capture.read(frame)) { //开始计时tick double start = (double)getTickCount(); //追踪当前帧目标 bool ok = tracker->update(frame, bbox); //计算帧率 float fps = getTickFrequency() / ((double)getTickCount() - start); if (ok) { //如果追踪到画目标框 rectangle(frame,bbox,Scalar(0,0,255),1,8); } else { putText(frame,"Tracking failure detected",Point(10,10), FONT_HERSHEY_SIMPLEX,0.5,Scalar(255,0,0),2); } //展示检测算法类型 putText(frame, trackerType + " Tracker", Point(10, 20), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(50, 170, 50), 2); // Display FPS on frame 表示FPS putText(frame, "FPS : " + to_string(int(fps)), Point(10, 50), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(50, 170, 50), 2); // Display frame. imshow("Tracking", frame); int k = waitKey(1); if (k == 27) { break; } } return 0; }
1、KCF核相关滤波解读
全看
https://zhuanlan.zhihu.com/p/48249974
看结论部分
https://www.cnblogs.com/jins-note/p/10215511.html
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。