C/C++ library for the ViBe algorithm
your-main-file-sequential.c
Go to the documentation of this file.
1 /**
2  * @file your-main-file-sequential.c
3  * \brief Shows how to use ViBe in your own C/C++ project
4  *
5 
6  This file contains an example of a main functions that uses the ViBe algorithm
7  implemented in vibe-background-sequential.{o, h}. You should read vibe-background-sequential.h for
8  more information.
9 
10  Full documentation is available online at:
11  http://www.ulg.ac.be/telecom/research/vibe/doc
12 
13  vibe-background.o was compiled by <tt>gcc</tt> using the following command
14  \verbatim
15  $> gcc -std=c99 -O3 -Wall -Werror -pedantic -Wno-unused-function -Wno-unused-parameter -Wno-deprecated -Wno-deprecated-declarations -Wno-sign-compare -Wno-unused-but-set-parameter -c vibe-background-sequential.c
16  \endverbatim
17 
18  This file can be compiled using the following command
19  \verbatim
20  $> gcc -o main -std=c99 -O3 -Wall -Werror -pedantic your-main-file.c vibe-background-sequential.o
21  \endverbatim
22 
23  * @date July 2014
24  * @author Marc Van Droogenbroeck
25 */
26 
28 
29 static int32_t get_image_width(void *stream)
30 {
31  /* Put your own code here. */
32  return(640);
33 }
34 
35 static int32_t get_image_height(void *stream)
36 {
37  /* Put your own code here. */
38  return(480);
39 }
40 
41 static int32_t *acquire_image_C1R(void *stream, uint8_t *image_data, int32_t width, int32_t height)
42 {
43  /* Put your own code here. */
44  memset(image_data, 127, width * height); // Fills the image with the 127 value.
45  return(0);
46 }
47 
48 static int32_t *acquire_image_C3R(void *stream, uint8_t *image_data, int32_t width, int32_t height)
49 {
50  /* Put your own code here. */
51  memset(image_data, 127, (3 * width) * height); // Fills the image with the 127 value.
52  return(0);
53 }
54 
55 /* Simulates a condition to stop after 100 frames. */
56 static int32_t finished(void *stream)
57 {
58  /* Put your own code here. */
59  static int32_t counter = 0;
60  return(!(counter++ < 100));
61 }
62 
63 int main(int argc, char **argv)
64 {
65  /* Your video stream. */
66  void *stream = NULL;
67 
68  /* Get the dimensions of the images of your stream. */
69  int32_t width = get_image_width(stream);
70  int32_t height = get_image_height(stream);
71 
72  /* Allocates memory to store the input images and the segmentation maps. */
73  uint8_t *image_data = NULL;
74  uint8_t *segmentation_map = (uint8_t*)malloc(width * height);
75 
76  /* The pointer to store the ViBe's model. */
77  vibeModel_Sequential_t *model = NULL;
78 
79  // ------------ This is for mono-channel images ( == C1R images) -------------
80  image_data = (uint8_t*)malloc(width * height);
81 
82  /* Acquires your first image. */
83  acquire_image_C1R(stream, image_data, width, height);
84 
85  /* Get a model data structure. */
87 
88  /* Allocates the model and initialize it with the first image. */
89  libvibeModel_Sequential_AllocInit_8u_C1R(model, image_data, width, height);
90 
91  /* Processes all the following frames of your stream: results are stored in "segmentation_map". */
92  while (!finished(stream)) {
93  fprintf(stderr, ".");
94  acquire_image_C1R(stream, image_data, width, height);
95 
96  /* Segmentation step: produces the output mask. */
97  libvibeModel_Sequential_Segmentation_8u_C1R(model, image_data, segmentation_map);
98 
99  /* Next, we update the model. This step is optional. */
100  libvibeModel_Sequential_Update_8u_C1R(model, image_data, segmentation_map);
101 
102  /* segmentation_map is the binary output map that you would like to display, save or
103  use in your own application. Put your own code hereafter. */
104  }
105 
106  fprintf(stderr, "\n");
107 
108  /* Cleanup allocated memory. */
110  free(image_data);
111 
112  // ----------- This is for three-channel images ( == C3R images) -------------
113  /* Data is stored as RGBRGBRGB... or BGRBGRBGR... Three consecutives bytes per pixel thus. */
114  image_data = (uint8_t*)malloc((3 * width) * height);
115 
116  /* Acquires your first image. */
117  acquire_image_C3R(stream, image_data, width, height);
118 
119  /* Get a model data structure. */
121 
122  /* Allocates the model and initialize it with the first image. */
123  libvibeModel_Sequential_AllocInit_8u_C3R(model, image_data, width, height);
124 
125  /* Processes all the following frames of your stream: results are stored in "segmentation_map". */
126  while (!finished(stream)) {
127  fprintf(stderr, ".");
128  acquire_image_C3R(stream, image_data, width, height);
129 
130  /* Segmentation step: produces the output mask. */
131  libvibeModel_Sequential_Segmentation_8u_C3R(model, image_data, segmentation_map);
132 
133  /* Next, we update the model. This step is optional. */
134  libvibeModel_Sequential_Update_8u_C3R(model, image_data, segmentation_map);
135 
136  /* segmentation_map is the binary output map that you would like to display, save or
137  use in your own application. Put your own code hereafter. */
138  }
139 
140  fprintf(stderr, "\n");
141 
142  /* Cleanup allocated memory. */
144  free(image_data);
145 
146  // ---------------------------- General cleanup ------------------------------
147 
148  free(segmentation_map);
149  return(0);
150 }
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_Segmentation_8u_C3R(vibeModel_Sequential_t *model, const uint8_t *image_data, uint8_t *segmentation_map)
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)
int32_t libvibeModel_Sequential_Update_8u_C3R(vibeModel_Sequential_t *model, const uint8_t *image_data, uint8_t *updating_mask)
int32_t libvibeModel_Sequential_AllocInit_8u_C3R(vibeModel_Sequential_t *model, const uint8_t *image_data, const uint32_t width, const uint32_t height)
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.