1 /*
2  * GStreamer
3  * Copyright (C) 2013 Miguel Casas-Sanchez <miguelecasassanchez@gmail.com>
4  *
5  * Permission is hereby granted, free of charge, to any person obtaining a
6  * copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation
8  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
9  * and/or sell copies of the Software, and to permit persons to whom the
10  * Software is furnished to do so, subject to the following conditions:
11  *
12  * The above copyright notice and this permission notice shall be included in
13  * all copies or substantial portions of the Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  *
23  * Alternatively, the contents of this file may be used under the
24  * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
25  * which case the following provisions apply instead of the ones
26  * mentioned above:
27  *
28  * This library is free software; you can redistribute it and/or
29  * modify it under the terms of the GNU Library General Public
30  * License as published by the Free Software Foundation; either
31  * version 2 of the License, or (at your option) any later version.
32  *
33  * This library is distributed in the hope that it will be useful,
34  * but WITHOUT ANY WARRANTY; without even the implied warranty of
35  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
36  * Library General Public License for more details.
37  *
38  * You should have received a copy of the GNU Library General Public
39  * License along with this library; if not, write to the
40  * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
41  * Boston, MA 02110-1301, USA.
42  */
43 
44 #ifndef __GST_SEGMENTATION_H__
45 #define __GST_SEGMENTATION_H__
46 
47 #include <gst/gst.h>
48 #include <gst/opencv/gstopencvvideofilter.h>
49 
50 #include <opencv2/video.hpp>
51 #include <opencv2/core.hpp>
52 #include <opencv2/bgsegm.hpp>
53 
54 G_BEGIN_DECLS
55 /* #defines don't like whitespacey bits */
56 #define GST_TYPE_SEGMENTATION \
57   (gst_segmentation_get_type())
58 #define GST_SEGMENTATION(obj) \
59   (G_TYPE_CHECK_INSTANCE_CAST((obj),GST_TYPE_SEGMENTATION,GstSegmentation))
60 #define GST_SEGMENTATION_CLASS(klass) \
61   (G_TYPE_CHECK_CLASS_CAST((klass),GST_TYPE_SEGMENTATION,GstSegmentationClass))
62 #define GST_IS_SEGMENTATION(obj) \
63   (G_TYPE_CHECK_INSTANCE_TYPE((obj),GST_TYPE_SEGMENTATION))
64 #define GST_IS_SEGMENTATION_CLASS(klass) \
65   (G_TYPE_CHECK_CLASS_TYPE((klass),GST_TYPE_SEGMENTATION))
66 typedef struct _GstSegmentation GstSegmentation;
67 typedef struct _GstSegmentationClass GstSegmentationClass;
68 
69 #define CHANNELS 3
70 typedef struct ce
71 {
72   unsigned char learnHigh[CHANNELS];    /* High side threshold for learning */
73   unsigned char learnLow[CHANNELS];     /* Low side threshold for learning */
74   unsigned char max[CHANNELS];  /* High side of box boundary */
75   unsigned char min[CHANNELS];  /* Low side of box boundary */
76   int t_last_update;            /* Allow us to kill stale entries */
77   int stale;                    /* max negative run (longest period of inactivity) */
78 } code_element;
79 
80 
81 typedef struct code_book
82 {
83   code_element **cb;
84   int numEntries;
85   int t;                        /*count every access */
86 } codeBook;
87 
88 struct _GstSegmentation
89 {
90   GstOpencvVideoFilter element;
91   gint method;
92 
93   gboolean test_mode;
94   gint width, height;
95 
96   cv::Mat cvRGB;
97   cv::Mat cvYUV;
98 
99   cv::Mat cvFG;               /*  used for the alpha BW 1ch image composition */
100   cv::Mat ch1, ch2, ch3;
101   int framecount;
102 
103   /* for codebook approach */
104   codeBook *TcodeBook;
105   int learning_interval;
106 
107   /* for MOG methods */
108   cv::Ptr<cv::BackgroundSubtractor> mog;                   /* cv::BackgroundSubtractorMOG */
109   cv::Ptr<cv::BackgroundSubtractorMOG2> mog2;                   /* cv::BackgroundSubtractorMOG2 */
110 
111   double learning_rate;
112 };
113 
114 struct _GstSegmentationClass
115 {
116   GstOpencvVideoFilterClass parent_class;
117 };
118 
119 GType gst_segmentation_get_type (void);
120 
121 gboolean gst_segmentation_plugin_init (GstPlugin * plugin);
122 
123 G_END_DECLS
124 #endif /* __GST_SEGMENTATION_H__ */
125