使用opencv仅检测缓慢移动的物体

detecting only slow moving objects using opencv

本文关键字:移动 缓慢 opencv 检测 使用      更新时间:2023-10-16

我使用OpenCV进行运动检测,并为此使用后台减法算法。我从网上得到了以下代码。

cv::Mat frame;
    cv::Mat back;
    cv::Mat fore;
    cv::VideoCapture cap(0);
    bg.nmixtures = 3;
    bg.bShadowDetection = false;
const int history = 5;
cv::BackgroundSubtractorMOG2 bg (history,nmixtures,bShadowDetection);
    std::vector<std::vector<cv::Point> > contours;
    cv::namedWindow("Frame");
    cv::namedWindow("Background");
    for(;;)
    {
        cap >> frame;
        bg.operator ()(frame,fore);
        bg.getBackgroundImage(back);
        cv::erode(fore,fore,cv::Mat());
        cv::dilate(fore,fore,cv::Mat());
        cv::findContours(fore,contours,CV_RETR_EXTERNAL,CV_CHAIN_APPROX_NONE);
        cv::drawContours(frame,contours,-1,cv::Scalar(0,0,255),2);
        cv::imshow("Frame",frame);
        cv::imshow("Background",back);
        if(cv::waitKey(30) >= 0) break;
    }

所以我可以设置一个阈值,这样如果新旧框架的变化超过阈值,就什么都不做了。或者可能是其他一些算法,应该适合我只捕捉缓慢移动物体的情况。

如果您希望检测慢速移动的对象,您可以更改高斯混合模型中的历史值(增加它)。

您可以尝试使用帧的移动平均值,而不是使用每一帧作为BG减法的输入。或者使用移动平均值来输出BG减法,然后通过阈值进行二值化。

请参阅addWeighted and moving Average(请参阅Cucumulative moving Average)。

整合将减少快速变化的影响。