简体   繁体   English

背景减法使用opencv mogdetector和c ++从现场相机饲料

[英]Background substraction using opencv mogdetector and c++ from live camera feed

I am trying to use this code for background subtraction for a live camera feed. 我正在尝试将此代码用于实时摄像机馈送的背景减法。 but this code is giving a white image in both the windows. 但是这段代码在两个窗口都给出了白色图像。 the problem is that when tested with a video file from the same camera it is working fine, without any error but when the video file is replaced by a camera feed it becomes white and in the running window terminal it displays error as: 问题是,当使用来自同一相机的视频文件进行测试时,它工作正常,没有任何错误,但当视频文件被相机输入替换时,它变为白色,并且在运行的窗口终端中显示错误:

HIGHGUI ERROR: V4L2: Unable to get property (1) - Invalid argument HIGHGUI错误:V4L2:无法获取属性(1) - 参数无效

The above error is continuously being repeated when the video is taken from camera feed. 从摄像机馈送中取出视频时,会不断重复上述错误。 Please help to solve this problem. 请帮忙解决这个问题。

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/background_segm.hpp>
#include <cv.h>
#include <iostream>
#include <sstream>

using namespace cv;
using namespace std;

Mat frame; //current frame
Mat fgMaskMOG; //fg mask generated by MOG method
Mat fgMaskMOG2; //fg mask fg mask generated by MOG2 method
Ptr<BackgroundSubtractor> pMOG; //MOG Background subtractor
Ptr<BackgroundSubtractor> pMOG2; //MOG2 Background subtractor
int keyboard;

int main()
{
//create GUI windows
namedWindow("Frame",0);
//namedWindow("FG Mask MOG",0);
namedWindow("FG Mask MOG 2",0);
namedWindow("eroded",0);
namedWindow("eroded2",0);
//create Background Subtractor objects

pMOG= new BackgroundSubtractorMOG(); //MOG approach
pMOG2 = new BackgroundSubtractorMOG2(); //MOG2 approach

//create the capture object
VideoCapture capture(0);

//read input data. ESC or 'q' for quitting
while( (char)keyboard != 'q' && (char)keyboard != 27 )
{
    //read the current frame
    if(!capture.read(frame))
    {
        cerr << "Unable to read next frame." << endl;
        cerr << "Exiting..." << endl;
        exit(EXIT_FAILURE);
    }
   //update the background model
   //AND HERE!!!
   //pMOG->operator()(frame, fgMaskMOG);
   pMOG2->operator()(frame, fgMaskMOG2);
   //get the frame number and write it on the current frame
   stringstream ss;
   rectangle(frame, cv::Point(10, 2), cv::Point(100,20),
   cv::Scalar(255,255,255), -1);
   ss << capture.get(CV_CAP_PROP_POS_FRAMES);
   string frameNumberString = ss.str();
   putText(frame, frameNumberString.c_str(), cv::Point(15, 15),
   FONT_HERSHEY_SIMPLEX, 0.5 , cv::Scalar(0,0,0));
   //show the current frame and the fg masks
   imshow("Frame", frame);


   imshow("FG Mask MOG", fgMaskMOG);
   imshow("FG Mask MOG 2", fgMaskMOG2);
   //get the input from the keyboard
   keyboard = waitKey( 30 );


}

 //delete capture object
capture.release();

//destroy GUI windows
distroyAllWindows();
return EXIT_SUCCESS;
}

I think property 我认为财产

CV_CAP_PROP_POS_FRAMES CV_CAP_PROP_POS_FRAMES

not valid for camera. 对相机无效。

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

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