您好,登录后才能下订单哦!
密码登录
登录注册
点击 登录注册 即表示同意《亿速云用户服务条款》
本文实例为大家分享了OpenCV实现检测和追踪车辆的具体代码,供大家参考,具体内容如下
完整源码GitHub
由于要对背景建模,这个方法要求背景是静止的
另外不同车辆白色区域不能连通,否则会认为是同一物体
void processVideo(char* videoFilename) { Mat frame; // current frame Mat fgMaskMOG2; // fg mask fg mask generated by MOG2 method Mat bgImg; // background Ptr<BackgroundSubtractorMOG2> pMOG2 = createBackgroundSubtractorMOG2(200, 36.0, false); // MOG2 Background subtractor while (true) { VideoCapture capture(videoFilename); if (!capture.isOpened()) { cerr << "Unable to open video file: " << videoFilename << endl; return; } int width = (int)capture.get(CV_CAP_PROP_FRAME_WIDTH); int height = (int)capture.get(CV_CAP_PROP_FRAME_HEIGHT); unique_ptr<IplImage, void(*)(IplImage*)> labelImg(cvCreateImage(cvSize(width, height), IPL_DEPTH_LABEL, 1), [](IplImage* p){ cvReleaseImage(&p); }); CvBlobs blobs; CvTracks tracks; while (true) { // read input data. ESC or 'q' for quitting int key = waitKey(1); if (key == 'q' || key == 27) return; if (!capture.read(frame)) break; // update background pMOG2->apply(frame, fgMaskMOG2); pMOG2->getBackgroundImage(bgImg); imshow("BG", bgImg); imshow("Original mask", fgMaskMOG2); // post process medianBlur(fgMaskMOG2, fgMaskMOG2, 5); imshow("medianBlur", fgMaskMOG2); morphologyEx(fgMaskMOG2, fgMaskMOG2, MORPH_CLOSE, getStructuringElement(MORPH_RECT, Size(5, 5))); // fill black holes morphologyEx(fgMaskMOG2, fgMaskMOG2, MORPH_OPEN, getStructuringElement(MORPH_RECT, Size(5, 5))); // fill white holes imshow("morphologyEx", fgMaskMOG2); // track cvLabel(&IplImage(fgMaskMOG2), labelImg.get(), blobs); cvFilterByArea(blobs, 64, 10000); cvUpdateTracks(blobs, tracks, 10, 90, 30); cvRenderTracks(tracks, &IplImage(frame), &IplImage(frame)); // show imshow("Frame", frame); key = waitKey(30); } } }
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持亿速云。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。