Ask Your Question

Revision history [back]

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

#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 > threshold_area)
         {
           // store this area into the vector
           //  areaOfContours.push_back(area); 
           //numberOfContoursExceedingThreshold++;
                    count++;

   // Find Momments and  Draw crosshairs on the original image at the component's centroid    
            //  vector<Moments> mu(contours.size());  
        Moments mu = moments(c,false);
                double CentroidX = (int)(mu.m10/mu.m00);
                double CentroidY = (int)(mu.m01/mu.m00);
        line(img, Point(max_x-25, max_y), Point(max_x+25, max_y), CV_RGB(255, 255, 0) );
        line(img, Point(max_x, max_y-25), Point(max_x, max_y+25), CV_RGB(255, 0, 0) );
        // drawCrosshairs(CentroidX, CentroidY, crosshair_size, color, img);
                if(area > maxArea)
                {
                    maxArea = area;
            maxCentroidX = CentroidX;
                    maxCentroidY = CentroidY;
                 }
          }
     }

       /// Number of connected components whose size exceeds a threshold

        cout<<"Frame#"<<frame_count<< "s connected components = " << count<<" "<<endl;
        double minimum_size = 180.0;

       /// print the message to a log file

     fstream logfile;
        logfile.open("logfile.txt");
        if(maxArea > minimum_size)
        {
      logfile<<frame_count<<"\t"<<contours.size()<<"\t"<<maxCentroidX<<"\t"<<maxCentroidY<<"\t"<<maxArea<<endl;
    }

    // log the frame number, total number of connected components
    vector<double>::iterator iter = max_element(areaOfContours.begin(), areaOfContours.end());
    double max_area = 0.0f;
     if(iter != areaOfContours.end()) 
      {
        max_area = *iter;  // max area of all the connected components
       }
     cout << "Frame: " << frame_count
          << " Total # of connected components: " << closed_contours.size()
          << " # of connected components exceeding area threshold: " << areaOfContours.size()
          << " Largest connected component area: " << max_area
          << endl;

    imshow("image", img);
    imshow("foreground mask", fgmask);
    imshow("foreground image", fgimg);
    //imshow("fgmask_contours", fgimg);
    if(!bgimg.empty())
      imshow("mean background image", bgimg );
    char k = (char)waitKey(30);
    if( k == 27 ) 
      {
    break;
      }
    if( k == ' ' )
    {
      update_bg_model = !update_bg_model;
      if(update_bg_model)
        printf("\n Background update is on\n");
      else
        printf("\n Background update is off\n");
    }
    frame_count++;

  return 0;
}
}