问题
everyone.! I am using opencv2.4.2. actually I am doing project on object detection. I tried using BackgroundSubtractorMOG model. But I am not able to load video file from my computer. While running on real time this below code for segmentation works fine. I have implemented using frame differencing method for object detection. Now I want to segment whole object from the background. I have static background. so can anybody help me in below code how to segment object from captured video. also how to load a video file? thank you.
#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; }
回答1:
The video loading in opencv works for me. To load a video you can try something like this. Once you have captured frame you either do processing in the loop or can call a separate function.
std::cout<<"Video File "<<argv[1]<<std::endl;
cv::VideoCapture input_video(argv[1]);
namedWindow("My_Win",1);
Mat cap_img;
while(input_video.grab())
{
if(input_video.retrieve(cap_img))
{
imshow("My_Win", cap_img);
/* Once you have the image do all the processing here */
/* Or Call your image processing function */
waitKey(1);
}
}
or You can do something
int main(int argc, char*argv[])
{
char *my_file = "C:\\vid_an2\\desp_me.avi";
std::cout<<"Video File "<<my_file<<std::endl;
cv::VideoCapture input_video;
if(input_video.open(my_file))
{
std::cout<<"Video file open "<<std::endl;
}
else
{
std::cout<<"Not able to Video file open "<<std::endl;
}
namedWindow("My_Win",1);
namedWindow("Segemented", 1);
Mat cap_img;
for(;;)
{
input_video >> cap_img;
imshow("My_Win", cap_img);
waitKey(0);
}
return 0;
}
来源:https://stackoverflow.com/questions/16185456/how-to-play-and-detect-an-object-using-captured-video-in-background-subtractor-m