[英]Motion detection with OpenCV c++
I'm trying to play with my webcam and OpenCV. 我正在尝试使用网络摄像头和OpenCV。 I follow this tuto : http://mateuszstankiewicz.eu/?p=189 .
我遵循此教程: http ://mateuszstankiewicz.eu/?p=189。 But the only result I have is one red border and I don't understand why.
但是我唯一的结果是一个红色边框,我不明白为什么。 Could anyone help me to make it right and fix this ?
谁能帮我解决这个问题并解决?
Here is my code : 这是我的代码:
#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;
}
Here is a screenshot of what our cam returns : 这是我们的cam返回的屏幕截图:
I tried on two other video from there and there and there is the same issue. 我试图在其他两个视频从那里并没有和有同样的问题。
Thanks for the help :). 谢谢您的帮助 :)。
I have used the following code which is similar to yours and it is working well. 我使用了以下代码,它们与您的代码相似,并且运行良好。 I am also taking the inputs from my webcam.
我也从网络摄像头获取输入。 In your code, i didnt find any imshow() and waitkey.
在您的代码中,我没有找到任何imshow()和waitkey。 Try to use them.
尝试使用它们。 My code is following:
我的代码如下:
#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放在我的对象的字段中并在构造函数中对其进行初始化使他可以正常工作。
As @Lenjyco said, we fixe the problem. 正如@Lenjyco所说,我们已解决问题。
@Micka had a good idea : @Micka有个好主意:
Firstly the BackgroundSubtractorMOG2 as to be instancied only ONCE. 首先,仅一次激活BackgroundSubtractorMOG2。
We instantiate it in the constructor and play with the Hystory and Threashold: 我们在构造函数中实例化它并使用Hystory和Threashold:
Mvt_detection::Mvt_detection()
{
bg = new cv::BackgroundSubtractorMOG2(10, 16, false);
}
10 : the number of image the backgound look back to compare.
10:背景要回头比较的图像数。
16 : the threshold level (blur)
16:阈值级别(模糊)
This way, we are now able to detect motion. 这样,我们现在可以检测运动。
Thank you ! 谢谢 !
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.