motioncells: Initialize m_changed_datafile class field
[platform/upstream/gstreamer.git] / ext / opencv / MotionCells.cpp
1 /*
2  * GStreamer
3  * Copyright (C) 2011 Robert Jobbagy <jobbagy.robert@gmail.com>
4  * Copyright (C) 2011 Nicola Murino <nicola.murino@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
21  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
22  * DEALINGS IN THE SOFTWARE.
23  *
24  * Alternatively, the contents of this file may be used under the
25  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
26  * which case the following provisions apply instead of the ones
27  * mentioned above:
28  *
29  * This library is free software; you can redistribute it and/or
30  * modify it under the terms of the GNU Library General Public
31  * License as published by the Free Software Foundation; either
32  * version 2 of the License, or (at your option) any later version.
33  *
34  * This library is distributed in the hope that it will be useful,
35  * but WITHOUT ANY WARRANTY; without even the implied warranty of
36  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
37  * Library General Public License for more details.
38  *
39  * You should have received a copy of the GNU Library General Public
40  * License along with this library; if not, write to the
41  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
42  * Boston, MA 02110-1301, USA.
43  */
44
45 /* This breaks the build for reasons that aren't entirely clear to me yet */
46 #if 0
47 //#ifdef HAVE_CONFIG_H
48 //#include "config.h"
49 //#endif
50 #endif
51
52 #include <errno.h>
53 #include "MotionCells.h"
54 #include <opencv2/imgproc/imgproc_c.h>
55
56 MotionCells::MotionCells ()
57 {
58   m_framecnt = 0;
59   m_motioncells_idx_count = 0;
60   m_motioncellsidxcstr = NULL;
61   m_saveInDatafile = false;
62   mc_savefile = NULL;
63   m_pcurFrame = NULL;
64   m_pprevFrame = NULL;
65   transparencyimg = NULL;
66   m_pdifferenceImage = NULL;
67   m_pbwImage = NULL;
68   m_initdatafilefailed = new char[BUSMSGLEN];
69   m_savedatafilefailed = new char[BUSMSGLEN];
70   m_initerrorcode = 0;
71   m_saveerrorcode = 0;
72   m_alpha = 0.5;
73   m_beta = 0.5;
74   m_useAlpha = false;
75   m_isVisible = false;
76   m_pCells = NULL;
77   m_gridx = 0;
78   m_gridy = 0;
79   m_cellwidth = 0;
80   m_cellheight = 0;
81   m_sensitivity = 0;
82   m_changed_datafile = false;
83
84   memset (&m_header, 0, sizeof (MotionCellHeader));
85   m_header.headersize = GINT32_TO_BE (MC_HEADER);
86   m_header.type = GINT32_TO_BE (MC_TYPE);
87   m_header.version = GINT32_TO_BE (MC_VERSION);
88   m_header.itemsize = 0;
89   m_header.gridx = 0;
90   m_header.gridy = 0;
91   m_header.starttime = 0;
92 }
93
94 MotionCells::~MotionCells ()
95 {
96   if (mc_savefile) {
97     fclose (mc_savefile);
98     mc_savefile = NULL;
99   }
100   delete[]m_initdatafilefailed;
101   delete[]m_savedatafilefailed;
102   if (m_motioncellsidxcstr)
103     delete[]m_motioncellsidxcstr;
104   if (m_pcurFrame)
105     cvReleaseImage (&m_pcurFrame);
106   if (m_pprevFrame)
107     cvReleaseImage (&m_pprevFrame);
108   if (transparencyimg)
109     cvReleaseImage (&transparencyimg);
110   if (m_pdifferenceImage)
111     cvReleaseImage (&m_pdifferenceImage);
112   if (m_pbwImage)
113     cvReleaseImage (&m_pbwImage);
114 }
115
116 int
117 MotionCells::performDetectionMotionCells (IplImage * p_frame,
118     double p_sensitivity, double p_framerate, int p_gridx, int p_gridy,
119     gint64 timestamp_millisec, bool p_isVisible, bool p_useAlpha,
120     int motionmaskcoord_count, motionmaskcoordrect * motionmaskcoords,
121     int motionmaskcells_count, motioncellidx * motionmaskcellsidx,
122     cellscolor motioncellscolor, int motioncells_count,
123     motioncellidx * motioncellsidx, gint64 starttime, char *p_datafile,
124     bool p_changed_datafile, int p_thickness)
125 {
126
127   int sumframecnt = 0;
128   int ret = 0;
129   CvSize frameSize;
130
131   p_framerate >= 1 ? p_framerate <= 5 ? sumframecnt = 1
132       : p_framerate <= 10 ? sumframecnt = 2
133       : p_framerate <= 15 ? sumframecnt = 3
134       : p_framerate <= 20 ? sumframecnt = 4
135       : p_framerate <= 25 ? sumframecnt = 5
136       : p_framerate <= 30 ? sumframecnt = 6 : sumframecnt = 0 : sumframecnt = 0;
137
138   m_framecnt++;
139   m_changed_datafile = p_changed_datafile;
140   if (m_framecnt >= sumframecnt) {
141     m_useAlpha = p_useAlpha;
142     m_gridx = p_gridx;
143     m_gridy = p_gridy;
144     if (m_changed_datafile) {
145       ret = initDataFile (p_datafile, starttime);
146       if (ret != 0)
147         return ret;
148     }
149
150     frameSize = cvGetSize (p_frame);
151     frameSize.width /= 2;
152     frameSize.height /= 2;
153     setMotionCells (frameSize.width, frameSize.height);
154     m_sensitivity = 1 - p_sensitivity;
155     m_isVisible = p_isVisible;
156     m_pcurFrame = cvCloneImage (p_frame);
157     IplImage *m_pcurgreyImage = cvCreateImage (frameSize, IPL_DEPTH_8U, 1);
158     IplImage *m_pprevgreyImage = cvCreateImage (frameSize, IPL_DEPTH_8U, 1);
159     IplImage *m_pgreyImage = cvCreateImage (frameSize, IPL_DEPTH_8U, 1);
160     IplImage *m_pcurDown =
161         cvCreateImage (frameSize, m_pcurFrame->depth, m_pcurFrame->nChannels);
162     IplImage *m_pprevDown = cvCreateImage (frameSize, m_pprevFrame->depth,
163         m_pprevFrame->nChannels);
164     m_pbwImage = cvCreateImage (frameSize, IPL_DEPTH_8U, 1);
165     cvPyrDown (m_pprevFrame, m_pprevDown);
166     cvCvtColor (m_pprevDown, m_pprevgreyImage, CV_RGB2GRAY);
167     cvPyrDown (m_pcurFrame, m_pcurDown);
168     cvCvtColor (m_pcurDown, m_pcurgreyImage, CV_RGB2GRAY);
169     m_pdifferenceImage = cvCloneImage (m_pcurgreyImage);
170     //cvSmooth(m_pcurgreyImage, m_pcurgreyImage, CV_GAUSSIAN, 3, 0);//TODO camera noise reduce,something smoothing, and rethink runningavg weights
171
172     //Minus the current gray frame from the 8U moving average.
173     cvAbsDiff (m_pprevgreyImage, m_pcurgreyImage, m_pdifferenceImage);
174
175     //Convert the image to black and white.
176     cvAdaptiveThreshold (m_pdifferenceImage, m_pbwImage, 255,
177         CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV, 7);
178
179     // Dilate and erode to get object blobs
180     cvDilate (m_pbwImage, m_pbwImage, NULL, 2);
181     cvErode (m_pbwImage, m_pbwImage, NULL, 2);
182
183     //mask-out the overlay on difference image
184     if (motionmaskcoord_count > 0)
185       performMotionMaskCoords (motionmaskcoords, motionmaskcoord_count);
186     if (motionmaskcells_count > 0)
187       performMotionMask (motionmaskcellsidx, motionmaskcells_count);
188     if (getIsNonZero (m_pbwImage)) {    //detect Motion
189       if (m_MotionCells.size () > 0)    //it contains previous motioncells what we used when frames dropped
190         m_MotionCells.clear ();
191       if (transparencyimg)
192         cvReleaseImage (&transparencyimg);
193       (motioncells_count > 0) ?
194           calculateMotionPercentInMotionCells (motioncellsidx,
195           motioncells_count)
196           : calculateMotionPercentInMotionCells (motionmaskcellsidx, 0);
197
198       transparencyimg = cvCreateImage (cvGetSize (p_frame), p_frame->depth, 3);
199       cvSetZero (transparencyimg);
200       if (m_motioncellsidxcstr)
201         delete[]m_motioncellsidxcstr;
202       m_motioncells_idx_count = m_MotionCells.size () * MSGLEN; //one motion cell idx: (lin idx : col idx,) it's 4 character except last motion cell idx
203       m_motioncellsidxcstr = new char[m_motioncells_idx_count];
204       char *tmpstr = new char[MSGLEN];
205       for (int i = 0; i < MSGLEN; i++)
206         tmpstr[i] = ' ';
207       for (unsigned int i = 0; i < m_MotionCells.size (); i++) {
208         CvPoint pt1, pt2;
209         pt1.x = m_MotionCells.at (i).cell_pt1.x * 2;
210         pt1.y = m_MotionCells.at (i).cell_pt1.y * 2;
211         pt2.x = m_MotionCells.at (i).cell_pt2.x * 2;
212         pt2.y = m_MotionCells.at (i).cell_pt2.y * 2;
213         if (m_useAlpha && m_isVisible) {
214           cvRectangle (transparencyimg,
215               pt1,
216               pt2,
217               CV_RGB (motioncellscolor.B_channel_value,
218                   motioncellscolor.G_channel_value,
219                   motioncellscolor.R_channel_value), CV_FILLED);
220         } else if (m_isVisible) {
221           cvRectangle (p_frame,
222               pt1,
223               pt2,
224               CV_RGB (motioncellscolor.B_channel_value,
225                   motioncellscolor.G_channel_value,
226                   motioncellscolor.R_channel_value), p_thickness);
227         }
228
229         if (i < m_MotionCells.size () - 1) {
230           snprintf (tmpstr, MSGLEN, "%d:%d,", m_MotionCells.at (i).lineidx,
231               m_MotionCells.at (i).colidx);
232         } else {
233           snprintf (tmpstr, MSGLEN, "%d:%d", m_MotionCells.at (i).lineidx,
234               m_MotionCells.at (i).colidx);
235         }
236         if (i == 0)
237           strncpy (m_motioncellsidxcstr, tmpstr, m_motioncells_idx_count);
238         else
239           strcat (m_motioncellsidxcstr, tmpstr);
240       }
241       if (m_MotionCells.size () == 0)
242         strncpy (m_motioncellsidxcstr, " ", m_motioncells_idx_count);
243
244       if (m_useAlpha && m_isVisible) {
245         if (m_MotionCells.size () > 0)
246           blendImages (p_frame, transparencyimg, m_alpha, m_beta);
247       }
248
249       delete[]tmpstr;
250
251       if (mc_savefile && m_saveInDatafile) {
252         ret = saveMotionCells (timestamp_millisec);
253         if (ret != 0)
254           return ret;
255       }
256     } else {
257       m_motioncells_idx_count = 0;
258       if (m_MotionCells.size () > 0)
259         m_MotionCells.clear ();
260       if (transparencyimg)
261         cvReleaseImage (&transparencyimg);
262     }
263
264     if (m_pprevFrame)
265       cvReleaseImage (&m_pprevFrame);
266     m_pprevFrame = cvCloneImage (m_pcurFrame);
267     m_framecnt = 0;
268     if (m_pcurFrame)
269       cvReleaseImage (&m_pcurFrame);
270     if (m_pdifferenceImage)
271       cvReleaseImage (&m_pdifferenceImage);
272     if (m_pcurgreyImage)
273       cvReleaseImage (&m_pcurgreyImage);
274     if (m_pprevgreyImage)
275       cvReleaseImage (&m_pprevgreyImage);
276     if (m_pgreyImage)
277       cvReleaseImage (&m_pgreyImage);
278     if (m_pbwImage)
279       cvReleaseImage (&m_pbwImage);
280     if (m_pprevDown)
281       cvReleaseImage (&m_pprevDown);
282     if (m_pcurDown)
283       cvReleaseImage (&m_pcurDown);
284     if (m_pCells) {
285       for (int i = 0; i < m_gridy; ++i) {
286         delete[]m_pCells[i];
287       }
288       delete[]m_pCells;
289     }
290
291     if (p_framerate <= 5) {
292       if (m_MotionCells.size () > 0)
293         m_MotionCells.clear ();
294       if (transparencyimg)
295         cvReleaseImage (&transparencyimg);
296     }
297   } else {                      //we do frame drop
298     m_motioncells_idx_count = 0;
299     ret = -2;
300     for (unsigned int i = 0; i < m_MotionCells.size (); i++) {
301       CvPoint pt1, pt2;
302       pt1.x = m_MotionCells.at (i).cell_pt1.x * 2;
303       pt1.y = m_MotionCells.at (i).cell_pt1.y * 2;
304       pt2.x = m_MotionCells.at (i).cell_pt2.x * 2;
305       pt2.y = m_MotionCells.at (i).cell_pt2.y * 2;
306       if (m_useAlpha && m_isVisible) {
307         cvRectangle (transparencyimg,
308             pt1,
309             pt2,
310             CV_RGB (motioncellscolor.B_channel_value,
311                 motioncellscolor.G_channel_value,
312                 motioncellscolor.R_channel_value), CV_FILLED);
313       } else if (m_isVisible) {
314         cvRectangle (p_frame,
315             pt1,
316             pt2,
317             CV_RGB (motioncellscolor.B_channel_value,
318                 motioncellscolor.G_channel_value,
319                 motioncellscolor.R_channel_value), p_thickness);
320       }
321
322     }
323     if (m_useAlpha && m_isVisible) {
324       if (m_MotionCells.size () > 0)
325         blendImages (p_frame, transparencyimg, m_alpha, m_beta);
326     }
327   }
328   return ret;
329 }
330
331 int
332 MotionCells::initDataFile (char *p_datafile, gint64 starttime)  //p_date is increased with difference between current and previous buffer ts
333 {
334   MotionCellData mcd;
335   if (strncmp (p_datafile, " ", 1)) {
336     mc_savefile = fopen (p_datafile, "w");
337     if (mc_savefile == NULL) {
338       //fprintf(stderr, "%s %d:initDataFile:fopen:%d (%s)\n", __FILE__, __LINE__, errno,
339       //strerror(errno));
340       strncpy (m_initdatafilefailed, strerror (errno), BUSMSGLEN - 1);
341       m_initerrorcode = errno;
342       return 1;
343     } else {
344       m_saveInDatafile = true;
345     }
346   } else
347     mc_savefile = NULL;
348
349   //it needs these bytes
350   m_header.itemsize =
351       GINT32_TO_BE ((int) ceil (ceil (m_gridx * m_gridy / 8.0) / 4.0) * 4 +
352       sizeof (mcd.timestamp));
353   m_header.gridx = GINT32_TO_BE (m_gridx);
354   m_header.gridy = GINT32_TO_BE (m_gridy);
355   m_header.starttime = GINT64_TO_BE (starttime);
356
357   snprintf (m_header.name, sizeof (m_header.name), "%s %dx%d", MC_VERSIONTEXT,
358       GINT32_FROM_BE (m_header.gridx), GINT32_FROM_BE (m_header.gridy));
359   m_changed_datafile = false;
360   return 0;
361 }
362
363 int
364 MotionCells::saveMotionCells (gint64 timestamp_millisec)
365 {
366
367   MotionCellData mc_data;
368   mc_data.timestamp = GINT32_TO_BE (timestamp_millisec);
369   mc_data.data = NULL;
370   //There is no datafile
371   if (mc_savefile == NULL)
372     return 0;
373
374   if (ftello (mc_savefile) == 0) {
375     //cerr << "Writing out file header"<< m_header.headersize <<":" << sizeof(MotionCellHeader) << " itemsize:"
376     //<< m_header.itemsize << endl;
377     if (fwrite (&m_header, sizeof (MotionCellHeader), 1, mc_savefile) != 1) {
378       //fprintf(stderr, "%s %d:saveMotionCells:fwrite:%d (%s)\n", __FILE__, __LINE__, errno,
379       //strerror(errno));
380       strncpy (m_savedatafilefailed, strerror (errno), BUSMSGLEN - 1);
381       m_saveerrorcode = errno;
382       return -1;
383     }
384   }
385
386   mc_data.data =
387       (char *) calloc (1,
388       GINT32_FROM_BE (m_header.itemsize) - sizeof (mc_data.timestamp));
389   if (mc_data.data == NULL) {
390     //fprintf(stderr, "%s %d:saveMotionCells:calloc:%d (%s)\n", __FILE__, __LINE__, errno,
391     //strerror(errno));
392     strncpy (m_savedatafilefailed, strerror (errno), BUSMSGLEN - 1);
393     m_saveerrorcode = errno;
394     return -1;
395   }
396
397   for (unsigned int i = 0; i < m_MotionCells.size (); i++) {
398     int bitnum =
399         m_MotionCells.at (i).lineidx * GINT32_FROM_BE (m_header.gridx) +
400         m_MotionCells.at (i).colidx;
401     int bytenum = (int) floor (bitnum / 8.0);
402     int shift = bitnum - bytenum * 8;
403     mc_data.data[bytenum] = mc_data.data[bytenum] | (1 << shift);
404     //cerr << "Motion Detected " <<  "line:" << m_MotionCells.at(i).lineidx << " col:" << m_MotionCells.at(i).colidx;
405     //cerr << "    bitnum " << bitnum << " bytenum " << bytenum << " shift " << shift << " value " << (int)mc_data.data[bytenum] << endl;
406   }
407
408   if (fwrite (&mc_data.timestamp, sizeof (mc_data.timestamp), 1,
409           mc_savefile) != 1) {
410     //fprintf(stderr, "%s %d:saveMotionCells:fwrite:%d (%s)\n", __FILE__, __LINE__, errno,
411     //strerror(errno));
412     strncpy (m_savedatafilefailed, strerror (errno), BUSMSGLEN - 1);
413     m_saveerrorcode = errno;
414     return -1;
415   }
416
417   if (fwrite (mc_data.data,
418           GINT32_FROM_BE (m_header.itemsize) - sizeof (mc_data.timestamp), 1,
419           mc_savefile) != 1) {
420     //fprintf(stderr, "%s %d:saveMotionCells:fwrite:%d (%s)\n", __FILE__, __LINE__, errno,
421     //strerror(errno));
422     strncpy (m_savedatafilefailed, strerror (errno), BUSMSGLEN - 1);
423     m_saveerrorcode = errno;
424     return -1;
425   }
426
427   free (mc_data.data);
428   return 0;
429 }
430
431 double
432 MotionCells::calculateMotionPercentInCell (int p_row, int p_col,
433     double *p_cellarea, double *p_motionarea)
434 {
435   double cntpixelsnum = 0;
436   double cntmotionpixelnum = 0;
437
438   int ybegin = floor ((double) p_row * m_cellheight);
439   int yend = floor ((double) (p_row + 1) * m_cellheight);
440   int xbegin = floor ((double) (p_col) * m_cellwidth);
441   int xend = floor ((double) (p_col + 1) * m_cellwidth);
442   int cellw = xend - xbegin;
443   int cellh = yend - ybegin;
444   int cellarea = cellw * cellh;
445   *p_cellarea = cellarea;
446   int thresholdmotionpixelnum = floor ((double) cellarea * m_sensitivity);
447
448   for (int i = ybegin; i < yend; i++) {
449     for (int j = xbegin; j < xend; j++) {
450       cntpixelsnum++;
451       if ((((uchar *) (m_pbwImage->imageData + m_pbwImage->widthStep * i))[j]) >
452           0) {
453         cntmotionpixelnum++;
454         if (cntmotionpixelnum >= thresholdmotionpixelnum) {     //we dont needs calculate anymore
455           *p_motionarea = cntmotionpixelnum;
456           return (cntmotionpixelnum / cntpixelsnum);
457         }
458       }
459       int remainingpixelsnum = cellarea - cntpixelsnum;
460       if ((cntmotionpixelnum + remainingpixelsnum) < thresholdmotionpixelnum) { //moving pixels number will be less than threshold
461         *p_motionarea = 0;
462         return 0;
463       }
464     }
465   }
466
467   return (cntmotionpixelnum / cntpixelsnum);
468 }
469
470 void
471 MotionCells::calculateMotionPercentInMotionCells (motioncellidx *
472     p_motioncellsidx, int p_motioncells_count)
473 {
474   if (p_motioncells_count == 0) {
475     for (int i = 0; i < m_gridy; i++) {
476       for (int j = 0; j < m_gridx; j++) {
477         m_pCells[i][j].MotionPercent = calculateMotionPercentInCell (i, j,
478             &m_pCells[i][j].CellArea, &m_pCells[i][j].MotionArea);
479         m_pCells[i][j].hasMotion =
480             m_sensitivity < m_pCells[i][j].MotionPercent ? true : false;
481         if (m_pCells[i][j].hasMotion) {
482           MotionCellsIdx mci;
483           mci.lineidx = i;
484           mci.colidx = j;
485           mci.cell_pt1.x = floor ((double) j * m_cellwidth);
486           mci.cell_pt1.y = floor ((double) i * m_cellheight);
487           mci.cell_pt2.x = floor ((double) (j + 1) * m_cellwidth);
488           mci.cell_pt2.y = floor ((double) (i + 1) * m_cellheight);
489           int w = mci.cell_pt2.x - mci.cell_pt1.x;
490           int h = mci.cell_pt2.y - mci.cell_pt1.y;
491           mci.motioncell = cvRect (mci.cell_pt1.x, mci.cell_pt1.y, w, h);
492           m_MotionCells.push_back (mci);
493         }
494       }
495     }
496   } else {
497     for (int k = 0; k < p_motioncells_count; ++k) {
498
499       int i = p_motioncellsidx[k].lineidx;
500       int j = p_motioncellsidx[k].columnidx;
501       m_pCells[i][j].MotionPercent =
502           calculateMotionPercentInCell (i, j,
503           &m_pCells[i][j].CellArea, &m_pCells[i][j].MotionArea);
504       m_pCells[i][j].hasMotion =
505           m_pCells[i][j].MotionPercent > m_sensitivity ? true : false;
506       if (m_pCells[i][j].hasMotion) {
507         MotionCellsIdx mci;
508         mci.lineidx = p_motioncellsidx[k].lineidx;
509         mci.colidx = p_motioncellsidx[k].columnidx;
510         mci.cell_pt1.x = floor ((double) j * m_cellwidth);
511         mci.cell_pt1.y = floor ((double) i * m_cellheight);
512         mci.cell_pt2.x = floor ((double) (j + 1) * m_cellwidth);
513         mci.cell_pt2.y = floor ((double) (i + 1) * m_cellheight);
514         int w = mci.cell_pt2.x - mci.cell_pt1.x;
515         int h = mci.cell_pt2.y - mci.cell_pt1.y;
516         mci.motioncell = cvRect (mci.cell_pt1.x, mci.cell_pt1.y, w, h);
517         m_MotionCells.push_back (mci);
518       }
519     }
520   }
521 }
522
523 void
524 MotionCells::performMotionMaskCoords (motionmaskcoordrect * p_motionmaskcoords,
525     int p_motionmaskcoords_count)
526 {
527   CvPoint upperleft;
528   upperleft.x = 0;
529   upperleft.y = 0;
530   CvPoint lowerright;
531   lowerright.x = 0;
532   lowerright.y = 0;
533   for (int i = 0; i < p_motionmaskcoords_count; i++) {
534     upperleft.x = p_motionmaskcoords[i].upper_left_x;
535     upperleft.y = p_motionmaskcoords[i].upper_left_y;
536     lowerright.x = p_motionmaskcoords[i].lower_right_x;
537     lowerright.y = p_motionmaskcoords[i].lower_right_y;
538     cvRectangle (m_pbwImage, upperleft, lowerright, CV_RGB (0, 0, 0),
539         CV_FILLED);
540   }
541 }
542
543 void
544 MotionCells::performMotionMask (motioncellidx * p_motionmaskcellsidx,
545     int p_motionmaskcells_count)
546 {
547   for (int k = 0; k < p_motionmaskcells_count; k++) {
548     int beginy = p_motionmaskcellsidx[k].lineidx * m_cellheight;
549     int beginx = p_motionmaskcellsidx[k].columnidx * m_cellwidth;
550     int endx =
551         (double) p_motionmaskcellsidx[k].columnidx * m_cellwidth + m_cellwidth;
552     int endy =
553         (double) p_motionmaskcellsidx[k].lineidx * m_cellheight + m_cellheight;
554     for (int i = beginy; i < endy; i++)
555       for (int j = beginx; j < endx; j++) {
556         ((uchar *) (m_pbwImage->imageData + m_pbwImage->widthStep * i))[j] = 0;
557       }
558   }
559 }
560
561 ///BGR if we use only OpenCV
562 //RGB if we use gst+OpenCV
563 void
564 MotionCells::blendImages (IplImage * p_actFrame, IplImage * p_cellsFrame,
565     float p_alpha, float p_beta)
566 {
567
568   int height = p_actFrame->height;
569   int width = p_actFrame->width;
570   int step = p_actFrame->widthStep / sizeof (uchar);
571   int channels = p_actFrame->nChannels;
572   int cellstep = p_cellsFrame->widthStep / sizeof (uchar);
573   uchar *curImageData = (uchar *) p_actFrame->imageData;
574   uchar *cellImageData = (uchar *) p_cellsFrame->imageData;
575
576   for (int i = 0; i < height; i++)
577     for (int j = 0; j < width; j++)
578       for (int k = 0; k < channels; k++)
579         if (cellImageData[i * cellstep + j * channels + k] > 0) {
580           curImageData[i * step + j * channels + k] =
581               round ((double) curImageData[i * step + j * channels +
582                   k] * p_alpha + ((double) cellImageData[i * cellstep +
583                       j * channels + k] * p_beta));
584         }
585 }