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