标签:source main output cout 建图 ctr open tor 目标
1 #include <iostream> 2 #include "opencv2/highgui.hpp" 3 #include "opencv2/tracking.hpp" 4 5 using namespace std; 6 7 int main() 8 { 9 //创建视频捕获对象 10 cv::VideoCapture video("FIFA.mp4"); 11 //检查视频捕获对象是否正确创建 12 if (!video.isOpened()) 13 { 14 cout << "can not open source" << endl; 15 return -1; 16 } 17 //创建图像矩阵,用于保存一帧数据 18 cv::Mat frame; 19 //获取视频解决方案 20 int frameWidth = video.get(cv::CAP_PROP_FRAME_WIDTH); 21 int frameHeight = video.get(cv::CAP_PROP_FRAME_HEIGHT); 22 //获取帧速率 23 int fps = video.get(cv::CAP_PROP_FPS); 24 //获取编解码方式 25 int fourcc = video.get(cv::CAP_PROP_FOURCC); 26 //获取框大小 27 cv::Size size = cv::Size(frameWidth, frameHeight); 28 29 //创建视频写入对象 30 cv::VideoWriter output("myoutput.mp4", fourcc, fps, size); 31 //读取一帧数据 32 video.read(frame); 33 //创建多目标跟踪器 34 cv::Ptr<cv::MultiTracker> multiTracker = cv::MultiTracker::create(); 35 //创建矩形向量,保存目标对象 36 std::vector<cv::Rect> boundingBoxes; 37 //选择感兴趣区域 38 cv::selectROIs("frame feed", frame, boundingBoxes, false); 39 //检查是否选择感兴趣区域 40 if (boundingBoxes.size() < 1) 41 return 0; 42 //为感兴趣区域添加目标跟踪器 43 for (auto &boundingBox : boundingBoxes) 44 { 45 multiTracker->add(cv::TrackerKCF::create(), frame, boundingBox); 46 } 47 //循环每一帧数据并且跟踪 48 while (video.read(frame)) 49 { 50 multiTracker->update(frame); 51 for (auto &object : multiTracker->getObjects()) { 52 cv::rectangle(frame, object, cv::Scalar(255, 0, 0), 2, 8); 53 } 54 //展现视频 55 cv::imshow("Video feed", frame); 56 //写入视频到指定文件 57 output.write(frame); 58 //退出循环 59 if (cv::waitKey(25)>=0) break; 60 } 61 //释放视频捕获器和视频写入对象 62 video.release(); 63 output.release(); 64 65 //释放所有窗口 66 cv::destroyAllWindows(); 67 68 69 return 0; 70 }
标签:source main output cout 建图 ctr open tor 目标
原文地址:https://www.cnblogs.com/jeapwu/p/11099018.html