int main(int argc, char **argv)
{
	CvCapture *capture = 0;
	capture = cvCaptureFromAVI("./dataset/video.avi");
	if(!capture)
	{
		std::cerr << "Cannot open video!" << std::endl;
		return 1;
	}
  
	cascade = (CvHaarClassifierCascade*) cvLoad("cars3.xml");
	storage = cvCreateMemStorage(0);
	assert(cascade && storage && capture);

	ofstream myfile;
	myfile.open ("Statistics.dat");
	
	int resize_factor = 100; // 50% of original image
	int sec=0;				 // Storing time temporarily 
	char buff[DTTMSZ];		 // Buffer of size 10 for storing seconds

	sec=atoi(getDtTm(buff)); // Storing corrent time

	IplImage *frame_aux = cvQueryFrame(capture);
	IplImage *frame = cvCreateImage(cvSize((int)((frame_aux->width*resize_factor)/100) , (int)((frame_aux->height*resize_factor)/100)), frame_aux->depth, frame_aux->nChannels);
  
	// Initializing Background Subtraction Methods
	IBGS *bgs;
	bgs = new PixelBasedAdaptiveSegmenter;
    
	// Initializing Blob Tracking 
	cv::Mat img_blob;
	BlobTracking* blobTracking;
	blobTracking = new BlobTracking;

	// Initializing Vehicle Counting Algorithm
	VehicleCouting* vehicleCouting;
	vehicleCouting = new VehicleCouting;

	int key = 0;
	while(key != 'q')
	{
		frame_aux = cvQueryFrame(capture);
		if(!frame_aux) break;

		cvResize(frame_aux, frame);

		cv::Mat img_input(frame);
		cv::imshow("input", img_input);

		// bgs->process(...) method internally shows the foreground mask image
		cv::Mat img_mask;
		bgs->process(img_input, img_mask);
    
		if(!img_mask.empty())
		{
			// Blob tracking each frame
			blobTracking->process(img_input, img_mask, img_blob);

			// Vehicle counting in each frame
			vehicleCouting->setInput(img_blob);
			vehicleCouting->setTracks(blobTracking->getTracks());
			vehicleCouting->process();
		
			// Haar Classification
			detect(frame);

			//Writing data to a file
			myfile <<(((atoi(getDtTm(buff))-sec)< 0) ?(atoi(getDtTm(buff))+ 60 -sec):(atoi(getDtTm(buff))-sec))<< "\t" <<(vehicleCouting->countAB +vehicleCouting->countBA) << "\n" ;
		}

		key = cvWaitKey(1);
	}

	delete vehicleCouting;
	delete blobTracking;
	delete bgs;

	cvDestroyAllWindows();
	cvReleaseCapture(&capture);
	cvReleaseHaarClassifierCascade(&cascade);
	cvReleaseMemStorage(&storage);
	cvReleaseImage(&frame);
	myfile.close();

	return 0;
}
int main(int argc, char **argv)
{
  std::cout << "Using OpenCV " << CV_MAJOR_VERSION << "." << CV_MINOR_VERSION << "." << CV_SUBMINOR_VERSION << std::endl;

  /* Open video file */
  CvCapture *capture = 0;
  capture = cvCaptureFromAVI("dataset/jakomali.mp4"); //video.avi
  if(!capture){
    std::cerr << "Cannot open video!" << std::endl;
    return 1;
  }
  
  /* Background Subtraction Algorithm */
  IBGS *bgs;
  bgs = new PixelBasedAdaptiveSegmenter;
  
  /* Blob Tracking Algorithm */
  cv::Mat img_blob;
  BlobTracking* blobTracking;
  blobTracking = new BlobTracking;

  /* Vehicle Counting Algorithm */
  VehicleCouting* vehicleCouting;
  vehicleCouting = new VehicleCouting;

  std::cout << "Press 'q' to quit..." << std::endl;
  int key = 0;
  IplImage *frame;
  while(key != 'q')
  {
    frame = cvQueryFrame(capture);
    if(!frame) break;

    cv::Mat img_input(frame);
    //cv::imshow("Input", img_input); // input video

    cv::Mat img_mask;
    bgs->process(img_input, img_mask);
    
    if(!img_mask.empty())
    {
      // Perform blob tracking
      blobTracking->process(img_input, img_mask, img_blob);

      // Perform vehicle counting
      vehicleCouting->setInput(img_blob);
      vehicleCouting->setTracks(blobTracking->getTracks());
      vehicleCouting->process();
    }

    key = cvWaitKey(1);
  }

  delete vehicleCouting;
  delete blobTracking;
  delete bgs;

  cvDestroyAllWindows();
  cvReleaseCapture(&capture);
  
  return 0;
}