C++ Open CV Program error. Program doesnt accept video. error: Aborted (core dumped)

asked 2014-06-08 02:06:46 -0600

#include <iostream>
#include "opencv2/core/core.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include "opencv2/video/background_segm.hpp"
#include "opencv2/highgui/highgui.hpp"
#include <stdio.h>
#include <fstream> 
#include <vector>

using namespace std;
using namespace cv;

  vector<vector<Point> > get_closed_contours(vector<vector<Point> > originalContours)
   {
       // Create a vector of vector of points to hold closed contours
       vector<vector<Point> > closed_contours;   
       // resize the vector to match the original contours size
       closed_contours.resize(originalContours.size());
       // iterate through each contour in the originalContours and apply approxPolyDP
       // function
       for(int i=0; i<originalContours.size(); i++)
       {
         approxPolyDP(originalContours[i], closed_contours[i], 0.1, true);
       }
       return closed_contours;
   }
static void help()
{
      printf("\nDo background segmentation, especially demonstrating the use of cvUpdateBGStatModel().\n"
      "Learns the background at the start and then segments.\n"
      "Learning is togged by the space key. Will read from file or camera\n"
      "Usage: \n""./bgfg_segm [--camera]=<use camera, if this key is present>, [--file_name]=<path to movie file> \n\n");
}
const char* keys =
{
  "{c |camera |true | use camera or not}"
    "{fn|file_name|tree.avi | movie file }"
};


//this is a sample for foreground detection functions

int main(int argc, const char** argv)
{
   help();
   CommandLineParser parser(argc, argv, keys);
   bool useCamera = parser.get<bool>("camera");
   string file = parser.get<string>("file_name");
   VideoCapture cap("sn203-12.mov");    //"/home/faculty/mcs/tebo/6825/sn203-12.mov");
  //VideoCapture cap(file);
   bool update_bg_model = true;
   if( !cap.isOpened() )
   {
     printf("can not open camera or video file\n");
     return -1;
   }
   BackgroundSubtractorMOG2 bg_model; //(100, 3, 0.3, 5);
   Mat img, fgmask, fgimg;
   Mat fgmask_ed, fgmask_dilated;
   int n = 0;
   int frame_count = 0;
   double threshold_area = 50.0f;
  int x=0,y=0,max_x=0, max_y=0;

  // print frame rate property of the input file
  int fps = cap.get(CV_CAP_PROP_FPS);
  Size sz = Size((int)cap.get(CV_CAP_PROP_FRAME_WIDTH), (int)cap.get(CV_CAP_PROP_FRAME_HEIGHT));
  // logfile.open("my_bgfg_segm.log", std::fstream::out | std::fstream::trunc);
  for(;;)
  {
    cap >> img;
    // if( img.empty()) break;
    //cvtColor(_img, img, COLOR_BGR2GRAY);
    if( fgimg.empty() )
      fgimg.create(img.size(), img.type());
    //update the model
    bg_model(img, fgmask, update_bg_model ? -1 : 0);

       fgimg = Scalar::all(0);
    img.copyTo(fgimg, fgmask);

    Mat bgimg;
    bg_model.getBackgroundImage(bgimg);

    erode(fgimg,fgmask,cv::Mat());
    dilate(fgmask,fgmask,cv::Mat());

   // create dst where we draw all contours
   // Mat dst;
    //dst.create(img.size(), img.type());
    //dst = Scalar::all(0);

    // extract contours and heirarchy
    vector<vector<Point> > contours;
    vector<Vec4i> hierarchy;

  ///find Contours and connected components

    findContours(fgmask_ed, contours, hierarchy, CV_RETR_TREE, CV_CHAIN_APPROX_SIMPLE,Point(0,0));
    //vector<vector<Point> > contours 
    vector<vector<Point> > closed_contours = get_closed_contours(contours);
    // vector<vec4i> hierarchy;
    // create a vector of doubles to hold the areas of contours in each frame
    // where area exceeds a threshold
    vector<double> areaOfContours;     
     if( contours.size() == 0 ) 
        continue;

    //Draw closed contours

     // int largestcomp=0; 
    int numberOfContoursExceedingThreshold = 0;
    double maxArea = 0, threshArea = 7;
    int maxCentroidX = 0, maxCentroidY = 0;
    int crosshair_size = 17;
    double M00, M01, M10;
    Scalar color(255, 255, 0);
    int count = 0;

    for( double idx=0; idx < contours.size(); idx++)
        {
            const vector<Point> &c = contours[idx];
        // calculate area of each closed contour
        double area = contourArea(contours[idx]);

         if(area ...
(more)
edit retag flag offensive close merge delete