您好,登錄后才能下訂單哦!
本文實例為大家分享了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進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。