aboutsummaryrefslogtreecommitdiff
path: root/pix_opencv_bgstats.cc
blob: 639e44c471e9844871228289194ab72792bf7e5c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
////////////////////////////////////////////////////////
//
// GEM - Graphics Environment for Multimedia
//
// zmoelnig@iem.kug.ac.at
//
// Implementation file
//
//    Copyright (c) 1997-2000 Mark Danks.
//    Copyright (c) Günther Geiger.
//    Copyright (c) 2001-2002 IOhannes m zmoelnig. forum::für::umläute. IEM
//    Copyright (c) 2002 James Tittle & Chris Clepper
//    For information on usage and redistribution, and for a DISCLAIMER OF ALL
//    WARRANTIES, see the file, "GEM.LICENSE.TERMS" in this distribution.
//
/////////////////////////////////////////////////////////

#include "pix_opencv_bgstats.h"

CPPEXTERN_NEW(pix_opencv_bgstats)

/////////////////////////////////////////////////////////
//
// pix_opencv_bgstats
//
/////////////////////////////////////////////////////////
// Constructor
//
/////////////////////////////////////////////////////////
pix_opencv_bgstats :: pix_opencv_bgstats()
{ 
  comp_xsize=320;
  comp_ysize=240;

  x_erode = 2;
  x_minarea = 10*10;
  x_delta = 0.1; // 0.1 seconds
  x_frames = 0;

  foreground = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 3);
  incoming = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 3);
  rgba = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 4);
  rgb  = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 3);
  gray = cvCreateImage(cvSize(comp_xsize,comp_ysize), IPL_DEPTH_8U, 1);
  x_model = NULL;

}

/////////////////////////////////////////////////////////
// Destructor
//
/////////////////////////////////////////////////////////
pix_opencv_bgstats :: ~pix_opencv_bgstats()
{ 
   //Destroy cv_images to clean memory
   cvReleaseImage(&rgba);
   cvReleaseImage(&rgb);
   cvReleaseImage(&gray);
   cvReleaseImage(&foreground);
   cvReleaseImage(&incoming);
}

/////////////////////////////////////////////////////////
// processImage
//
/////////////////////////////////////////////////////////
void pix_opencv_bgstats :: processRGBAImage(imageStruct &image)
{

  if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!rgba)) {

	this->comp_xsize = image.xsize;
	this->comp_ysize = image.ysize;
        x_frames = 0;

    	//Destroy cv_images to clean memory
	cvReleaseImage(&rgba);
    	cvReleaseImage(&rgb);
    	cvReleaseImage(&gray);
        cvReleaseImage(&foreground);
        cvReleaseImage(&incoming);

	//create the orig image with new size
        rgba = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4);
        rgb  = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3);
    	gray = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 1);

        incoming  = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3);
        foreground  = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3);

        cvReleaseBGStatModel( &x_model );
        x_model = NULL;

    }
    memcpy( rgba->imageData, image.data, image.xsize*image.ysize*4 );
    cvCvtColor(rgba, incoming, CV_BGRA2BGR);

    if ( x_model == NULL )
    {
      x_modelparams.Lc = CV_BGFG_FGD_LC;
      x_modelparams.N1c = CV_BGFG_FGD_N1C;
      x_modelparams.N2c = CV_BGFG_FGD_N2C;
      x_modelparams.Lcc = CV_BGFG_FGD_LCC;
      x_modelparams.N1cc = CV_BGFG_FGD_N1CC;
      x_modelparams.N2cc = CV_BGFG_FGD_N2CC;
      x_modelparams.is_obj_without_holes = 1;
      x_modelparams.alpha1 = CV_BGFG_FGD_ALPHA_1;
      x_modelparams.alpha2 = CV_BGFG_FGD_ALPHA_2;
      x_modelparams.alpha3 = CV_BGFG_FGD_ALPHA_3;
      x_modelparams.T = CV_BGFG_FGD_T;
      x_modelparams.perform_morphing = x_erode;
      x_modelparams.minArea = x_minarea;
      x_modelparams.delta = x_delta;
      x_model = cvCreateFGDStatModel( incoming, &x_modelparams );
    }
    else if ( x_frames == 5 )
    {
      // strange model stabilize after a few frames
      cvReleaseBGStatModel( &x_model );
      x_model = cvCreateFGDStatModel( incoming, &x_modelparams );
    }
    else
    {
       cvUpdateBGStatModel( incoming, x_model );
    }

    x_frames++;
    cvCvtColor(x_model->foreground, rgba, CV_GRAY2BGRA);
    
    memcpy( image.data, rgba->imageData, image.xsize*image.ysize*4 );
}

void pix_opencv_bgstats :: processRGBImage(imageStruct &image)
{

  if ((this->comp_xsize!=image.xsize)||(this->comp_ysize!=image.ysize)||(!rgba)) {

	this->comp_xsize = image.xsize;
	this->comp_ysize = image.ysize;
        x_frames = 0;

    	//Destroy cv_images to clean memory
	cvReleaseImage(&rgba);
    	cvReleaseImage(&rgb);
    	cvReleaseImage(&gray);
        cvReleaseImage(&foreground);
        cvReleaseImage(&incoming);

	//create the orig image with new size
        rgba = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 4);
        rgb  = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3);
    	gray = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 1);

        incoming  = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3);
        foreground  = cvCreateImage(cvSize(image.xsize,image.ysize), IPL_DEPTH_8U, 3);

        cvReleaseBGStatModel( &x_model );
        x_model = NULL;
    }
    memcpy( rgb->imageData, image.data, image.xsize*image.ysize*3 );
    memcpy( incoming->imageData, image.data, image.xsize*image.ysize*3 );
    
    if ( x_model == NULL )
    {
      x_modelparams.Lc = CV_BGFG_FGD_LC;
      x_modelparams.N1c = CV_BGFG_FGD_N1C;
      x_modelparams.N2c = CV_BGFG_FGD_N2C;
      x_modelparams.Lcc = CV_BGFG_FGD_LCC;
      x_modelparams.N1cc = CV_BGFG_FGD_N1CC;
      x_modelparams.N2cc = CV_BGFG_FGD_N2CC;
      x_modelparams.is_obj_without_holes = 1;
      x_modelparams.alpha1 = CV_BGFG_FGD_ALPHA_1;
      x_modelparams.alpha2 = CV_BGFG_FGD_ALPHA_2;
      x_modelparams.alpha3 = CV_BGFG_FGD_ALPHA_3;
      x_modelparams.T = CV_BGFG_FGD_T;
      x_modelparams.perform_morphing = x_erode;
      x_modelparams.minArea = x_minarea;
      x_modelparams.delta = x_delta;
      x_model = cvCreateFGDStatModel( incoming, &x_modelparams );
    }
    else if ( x_frames == 5 )
    {
      // strange model stabilize after a few frames
      cvReleaseBGStatModel( &x_model );
      x_model = cvCreateFGDStatModel( incoming, &x_modelparams );
    }
    else
    {
       cvUpdateBGStatModel( incoming, x_model );
    }

    x_frames++;
    cvCvtColor(x_model->foreground, rgb, CV_GRAY2BGR);
    
    memcpy( image.data, rgb->imageData, image.xsize*image.ysize*3 );

}

void pix_opencv_bgstats :: processYUVImage(imageStruct &image)
{
  post( "pix_opencv_bgstats : yuv format not supported" );
}
    	
void pix_opencv_bgstats :: processGrayImage(imageStruct &image)
{ 
  post( "pix_opencv_bgstats : gray format not supported" );
}

/////////////////////////////////////////////////////////
// floatThreshMess
//
/////////////////////////////////////////////////////////
void pix_opencv_bgstats :: floatMinAreaMess (float minarea)
{
  if ( ( (int)minarea>0 ) && ( x_model != NULL ) )
  {
       x_minarea = minarea;
       x_modelparams.minArea = minarea;
       cvReleaseBGStatModel( &x_model );
       x_model = cvCreateFGDStatModel( incoming, &x_modelparams );
  }
}

void pix_opencv_bgstats :: floatErodeMess(float erode)
{
  if ( ( (int)erode>0 ) && ( x_model != NULL ) )
  {
       x_erode = (int)erode;
       x_modelparams.perform_morphing = (int)erode;
       cvReleaseBGStatModel( &x_model );
       x_model = cvCreateFGDStatModel( incoming, &x_modelparams );
  }
}

void pix_opencv_bgstats :: floatDeltaMess(float delta)
{
   if ( ( delta>0.0 ) && ( x_model != NULL ) )
   {
       x_delta = delta;
       x_modelparams.delta = delta;
       cvReleaseBGStatModel( &x_model );
       x_model = cvCreateFGDStatModel( incoming, &x_modelparams );
   }
}

void pix_opencv_bgstats :: resetMess ()
{
  if ( x_model != NULL )
  {
       cvReleaseBGStatModel( &x_model );
       x_model = cvCreateFGDStatModel( incoming, &x_modelparams );
  }
}

/////////////////////////////////////////////////////////
// static member function
//
/////////////////////////////////////////////////////////
void pix_opencv_bgstats :: obj_setupCallback(t_class *classPtr)
{
  class_addmethod(classPtr, (t_method)&pix_opencv_bgstats::floatMinAreaMessCallback,
  		  gensym("minarea"), A_FLOAT, A_NULL);
  class_addmethod(classPtr, (t_method)&pix_opencv_bgstats::floatErodeMessCallback,
  		  gensym("erode"), A_FLOAT, A_NULL);
  class_addmethod(classPtr, (t_method)&pix_opencv_bgstats::floatDeltaMessCallback,
  		  gensym("delta"), A_FLOAT, A_NULL);
  class_addmethod(classPtr, (t_method)&pix_opencv_bgstats::resetMessCallback,
  		  gensym("reset"), A_NULL);

}

void pix_opencv_bgstats :: floatMinAreaMessCallback(void *data, t_floatarg minarea)
{
  GetMyClass(data)->floatMinAreaMess((float)minarea);
}

void pix_opencv_bgstats :: floatErodeMessCallback(void *data, t_floatarg erode)
{
  GetMyClass(data)->floatErodeMess((float)erode);
}

void pix_opencv_bgstats :: floatDeltaMessCallback(void *data, t_floatarg delta)
{
  GetMyClass(data)->floatDeltaMess((float)delta);
}

void pix_opencv_bgstats :: resetMessCallback(void *data)
{
  GetMyClass(data)->resetMess();
}