如何在背景减法模型中使用捕获的视频播放和检测对象? [英] How to play and detect an object using captured video in background subtractor model?

查看:154
本文介绍了如何在背景减法模型中使用捕获的视频播放和检测对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家!我正在使用opencv2.4.2。实际上我正在进行物体检测项目。我尝试使用BackgroundSubtractorMOG模型。
但是我无法从我的电脑加载视频文件。在实时运行时,下面的分段代码工作正常。
我已经使用帧差分方法实现了对象检测。现在我想从背景中分割整个对象。我有静态背景。所以任何人都可以帮助我在下面的代码中如何从捕获的视频中分割对象。还有如何加载视频文件?
谢谢。

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


推荐答案

opencv中的视频加载对我有用。要加载视频,您可以尝试这样的操作。捕获帧后,您可以在循环中进行处理,也可以调用单独的函数。

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);

   }
}

或者你可以做点什么

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

这篇关于如何在背景减法模型中使用捕获的视频播放和检测对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆