簡體   English   中英

背景減法使用opencv mogdetector和c ++從現場相機飼料

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

我正在嘗試將此代碼用於實時攝像機饋送的背景減法。 但是這段代碼在兩個窗口都給出了白色圖像。 問題是,當使用來自同一相機的視頻文件進行測試時,它工作正常,沒有任何錯誤,但當視頻文件被相機輸入替換時,它變為白色,並且在運行的窗口終端中顯示錯誤:

HIGHGUI錯誤:V4L2:無法獲取屬性(1) - 參數無效

從攝像機饋送中取出視頻時,會不斷重復上述錯誤。 請幫忙解決這個問題。

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

我認為財產

CV_CAP_PROP_POS_FRAMES

對相機無效。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM