motioncells: Remove unused includes
[platform/upstream/gstreamer.git] / ext / opencv / MotionCells.h
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 #ifndef MOTIONCELLS_H_
46 #define MOTIONCELLS_H_
47
48 #include <opencv2/core/core_c.h>
49 #ifdef HAVE_HIGHGUI_H
50 #include <highgui.h>            // includes highGUI definitions
51 #endif
52 #ifdef HAVE_OPENCV2_HIGHGUI_HIGHGUI_C_H
53 #include <opencv2/highgui/highgui_c.h>            // includes highGUI definitions
54 #endif
55 #include <fstream>
56 #include <vector>
57 #include <glib.h>
58
59 //MotionCells defines
60 #define MC_HEADER 64
61 #define MC_TYPE 1
62 #define MC_VERSION 1
63 #define MC_VERSIONTEXT "MotionCells-1"
64 #define MSGLEN 6
65 #define BUSMSGLEN 20
66
67 using namespace std;
68
69 struct MotionCellHeader{
70         gint32 headersize;
71         gint32 type;
72         gint32 version;
73         gint32 itemsize;
74         gint32 gridx;
75         gint32 gridy;
76         gint64 starttime;
77         char name[MC_HEADER - 32];
78 };
79
80 struct MotionCellData{
81         gint32 timestamp;
82         char *data;
83 };
84
85 typedef struct {
86         int upper_left_x;
87         int upper_left_y;
88         int lower_right_x;
89         int lower_right_y;
90 } motionmaskcoordrect;
91
92 typedef struct {
93         int R_channel_value;
94         int G_channel_value;
95         int B_channel_value;
96 } cellscolor;
97
98 typedef struct {
99         int lineidx;
100         int columnidx;
101 } motioncellidx;
102
103 struct Cell
104 {
105   double MotionArea;
106   double CellArea;
107   double MotionPercent;
108   bool hasMotion;
109 };
110
111 struct MotionCellsIdx
112 {
113   CvRect motioncell;
114   //Points for the edges of the rectangle.
115   CvPoint cell_pt1;
116   CvPoint cell_pt2;
117   int lineidx;
118   int colidx;
119 };
120
121 struct OverlayRegions
122 {
123   CvPoint upperleft;
124   CvPoint lowerright;
125 };
126
127 class MotionCells
128 {
129 public:
130
131   MotionCells ();
132   virtual ~ MotionCells ();
133
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);
141
142   void setPrevFrame (IplImage * p_prevframe)
143   {
144     m_pprevFrame = cvCloneImage (p_prevframe);
145   }
146   char *getMotionCellsIdx ()
147   {
148     return m_motioncellsidxcstr;
149   }
150
151   int getMotionCellsIdxCount ()
152   {
153     return m_motioncells_idx_count;
154   }
155
156   bool getChangedDataFile ()
157   {
158     return m_changed_datafile;
159   }
160
161   char *getDatafileInitFailed ()
162   {
163     return m_initdatafilefailed;
164   }
165
166   char *getDatafileSaveFailed ()
167   {
168     return m_savedatafilefailed;
169   }
170
171   int getInitErrorCode ()
172   {
173     return m_initerrorcode;
174   }
175
176   int getSaveErrorCode ()
177   {
178     return m_saveerrorcode;
179   }
180
181   void freeDataFile ()
182   {
183     if (mc_savefile) {
184       fclose (mc_savefile);
185       mc_savefile = NULL;
186       m_saveInDatafile = false;
187     }
188   }
189
190 private:
191
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);
204
205   void setData (IplImage * img, int lin, int col, uchar valor)
206   {
207     ((uchar *) (img->imageData + img->widthStep * lin))[col] = valor;
208   }
209
210   uchar getData (IplImage * img, int lin, int col)
211   {
212     return ((uchar *) (img->imageData + img->widthStep * lin))[col];
213   }
214
215   bool getIsNonZero (IplImage * img)
216   {
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)
220           return true;
221       }
222     return false;
223   }
224
225   void setMotionCells (int p_frameWidth, int p_frameHeight)
226   {
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];
232
233     //init cells
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;
240       }
241   }
242
243   IplImage *m_pcurFrame, *m_pprevFrame, *m_pdifferenceImage,
244       *m_pbwImage,*transparencyimg;
245   CvSize m_frameSize;
246   bool m_isVisible, m_changed_datafile, m_useAlpha, m_saveInDatafile;
247   Cell **m_pCells;
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;
256   FILE *mc_savefile;
257   MotionCellHeader m_header;
258
259 };
260
261 #endif /* MOTIONCELLS_H_ */