繁体   English   中英

使用OpenCV C ++进行运动检测

[英]Motion detection with OpenCV c++

我正在尝试使用网络摄像头和OpenCV。 我遵循此教程: http ://mateuszstankiewicz.eu/?p=189。 但是我唯一的结果是一个红色边框,我不明白为什么。 谁能帮我解决这个问题并解决?

这是我的代码:

#include "mvt_detection.h"


Mvt_detection::Mvt_detection()
{

}

Mvt_detection::~Mvt_detection()
{
}

cv::Mat Mvt_detection::start(cv::Mat frame)
{
    cv::Mat back;
    cv::Mat fore;
    cv::BackgroundSubtractorMOG2 bg(5,3,true) ;
    cv::namedWindow("Background");
    std::vector<std::vector<cv::Point> > contours;

    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);
    return frame;
}

这是我们的cam返回的屏幕截图: 在此处输入图片说明

我试图在其他两个视频从那里没有和有同样的问题。

谢谢您的帮助 :)。

我使用了以下代码,它们与您的代码相似,并且运行良好。 我也从网络摄像头获取输入。 在您的代码中,我没有找到任何imshow()和waitkey。 尝试使用它们。 我的代码如下:

#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>

#include <iostream>
#include <vector>

using namespace std;
using namespace cv;

int main()
{

    VideoCapture cap;
    bool update_bg_model = true;

    cap.open(0);
    cv::BackgroundSubtractorMOG2 bg;//(100, 3, 0.3, 5);
    bg.set ("nmixtures", 3);
    std::vector < std::vector < cv::Point > >contours;

    cv::namedWindow ("Frame");
    cv::namedWindow ("Background");

    Mat frame, fgmask, fgimg, backgroundImage;

    for(;;)
    {
        cap >> frame;
        bg.operator()(frame, fgimg);
        bg.getBackgroundImage (backgroundImage);
        cv::erode (fgimg, fgimg, cv::Mat ());
        cv::dilate (fgimg, fgimg, cv::Mat ());

        cv::findContours (fgimg, 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", backgroundImage);


        char k = (char)waitKey(30);
        if( k == 27 ) break;

    }

    return 0;
}

解决了问题,将BackgroundSubtractorMOG2放在我的对象的字段中并在构造函数中对其进行初始化使他可以正常工作。

正如@Lenjyco所说,我们已解决问题。

@Micka有个好主意:

首先,仅一次激活BackgroundSubtractorMOG2。

我们在构造函数中实例化它并使用Hystory和Threashold:

Mvt_detection::Mvt_detection()
{
    bg = new cv::BackgroundSubtractorMOG2(10, 16, false);
}

10:背景要回头比较的图像数。

16:阈值级别(模糊)

这样,我们现在可以检测运动。

谢谢 !

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM