每个人。!我正在使用opencv2.4.2。实际上我正在做对象检测项目。我尝试使用 BackgroundSubtractorMOG 模型。但我无法从我的电脑加载视频文件。在实时运行时,下面的分段代码可以正常工作。我已经实现了使用帧差分方法进行对象检测。现在我想从背景中分割整个对象。我有静态背景。所以任何人都可以在下面的代码中帮助我如何从捕获的视频中分割对象。还如何加载视频文件?谢谢你。
#include "stdafx.h" #include "opencv2/imgproc/imgproc.hpp" #include "opencv2/highgui/highgui.hpp" #include "opencv2/contrib/contrib.hpp" #include "conio.h" #include "time.h" #include "opencv/cvaux.hpp" #include "opencv2/core/core.hpp" #include "opencv2/calib3d/calib3d.hpp" using namespace std; using namespace cv; int main(int argc, char** argv) { //IplImage* tmp_frame; //std::string arg = argv[1]; //VideoCapture capture(); cv::VideoCapture cap; /*CvCapture *cap =cvCaptureFromFile("S:\\offline object detection database\\SINGLE PERSON Database\\video4.avi"); if(!cap){ printf("Capture failure\n"); return -1; } IplImage* frame=0; frame = cvQueryFrame(cap); if(!frame) return -1;*/ bool update_bg_model = true; if( argc < 2 ) cap.open(0); else cap.open(std::string(argv[1])); if( !cap.isOpened() ) { printf("can not open camera or video file\n"); return -1; } Mat tmp_frame, bgmask; cap >> tmp_frame; if(!tmp_frame.data) { printf("can not read data from the video source\n"); return -1; } namedWindow("video", 1); namedWindow("segmented", 1); BackgroundSubtractorMOG bgsubtractor; for(;;) { //double t = (double)cvGetTickCount(); cap >> tmp_frame; if( !tmp_frame.data ) break; bgsubtractor(tmp_frame, bgmask, update_bg_model ? -1 : 0); //t = (double)cvGetTickCount() - t; //printf( "%d. %.1f\n", fr, t/(cvGetTickFrequency()*1000.) ); imshow("video", tmp_frame); imshow("segmented", bgmask); char keycode = waitKey(30); if( keycode == 27 ) break; if( keycode == ' ' ) update_bg_model = !update_bg_model; } return 0; }