3 * Copyright (C) 2011 Robert Jobbagy <jobbagy.robert@gmail.com>
4 * Copyright (C) 2011 Nicola Murino <nicola.murino@gmail.com>
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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.
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
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.
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.
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.
45 #ifndef MOTIONCELLS_H_
46 #define MOTIONCELLS_H_
48 #include <opencv2/core/core_c.h>
50 #include <highgui.h> // includes highGUI definitions
52 #ifdef HAVE_OPENCV2_HIGHGUI_HIGHGUI_C_H
53 #include <opencv2/highgui/highgui_c.h> // includes highGUI definitions
63 #define MC_VERSIONTEXT "MotionCells-1"
69 struct MotionCellHeader{
77 char name[MC_HEADER - 32];
80 struct MotionCellData{
90 } motionmaskcoordrect;
107 double MotionPercent;
111 struct MotionCellsIdx
114 //Points for the edges of the rectangle.
121 struct OverlayRegions
132 virtual ~ MotionCells ();
134 int performDetectionMotionCells (IplImage * p_frame, double p_sensitivity,
135 double p_framerate, int p_gridx, int p_gridy, gint64 timestamp_millisec,
136 bool p_isVisble, bool p_useAlpha, int motionmaskcoord_count,
137 motionmaskcoordrect * motionmaskcoords, int motionmaskcells_count,
138 motioncellidx * motionmaskcellsidx, cellscolor motioncellscolor,
139 int motioncells_count, motioncellidx * motioncellsidx, gint64 starttime,
140 char *datafile, bool p_changed_datafile, int p_thickness);
142 void setPrevFrame (IplImage * p_prevframe)
144 m_pprevFrame = cvCloneImage (p_prevframe);
146 char *getMotionCellsIdx ()
148 return m_motioncellsidxcstr;
151 int getMotionCellsIdxCount ()
153 return m_motioncells_idx_count;
156 bool getChangedDataFile ()
158 return m_changed_datafile;
161 char *getDatafileInitFailed ()
163 return m_initdatafilefailed;
166 char *getDatafileSaveFailed ()
168 return m_savedatafilefailed;
171 int getInitErrorCode ()
173 return m_initerrorcode;
176 int getSaveErrorCode ()
178 return m_saveerrorcode;
184 fclose (mc_savefile);
186 m_saveInDatafile = false;
192 double calculateMotionPercentInCell (int p_row, int p_col, double *p_cellarea,
193 double *p_motionarea);
194 void performMotionMaskCoords (motionmaskcoordrect * p_motionmaskcoords,
195 int p_motionmaskcoords_count);
196 void performMotionMask (motioncellidx * p_motionmaskcellsidx,
197 int p_motionmaskcells_count);
198 void calculateMotionPercentInMotionCells (motioncellidx *
199 p_motionmaskcellsidx, int p_motionmaskcells_count = 0);
200 int saveMotionCells (gint64 timestamp_millisec);
201 int initDataFile (char *p_datafile, gint64 starttime);
202 void blendImages (IplImage * p_actFrame, IplImage * p_cellsFrame,
203 float p_alpha, float p_beta);
205 void setData (IplImage * img, int lin, int col, uchar valor)
207 ((uchar *) (img->imageData + img->widthStep * lin))[col] = valor;
210 uchar getData (IplImage * img, int lin, int col)
212 return ((uchar *) (img->imageData + img->widthStep * lin))[col];
215 bool getIsNonZero (IplImage * img)
217 for (int lin = 0; lin < img->height; lin++)
218 for (int col = 0; col < img->width; col++) {
219 if ((((uchar *) (img->imageData + img->widthStep * lin))[col]) > 0)
225 void setMotionCells (int p_frameWidth, int p_frameHeight)
227 m_cellwidth = (double) p_frameWidth / (double) m_gridx;
228 m_cellheight = (double) p_frameHeight / (double) m_gridy;
229 m_pCells = new Cell *[m_gridy];
230 for (int i = 0; i < m_gridy; i++)
231 m_pCells[i] = new Cell[m_gridx];
234 for (int i = 0; i < m_gridy; i++)
235 for (int j = 0; j < m_gridx; j++) {
236 m_pCells[i][j].MotionArea = 0;
237 m_pCells[i][j].CellArea = 0;
238 m_pCells[i][j].MotionPercent = 0;
239 m_pCells[i][j].hasMotion = false;
243 IplImage *m_pcurFrame, *m_pprevFrame, *m_pdifferenceImage,
244 *m_pbwImage,*transparencyimg;
246 bool m_isVisible, m_changed_datafile, m_useAlpha, m_saveInDatafile;
248 vector < MotionCellsIdx > m_MotionCells;
249 vector < OverlayRegions > m_OverlayRegions;
250 int m_gridx, m_gridy;
251 double m_cellwidth, m_cellheight;
252 double m_alpha, m_beta;
253 double m_sensitivity;
254 int m_framecnt, m_motioncells_idx_count, m_initerrorcode, m_saveerrorcode;
255 char *m_motioncellsidxcstr, *m_initdatafilefailed, *m_savedatafilefailed;
257 MotionCellHeader m_header;
261 #endif /* MOTIONCELLS_H_ */