c++ - 使用 opencv mog 检测器和 c++ 从实时摄像机馈送中进行背景减法

标签 c++ opencv camera codeblocks background-subtraction

我正在尝试将此代码用于实时摄像机馈送的背景减除。但是这段代码在两个窗口中都给出了白色图像。问题是,当使用来自同一台摄像机的视频文件进行测试时,它工作正常,没有任何错误,但是当视频文件被摄像机源替换时,它变成白色,并且在运行的窗口终端中它显示错误为:

HIGHGUI ERROR: V4L2: Unable to get property (1) - Invalid argument

当从摄像头源中获取视频时,上述错误不断重复。请帮忙解决这个问题。

#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;
}

最佳答案

我认为属性(property)

CV_CAP_PROP_POS_FRAMES

不适用于相机。

关于c++ - 使用 opencv mog 检测器和 c++ 从实时摄像机馈送中进行背景减法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23595544/

相关文章:

使用 setw 和 setfill 的 C++ 输出格式化

c++ - (WinAPI) 模拟 ComboBox 中的项目选择

C++ 构建器 - TForm 中可点击的欧洲 map

python - 如何使用 opencv python 计算乐高积木上的孔数?

ios - 在 iOS 中请求相机权限对话框启动(主要权限)

python - 对于 PiCameraCircularIO 类 'copy_to()' 函数,Mjpeg 中的流输出的 'first_frame' 的类型是什么?

c++ - 混合/修改数组访问

c++ - OpenCV VideoWriter 断言失败 img.cols == width && img.rows == height*3

python - 需要帮助此时钟使pytesseract ocr

Android (onPreviewFrame) : Saving NV21 data as JPEG File