C/C++ library for the ViBe algorithm
main-opencv.cpp
Go to the documentation of this file.
1 /**
2  * @file main-opencv.cpp
3  * @date July 2014
4  * @brief An exemplative main file for the use of ViBe and OpenCV
5  */
6 #include <iostream>
7 
8 #include <opencv/cv.h>
9 #include <opencv/highgui.h>
10 
12 
13 using namespace cv;
14 using namespace std;
15 
16 /** Function Headers */
17 void processVideo(char* videoFilename);
18 
19 /**
20  * Displays instructions on how to use this program.
21  */
22 
23 void help()
24 {
25  cout
26  << "--------------------------------------------------------------------------" << endl
27  << "This program shows how to use ViBe with OpenCV " << endl
28  << "Usage:" << endl
29  << "./main-opencv <video filename>" << endl
30  << "for example: ./main-opencv video.avi" << endl
31  << "--------------------------------------------------------------------------" << endl
32  << endl;
33 }
34 
35 /**
36  * Main program. It shows how to use the grayscale version (C1R) and the RGB version (C3R).
37  */
38 int main(int argc, char* argv[])
39 {
40  /* Print help information. */
41  help();
42 
43  /* Check for the input parameter correctness. */
44  if (argc != 2) {
45  cerr <<"Incorrect input" << endl;
46  cerr <<"exiting..." << endl;
47  return EXIT_FAILURE;
48  }
49 
50  /* Create GUI windows. */
51  namedWindow("Frame");
52  namedWindow("Segmentation by ViBe");
53 
54  processVideo(argv[1]);
55 
56  /* Destroy GUI windows. */
57  destroyAllWindows();
58  return EXIT_SUCCESS;
59 }
60 
61 /**
62  * Processes the video. The code of ViBe is included here.
63  *
64  * @param videoFilename The name of the input video file.
65  */
66 void processVideo(char* videoFilename)
67 {
68  /* Create the capture object. */
69  VideoCapture capture(videoFilename);
70 
71  if (!capture.isOpened()) {
72  /* Error in opening the video input. */
73  cerr << "Unable to open video file: " << videoFilename << endl;
74  exit(EXIT_FAILURE);
75  }
76 
77  /* Variables. */
78  static int frameNumber = 1; /* The current frame number */
79  Mat frame; /* Current frame. */
80  Mat segmentationMap; /* Will contain the segmentation map. This is the binary output map. */
81  int keyboard = 0; /* Input from keyboard. Used to stop the program. Enter 'q' to quit. */
82 
83  /* Model for ViBe. */
84  vibeModel_Sequential_t *model = NULL; /* Model used by ViBe. */
85 
86  /* Read input data. ESC or 'q' for quitting. */
87  while ((char)keyboard != 'q' && (char)keyboard != 27) {
88  // Read the current frame.
89  if (!capture.read(frame)) {
90  cerr << "Unable to read next frame." << endl;
91  cerr << "Exiting..." << endl;
92  exit(EXIT_FAILURE);
93  }
94 
95  if ((frameNumber % 100) == 0) { cout << "Frame number = " << frameNumber << endl; }
96 
97  /* Applying ViBe.
98  * If you want to use the grayscale version of ViBe (which is much faster!):
99  * (1) remplace C3R by C1R in this file.
100  * (2) uncomment the next line (cvtColor).
101  */
102  cvtColor(frame, frame, CV_BGR2GRAY);
103 
104  if (frameNumber == 1) {
105  segmentationMap = Mat(frame.rows, frame.cols, CV_8UC1);
107  libvibeModel_Sequential_AllocInit_8u_C1R(model, frame.data, frame.cols, frame.rows);
108  }
109 
110  /* ViBe: Segmentation and updating. */
111  libvibeModel_Sequential_Segmentation_8u_C1R(model, frame.data, segmentationMap.data);
112  libvibeModel_Sequential_Update_8u_C1R(model, frame.data, segmentationMap.data);
113 
114  /* Post-processes the segmentation map. This step is not compulsory.
115  Note that we strongly recommend to use post-processing filters, as they
116  always smooth the segmentation map. For example, the post-processing filter
117  used for the Change Detection dataset (see http://www.changedetection.net/ )
118  is a 5x5 median filter. */
119  medianBlur(segmentationMap, segmentationMap, 3); /* 3x3 median filtering */
120 
121  /* Shows the current frame and the segmentation map. */
122  imshow("Frame", frame);
123  imshow("Segmentation by ViBe", segmentationMap);
124 
125  ++frameNumber;
126 
127  /* Gets the input from the keyboard. */
128  keyboard = waitKey(1);
129  }
130 
131  /* Delete capture object. */
132  capture.release();
133 
134  /* Frees the model. */
136 }
int32_t libvibeModel_Sequential_Segmentation_8u_C1R(vibeModel_Sequential_t *model, const uint8_t *image_data, uint8_t *segmentation_map)
int32_t libvibeModel_Sequential_Free(vibeModel_Sequential_t *model)
Frees all the memory used by the model and deallocates the structure.
int32_t libvibeModel_Sequential_Update_8u_C1R(vibeModel_Sequential_t *model, const uint8_t *image_data, uint8_t *updating_mask)
int32_t libvibeModel_Sequential_AllocInit_8u_C1R(vibeModel_Sequential_t *model, const uint8_t *image_data, const uint32_t width, const uint32_t height)
void help()
Definition: main-opencv.cpp:23
void processVideo(char *videoFilename)
Definition: main-opencv.cpp:66
vibeModel_Sequential_t * libvibeModel_Sequential_New()
Interface for the ViBe library.
struct vibeModel_Sequential vibeModel_Sequential_t
Data structure for the background subtraction model.
int main(int argc, char *argv[])
Definition: main-opencv.cpp:38

License/Copyright

This code is copyrighted by the University of Liège, Belgium. 
It is only shared for research purposes. Please do not distribute it. 
Prof. M. Van Droogenbroeck, July 2014.