motioncells: Remove unused includes
[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 }
83
84 MotionCells::~MotionCells ()
85 {
86   if (mc_savefile) {
87     fclose (mc_savefile);
88     mc_savefile = NULL;
89   }
90   delete[]m_initdatafilefailed;
91   delete[]m_savedatafilefailed;
92   if (m_motioncellsidxcstr)
93     delete[]m_motioncellsidxcstr;
94   if (m_pcurFrame)
95     cvReleaseImage (&m_pcurFrame);
96   if (m_pprevFrame)
97     cvReleaseImage (&m_pprevFrame);
98   if (transparencyimg)
99     cvReleaseImage (&transparencyimg);
100   if (m_pdifferenceImage)
101     cvReleaseImage (&m_pdifferenceImage);
102   if (m_pbwImage)
103     cvReleaseImage (&m_pbwImage);
104 }
105
106 int
107 MotionCells::performDetectionMotionCells (IplImage * p_frame,
108     double p_sensitivity, double p_framerate, int p_gridx, int p_gridy,
109     gint64 timestamp_millisec, bool p_isVisible, bool p_useAlpha,
110     int motionmaskcoord_count, motionmaskcoordrect * motionmaskcoords,
111     int motionmaskcells_count, motioncellidx * motionmaskcellsidx,
112     cellscolor motioncellscolor, int motioncells_count,
113     motioncellidx * motioncellsidx, gint64 starttime, char *p_datafile,
114     bool p_changed_datafile, int p_thickness)
115 {
116
117   int sumframecnt = 0;
118   int ret = 0;
119   p_framerate >= 1 ? p_framerate <= 5 ? sumframecnt = 1
120       : p_framerate <= 10 ? sumframecnt = 2
121       : p_framerate <= 15 ? sumframecnt = 3
122       : p_framerate <= 20 ? sumframecnt = 4
123       : p_framerate <= 25 ? sumframecnt = 5
124       : p_framerate <= 30 ? sumframecnt = 6 : sumframecnt = 0 : sumframecnt = 0;
125
126   m_framecnt++;
127   m_changed_datafile = p_changed_datafile;
128   if (m_framecnt >= sumframecnt) {
129     m_useAlpha = p_useAlpha;
130     m_gridx = p_gridx;
131     m_gridy = p_gridy;
132     if (m_changed_datafile) {
133       ret = initDataFile (p_datafile, starttime);
134       if (ret != 0)
135         return ret;
136     }
137
138     m_frameSize = cvGetSize (p_frame);
139     m_frameSize.width /= 2;
140     m_frameSize.height /= 2;
141     setMotionCells (m_frameSize.width, m_frameSize.height);
142     m_sensitivity = 1 - p_sensitivity;
143     m_isVisible = p_isVisible;
144     m_pcurFrame = cvCloneImage (p_frame);
145     IplImage *m_pcurgreyImage = cvCreateImage (m_frameSize, IPL_DEPTH_8U, 1);
146     IplImage *m_pprevgreyImage = cvCreateImage (m_frameSize, IPL_DEPTH_8U, 1);
147     IplImage *m_pgreyImage = cvCreateImage (m_frameSize, IPL_DEPTH_8U, 1);
148     IplImage *m_pcurDown =
149         cvCreateImage (m_frameSize, m_pcurFrame->depth, m_pcurFrame->nChannels);
150     IplImage *m_pprevDown = cvCreateImage (m_frameSize, m_pprevFrame->depth,
151         m_pprevFrame->nChannels);
152     m_pbwImage = cvCreateImage (m_frameSize, IPL_DEPTH_8U, 1);
153     cvPyrDown (m_pprevFrame, m_pprevDown);
154     cvCvtColor (m_pprevDown, m_pprevgreyImage, CV_RGB2GRAY);
155     cvPyrDown (m_pcurFrame, m_pcurDown);
156     cvCvtColor (m_pcurDown, m_pcurgreyImage, CV_RGB2GRAY);
157     m_pdifferenceImage = cvCloneImage (m_pcurgreyImage);
158     //cvSmooth(m_pcurgreyImage, m_pcurgreyImage, CV_GAUSSIAN, 3, 0);//TODO camera noise reduce,something smoothing, and rethink runningavg weights
159
160     //Minus the current gray frame from the 8U moving average.
161     cvAbsDiff (m_pprevgreyImage, m_pcurgreyImage, m_pdifferenceImage);
162
163     //Convert the image to black and white.
164     cvAdaptiveThreshold (m_pdifferenceImage, m_pbwImage, 255,
165         CV_ADAPTIVE_THRESH_GAUSSIAN_C, CV_THRESH_BINARY_INV, 7);
166
167     // Dilate and erode to get object blobs
168     cvDilate (m_pbwImage, m_pbwImage, NULL, 2);
169     cvErode (m_pbwImage, m_pbwImage, NULL, 2);
170
171     //mask-out the overlay on difference image
172     if (motionmaskcoord_count > 0)
173       performMotionMaskCoords (motionmaskcoords, motionmaskcoord_count);
174     if (motionmaskcells_count > 0)
175       performMotionMask (motionmaskcellsidx, motionmaskcells_count);
176     if (getIsNonZero (m_pbwImage)) {    //detect Motion
177       if (m_MotionCells.size () > 0)    //it contains previous motioncells what we used when frames dropped
178         m_MotionCells.clear ();
179       if (transparencyimg)
180         cvReleaseImage (&transparencyimg);
181       (motioncells_count > 0) ?
182           calculateMotionPercentInMotionCells (motioncellsidx,
183           motioncells_count)
184           : calculateMotionPercentInMotionCells (motionmaskcellsidx, 0);
185
186       transparencyimg = cvCreateImage (cvGetSize (p_frame), p_frame->depth, 3);
187       cvSetZero (transparencyimg);
188       if (m_motioncellsidxcstr)
189         delete[]m_motioncellsidxcstr;
190       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
191       m_motioncellsidxcstr = new char[m_motioncells_idx_count];
192       char *tmpstr = new char[MSGLEN];
193       for (int i = 0; i < MSGLEN; i++)
194         tmpstr[i] = ' ';
195       for (unsigned int i = 0; i < m_MotionCells.size (); i++) {
196         CvPoint pt1, pt2;
197         pt1.x = m_MotionCells.at (i).cell_pt1.x * 2;
198         pt1.y = m_MotionCells.at (i).cell_pt1.y * 2;
199         pt2.x = m_MotionCells.at (i).cell_pt2.x * 2;
200         pt2.y = m_MotionCells.at (i).cell_pt2.y * 2;
201         if (m_useAlpha && m_isVisible) {
202           cvRectangle (transparencyimg,
203               pt1,
204               pt2,
205               CV_RGB (motioncellscolor.B_channel_value,
206                   motioncellscolor.G_channel_value,
207                   motioncellscolor.R_channel_value), CV_FILLED);
208         } else if (m_isVisible) {
209           cvRectangle (p_frame,
210               pt1,
211               pt2,
212               CV_RGB (motioncellscolor.B_channel_value,
213                   motioncellscolor.G_channel_value,
214                   motioncellscolor.R_channel_value), p_thickness);
215         }
216
217         if (i < m_MotionCells.size () - 1) {
218           snprintf (tmpstr, MSGLEN, "%d:%d,", m_MotionCells.at (i).lineidx,
219               m_MotionCells.at (i).colidx);
220         } else {
221           snprintf (tmpstr, MSGLEN, "%d:%d", m_MotionCells.at (i).lineidx,
222               m_MotionCells.at (i).colidx);
223         }
224         if (i == 0)
225           strncpy (m_motioncellsidxcstr, tmpstr, m_motioncells_idx_count);
226         else
227           strcat (m_motioncellsidxcstr, tmpstr);
228       }
229       if (m_MotionCells.size () == 0)
230         strncpy (m_motioncellsidxcstr, " ", m_motioncells_idx_count);
231
232       if (m_useAlpha && m_isVisible) {
233         if (m_MotionCells.size () > 0)
234           blendImages (p_frame, transparencyimg, m_alpha, m_beta);
235       }
236
237       delete[]tmpstr;
238
239       if (mc_savefile && m_saveInDatafile) {
240         ret = saveMotionCells (timestamp_millisec);
241         if (ret != 0)
242           return ret;
243       }
244     } else {
245       m_motioncells_idx_count = 0;
246       if (m_MotionCells.size () > 0)
247         m_MotionCells.clear ();
248       if (transparencyimg)
249         cvReleaseImage (&transparencyimg);
250     }
251
252     if (m_pprevFrame)
253       cvReleaseImage (&m_pprevFrame);
254     m_pprevFrame = cvCloneImage (m_pcurFrame);
255     m_framecnt = 0;
256     if (m_pcurFrame)
257       cvReleaseImage (&m_pcurFrame);
258     if (m_pdifferenceImage)
259       cvReleaseImage (&m_pdifferenceImage);
260     if (m_pcurgreyImage)
261       cvReleaseImage (&m_pcurgreyImage);
262     if (m_pprevgreyImage)
263       cvReleaseImage (&m_pprevgreyImage);
264     if (m_pgreyImage)
265       cvReleaseImage (&m_pgreyImage);
266     if (m_pbwImage)
267       cvReleaseImage (&m_pbwImage);
268     if (m_pprevDown)
269       cvReleaseImage (&m_pprevDown);
270     if (m_pcurDown)
271       cvReleaseImage (&m_pcurDown);
272     if (m_pCells) {
273       for (int i = 0; i < m_gridy; ++i) {
274         delete[]m_pCells[i];
275       }
276       delete[]m_pCells;
277     }
278
279     if (p_framerate <= 5) {
280       if (m_MotionCells.size () > 0)
281         m_MotionCells.clear ();
282       if (transparencyimg)
283         cvReleaseImage (&transparencyimg);
284     }
285   } else {                      //we do frame drop
286     m_motioncells_idx_count = 0;
287     ret = -2;
288     for (unsigned int i = 0; i < m_MotionCells.size (); i++) {
289       CvPoint pt1, pt2;
290       pt1.x = m_MotionCells.at (i).cell_pt1.x * 2;
291       pt1.y = m_MotionCells.at (i).cell_pt1.y * 2;
292       pt2.x = m_MotionCells.at (i).cell_pt2.x * 2;
293       pt2.y = m_MotionCells.at (i).cell_pt2.y * 2;
294       if (m_useAlpha && m_isVisible) {
295         cvRectangle (transparencyimg,
296             pt1,
297             pt2,
298             CV_RGB (motioncellscolor.B_channel_value,
299                 motioncellscolor.G_channel_value,
300                 motioncellscolor.R_channel_value), CV_FILLED);
301       } else if (m_isVisible) {
302         cvRectangle (p_frame,
303             pt1,
304             pt2,
305             CV_RGB (motioncellscolor.B_channel_value,
306                 motioncellscolor.G_channel_value,
307                 motioncellscolor.R_channel_value), p_thickness);
308       }
309
310     }
311     if (m_useAlpha && m_isVisible) {
312       if (m_MotionCells.size () > 0)
313         blendImages (p_frame, transparencyimg, m_alpha, m_beta);
314     }
315   }
316   return ret;
317 }
318
319 int
320 MotionCells::initDataFile (char *p_datafile, gint64 starttime)  //p_date is increased with difference between current and previous buffer ts
321 {
322   MotionCellData mcd;
323   if (strncmp (p_datafile, " ", 1)) {
324     mc_savefile = fopen (p_datafile, "w");
325     if (mc_savefile == NULL) {
326       //fprintf(stderr, "%s %d:initDataFile:fopen:%d (%s)\n", __FILE__, __LINE__, errno,
327       //strerror(errno));
328       strncpy (m_initdatafilefailed, strerror (errno), BUSMSGLEN - 1);
329       m_initerrorcode = errno;
330       return 1;
331     } else {
332       m_saveInDatafile = true;
333     }
334   } else
335     mc_savefile = NULL;
336   memset (&m_header, 0, sizeof (MotionCellHeader));
337   m_header.headersize = GINT32_TO_BE (MC_HEADER);
338   m_header.type = GINT32_TO_BE (MC_TYPE);
339   m_header.version = GINT32_TO_BE (MC_VERSION);
340   //it needs these bytes
341   m_header.itemsize =
342       GINT32_TO_BE ((int) ceil (ceil (m_gridx * m_gridy / 8.0) / 4.0) * 4 +
343       sizeof (mcd.timestamp));
344   m_header.gridx = GINT32_TO_BE (m_gridx);
345   m_header.gridy = GINT32_TO_BE (m_gridy);
346   m_header.starttime = GINT64_TO_BE (starttime);
347
348   snprintf (m_header.name, sizeof (m_header.name), "%s %dx%d", MC_VERSIONTEXT,
349       GINT32_FROM_BE (m_header.gridx), GINT32_FROM_BE (m_header.gridy));
350   m_changed_datafile = false;
351   return 0;
352 }
353
354 int
355 MotionCells::saveMotionCells (gint64 timestamp_millisec)
356 {
357
358   MotionCellData mc_data;
359   mc_data.timestamp = GINT32_TO_BE (timestamp_millisec);
360   mc_data.data = NULL;
361   //There is no datafile
362   if (mc_savefile == NULL)
363     return 0;
364
365   if (ftello (mc_savefile) == 0) {
366     //cerr << "Writing out file header"<< m_header.headersize <<":" << sizeof(MotionCellHeader) << " itemsize:"
367     //<< m_header.itemsize << endl;
368     if (fwrite (&m_header, sizeof (MotionCellHeader), 1, mc_savefile) != 1) {
369       //fprintf(stderr, "%s %d:saveMotionCells:fwrite:%d (%s)\n", __FILE__, __LINE__, errno,
370       //strerror(errno));
371       strncpy (m_savedatafilefailed, strerror (errno), BUSMSGLEN - 1);
372       m_saveerrorcode = errno;
373       return -1;
374     }
375   }
376
377   mc_data.data =
378       (char *) calloc (1,
379       GINT32_FROM_BE (m_header.itemsize) - sizeof (mc_data.timestamp));
380   if (mc_data.data == NULL) {
381     //fprintf(stderr, "%s %d:saveMotionCells:calloc:%d (%s)\n", __FILE__, __LINE__, errno,
382     //strerror(errno));
383     strncpy (m_savedatafilefailed, strerror (errno), BUSMSGLEN - 1);
384     m_saveerrorcode = errno;
385     return -1;
386   }
387
388   for (unsigned int i = 0; i < m_MotionCells.size (); i++) {
389     int bitnum =
390         m_MotionCells.at (i).lineidx * GINT32_FROM_BE (m_header.gridx) +
391         m_MotionCells.at (i).colidx;
392     int bytenum = (int) floor (bitnum / 8.0);
393     int shift = bitnum - bytenum * 8;
394     mc_data.data[bytenum] = mc_data.data[bytenum] | (1 << shift);
395     //cerr << "Motion Detected " <<  "line:" << m_MotionCells.at(i).lineidx << " col:" << m_MotionCells.at(i).colidx;
396     //cerr << "    bitnum " << bitnum << " bytenum " << bytenum << " shift " << shift << " value " << (int)mc_data.data[bytenum] << endl;
397   }
398
399   if (fwrite (&mc_data.timestamp, sizeof (mc_data.timestamp), 1,
400           mc_savefile) != 1) {
401     //fprintf(stderr, "%s %d:saveMotionCells:fwrite:%d (%s)\n", __FILE__, __LINE__, errno,
402     //strerror(errno));
403     strncpy (m_savedatafilefailed, strerror (errno), BUSMSGLEN - 1);
404     m_saveerrorcode = errno;
405     return -1;
406   }
407
408   if (fwrite (mc_data.data,
409           GINT32_FROM_BE (m_header.itemsize) - sizeof (mc_data.timestamp), 1,
410           mc_savefile) != 1) {
411     //fprintf(stderr, "%s %d:saveMotionCells:fwrite:%d (%s)\n", __FILE__, __LINE__, errno,
412     //strerror(errno));
413     strncpy (m_savedatafilefailed, strerror (errno), BUSMSGLEN - 1);
414     m_saveerrorcode = errno;
415     return -1;
416   }
417
418   free (mc_data.data);
419   return 0;
420 }
421
422 double
423 MotionCells::calculateMotionPercentInCell (int p_row, int p_col,
424     double *p_cellarea, double *p_motionarea)
425 {
426   double cntpixelsnum = 0;
427   double cntmotionpixelnum = 0;
428
429   int ybegin = floor ((double) p_row * m_cellheight);
430   int yend = floor ((double) (p_row + 1) * m_cellheight);
431   int xbegin = floor ((double) (p_col) * m_cellwidth);
432   int xend = floor ((double) (p_col + 1) * m_cellwidth);
433   int cellw = xend - xbegin;
434   int cellh = yend - ybegin;
435   int cellarea = cellw * cellh;
436   *p_cellarea = cellarea;
437   int thresholdmotionpixelnum = floor ((double) cellarea * m_sensitivity);
438
439   for (int i = ybegin; i < yend; i++) {
440     for (int j = xbegin; j < xend; j++) {
441       cntpixelsnum++;
442       if ((((uchar *) (m_pbwImage->imageData + m_pbwImage->widthStep * i))[j]) >
443           0) {
444         cntmotionpixelnum++;
445         if (cntmotionpixelnum >= thresholdmotionpixelnum) {     //we dont needs calculate anymore
446           *p_motionarea = cntmotionpixelnum;
447           return (cntmotionpixelnum / cntpixelsnum);
448         }
449       }
450       int remainingpixelsnum = cellarea - cntpixelsnum;
451       if ((cntmotionpixelnum + remainingpixelsnum) < thresholdmotionpixelnum) { //moving pixels number will be less than threshold
452         *p_motionarea = 0;
453         return 0;
454       }
455     }
456   }
457
458   return (cntmotionpixelnum / cntpixelsnum);
459 }
460
461 void
462 MotionCells::calculateMotionPercentInMotionCells (motioncellidx *
463     p_motioncellsidx, int p_motioncells_count)
464 {
465   if (p_motioncells_count == 0) {
466     for (int i = 0; i < m_gridy; i++) {
467       for (int j = 0; j < m_gridx; j++) {
468         m_pCells[i][j].MotionPercent = calculateMotionPercentInCell (i, j,
469             &m_pCells[i][j].CellArea, &m_pCells[i][j].MotionArea);
470         m_pCells[i][j].hasMotion =
471             m_sensitivity < m_pCells[i][j].MotionPercent ? true : false;
472         if (m_pCells[i][j].hasMotion) {
473           MotionCellsIdx mci;
474           mci.lineidx = i;
475           mci.colidx = j;
476           mci.cell_pt1.x = floor ((double) j * m_cellwidth);
477           mci.cell_pt1.y = floor ((double) i * m_cellheight);
478           mci.cell_pt2.x = floor ((double) (j + 1) * m_cellwidth);
479           mci.cell_pt2.y = floor ((double) (i + 1) * m_cellheight);
480           int w = mci.cell_pt2.x - mci.cell_pt1.x;
481           int h = mci.cell_pt2.y - mci.cell_pt1.y;
482           mci.motioncell = cvRect (mci.cell_pt1.x, mci.cell_pt1.y, w, h);
483           m_MotionCells.push_back (mci);
484         }
485       }
486     }
487   } else {
488     for (int k = 0; k < p_motioncells_count; ++k) {
489
490       int i = p_motioncellsidx[k].lineidx;
491       int j = p_motioncellsidx[k].columnidx;
492       m_pCells[i][j].MotionPercent =
493           calculateMotionPercentInCell (i, j,
494           &m_pCells[i][j].CellArea, &m_pCells[i][j].MotionArea);
495       m_pCells[i][j].hasMotion =
496           m_pCells[i][j].MotionPercent > m_sensitivity ? true : false;
497       if (m_pCells[i][j].hasMotion) {
498         MotionCellsIdx mci;
499         mci.lineidx = p_motioncellsidx[k].lineidx;
500         mci.colidx = p_motioncellsidx[k].columnidx;
501         mci.cell_pt1.x = floor ((double) j * m_cellwidth);
502         mci.cell_pt1.y = floor ((double) i * m_cellheight);
503         mci.cell_pt2.x = floor ((double) (j + 1) * m_cellwidth);
504         mci.cell_pt2.y = floor ((double) (i + 1) * m_cellheight);
505         int w = mci.cell_pt2.x - mci.cell_pt1.x;
506         int h = mci.cell_pt2.y - mci.cell_pt1.y;
507         mci.motioncell = cvRect (mci.cell_pt1.x, mci.cell_pt1.y, w, h);
508         m_MotionCells.push_back (mci);
509       }
510     }
511   }
512 }
513
514 void
515 MotionCells::performMotionMaskCoords (motionmaskcoordrect * p_motionmaskcoords,
516     int p_motionmaskcoords_count)
517 {
518   CvPoint upperleft;
519   upperleft.x = 0;
520   upperleft.y = 0;
521   CvPoint lowerright;
522   lowerright.x = 0;
523   lowerright.y = 0;
524   for (int i = 0; i < p_motionmaskcoords_count; i++) {
525     upperleft.x = p_motionmaskcoords[i].upper_left_x;
526     upperleft.y = p_motionmaskcoords[i].upper_left_y;
527     lowerright.x = p_motionmaskcoords[i].lower_right_x;
528     lowerright.y = p_motionmaskcoords[i].lower_right_y;
529     cvRectangle (m_pbwImage, upperleft, lowerright, CV_RGB (0, 0, 0),
530         CV_FILLED);
531   }
532 }
533
534 void
535 MotionCells::performMotionMask (motioncellidx * p_motionmaskcellsidx,
536     int p_motionmaskcells_count)
537 {
538   for (int k = 0; k < p_motionmaskcells_count; k++) {
539     int beginy = p_motionmaskcellsidx[k].lineidx * m_cellheight;
540     int beginx = p_motionmaskcellsidx[k].columnidx * m_cellwidth;
541     int endx =
542         (double) p_motionmaskcellsidx[k].columnidx * m_cellwidth + m_cellwidth;
543     int endy =
544         (double) p_motionmaskcellsidx[k].lineidx * m_cellheight + m_cellheight;
545     for (int i = beginy; i < endy; i++)
546       for (int j = beginx; j < endx; j++) {
547         ((uchar *) (m_pbwImage->imageData + m_pbwImage->widthStep * i))[j] = 0;
548       }
549   }
550 }
551
552 ///BGR if we use only OpenCV
553 //RGB if we use gst+OpenCV
554 void
555 MotionCells::blendImages (IplImage * p_actFrame, IplImage * p_cellsFrame,
556     float p_alpha, float p_beta)
557 {
558
559   int height = p_actFrame->height;
560   int width = p_actFrame->width;
561   int step = p_actFrame->widthStep / sizeof (uchar);
562   int channels = p_actFrame->nChannels;
563   int cellstep = p_cellsFrame->widthStep / sizeof (uchar);
564   uchar *curImageData = (uchar *) p_actFrame->imageData;
565   uchar *cellImageData = (uchar *) p_cellsFrame->imageData;
566
567   for (int i = 0; i < height; i++)
568     for (int j = 0; j < width; j++)
569       for (int k = 0; k < channels; k++)
570         if (cellImageData[i * cellstep + j * channels + k] > 0) {
571           curImageData[i * step + j * channels + k] =
572               round ((double) curImageData[i * step + j * channels +
573                   k] * p_alpha + ((double) cellImageData[i * cellstep +
574                       j * channels + k] * p_beta));
575         }
576 }