OpenCV中的深度学习车辆检测
【摘要】 完整源码GitHub使用高斯混合模型(BackgroundSubtractorMOG2)对背景建模,提取出前景使用中值滤波去掉椒盐噪声,再闭运算和开运算填充空洞使用cvBlob库追踪车辆,我稍微修改了cvBlob源码来通过编译由于要对背景建模,这个方法要求背景是静止的另外不同车辆白色区域不能连通,否则会认为是同一物体[cpp] view plain copyvoid processVid...
- 使用高斯混合模型(BackgroundSubtractorMOG2)对背景建模,提取出前景
- 使用中值滤波去掉椒盐噪声,再闭运算和开运算填充空洞
- 使用cvBlob库追踪车辆,我稍微修改了cvBlob源码来通过编译
由于要对背景建模,这个方法要求背景是静止的
另外不同车辆白色区域不能连通,否则会认为是同一物体
- 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);
- }
- }
- }
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)