1 /*
2 * GStreamer
3 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
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 /**
45 * SECTION:element-cvlaplace
46 *
47 * Applies cvLaplace OpenCV function to the image.
48 *
49 * Example launch line
50 *
51 * |[
52 * gst-launch-1.0 videotestsrc ! cvlaplace ! videoconvert ! autovideosink
53 * ]|
54 */
55
56 #ifdef HAVE_CONFIG_H
57 # include <config.h>
58 #endif
59
60 #include "gstcvlaplace.h"
61 #include <opencv2/imgproc.hpp>
62
63
64 GST_DEBUG_CATEGORY_STATIC (gst_cv_laplace_debug);
65 #define GST_CAT_DEFAULT gst_cv_laplace_debug
66
67 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
68 GST_PAD_SINK,
69 GST_PAD_ALWAYS,
70 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
71 );
72
73 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
74 GST_PAD_SRC,
75 GST_PAD_ALWAYS,
76 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
77 );
78
79 /* Filter signals and args */
80 enum
81 {
82 /* FILL ME */
83 LAST_SIGNAL
84 };
85 enum
86 {
87 PROP_0,
88 PROP_APERTURE_SIZE,
89 PROP_SCALE,
90 PROP_SHIFT,
91 PROP_MASK
92 };
93
94 #define DEFAULT_APERTURE_SIZE 3
95 #define DEFAULT_SCALE_FACTOR 1.0
96 #define DEFAULT_SHIFT 0.0
97 #define DEFAULT_MASK TRUE
98
99 G_DEFINE_TYPE_WITH_CODE (GstCvLaplace, gst_cv_laplace,
100 GST_TYPE_OPENCV_VIDEO_FILTER, GST_DEBUG_CATEGORY_INIT (gst_cv_laplace_debug,
101 "cvlaplace", 0, "cvlaplace");
102 );
103 GST_ELEMENT_REGISTER_DEFINE (cvlaplace, "cvlaplace", GST_RANK_NONE,
104 GST_TYPE_CV_LAPLACE);
105 static void gst_cv_laplace_set_property (GObject * object, guint prop_id,
106 const GValue * value, GParamSpec * pspec);
107 static void gst_cv_laplace_get_property (GObject * object, guint prop_id,
108 GValue * value, GParamSpec * pspec);
109
110 static GstFlowReturn gst_cv_laplace_transform (GstOpencvVideoFilter * filter,
111 GstBuffer * buf, cv::Mat img, GstBuffer * outbuf, cv::Mat outimg);
112
113 static gboolean gst_cv_laplace_cv_set_caps (GstOpencvVideoFilter * trans,
114 gint in_width, gint in_height, int in_cv_type,
115 gint out_width, gint out_height, int out_cv_type);
116
117 /* Clean up */
118 static void
gst_cv_laplace_finalize(GObject * obj)119 gst_cv_laplace_finalize (GObject * obj)
120 {
121 GstCvLaplace *filter = GST_CV_LAPLACE (obj);
122
123 filter->intermediary_img.release ();
124 filter->cvGray.release ();
125 filter->Laplace.release ();
126
127 G_OBJECT_CLASS (gst_cv_laplace_parent_class)->finalize (obj);
128 }
129
130 /* initialize the cvlaplace's class */
131 static void
gst_cv_laplace_class_init(GstCvLaplaceClass * klass)132 gst_cv_laplace_class_init (GstCvLaplaceClass * klass)
133 {
134 GObjectClass *gobject_class;
135 GstOpencvVideoFilterClass *gstopencvbasefilter_class;
136 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
137
138 gobject_class = (GObjectClass *) klass;
139 gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
140
141 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_cv_laplace_finalize);
142 gobject_class->set_property = gst_cv_laplace_set_property;
143 gobject_class->get_property = gst_cv_laplace_get_property;
144
145 gstopencvbasefilter_class->cv_trans_func = gst_cv_laplace_transform;
146 gstopencvbasefilter_class->cv_set_caps = gst_cv_laplace_cv_set_caps;
147
148 g_object_class_install_property (gobject_class, PROP_APERTURE_SIZE,
149 g_param_spec_int ("aperture-size", "aperture size",
150 "Size of the extended Laplace Kernel (1, 3, 5 or 7)", 1, 7,
151 DEFAULT_APERTURE_SIZE,
152 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
153 g_object_class_install_property (gobject_class, PROP_SCALE,
154 g_param_spec_double ("scale", "scale factor", "Scale factor", 0.0,
155 G_MAXDOUBLE, DEFAULT_SCALE_FACTOR,
156 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
157 g_object_class_install_property (gobject_class, PROP_SHIFT,
158 g_param_spec_double ("shift", "Shift",
159 "Value added to the scaled source array elements", 0.0, G_MAXDOUBLE,
160 DEFAULT_SHIFT,
161 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
162 g_object_class_install_property (gobject_class, PROP_MASK,
163 g_param_spec_boolean ("mask", "Mask",
164 "Sets whether the detected edges should be used as a mask on the original input or not",
165 DEFAULT_MASK,
166 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
167
168 gst_element_class_add_static_pad_template (element_class, &src_factory);
169 gst_element_class_add_static_pad_template (element_class, &sink_factory);
170
171 gst_element_class_set_static_metadata (element_class,
172 "cvlaplace",
173 "Transform/Effect/Video",
174 "Applies cvLaplace OpenCV function to the image",
175 "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
176 }
177
178 static void
gst_cv_laplace_init(GstCvLaplace * filter)179 gst_cv_laplace_init (GstCvLaplace * filter)
180 {
181 filter->aperture_size = DEFAULT_APERTURE_SIZE;
182 filter->scale = DEFAULT_SCALE_FACTOR;
183 filter->shift = DEFAULT_SHIFT;
184 filter->mask = DEFAULT_MASK;
185
186 gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
187 FALSE);
188 }
189
190 static gboolean
gst_cv_laplace_cv_set_caps(GstOpencvVideoFilter * trans,gint in_width,gint in_height,int in_cv_type,gint out_width,gint out_height,int out_cv_type)191 gst_cv_laplace_cv_set_caps (GstOpencvVideoFilter * trans, gint in_width,
192 gint in_height, int in_cv_type, gint out_width,
193 gint out_height, int out_cv_type)
194 {
195 GstCvLaplace *filter = GST_CV_LAPLACE (trans);
196
197 filter->intermediary_img.create (cv::Size (out_width, out_height), CV_16SC1);
198 filter->cvGray.create (cv::Size (in_width, in_height), CV_8UC1);
199 filter->Laplace.create (cv::Size (in_width, in_height), CV_8UC1);
200
201 return TRUE;
202 }
203
204 static void
gst_cv_laplace_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)205 gst_cv_laplace_set_property (GObject * object, guint prop_id,
206 const GValue * value, GParamSpec * pspec)
207 {
208 GstCvLaplace *filter = GST_CV_LAPLACE (object);
209
210 switch (prop_id) {
211 case PROP_APERTURE_SIZE:{
212 gint as = g_value_get_int (value);
213
214 if (as % 2 != 1) {
215 GST_WARNING_OBJECT (filter, "Invalid value %d for aperture size", as);
216 } else
217 filter->aperture_size = g_value_get_int (value);
218 }
219 break;
220 case PROP_SCALE:
221 filter->scale = g_value_get_double (value);
222 break;
223 case PROP_SHIFT:
224 filter->shift = g_value_get_double (value);
225 break;
226 case PROP_MASK:
227 filter->mask = g_value_get_boolean (value);
228 break;
229 default:
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
231 break;
232 }
233 }
234
235 static void
gst_cv_laplace_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)236 gst_cv_laplace_get_property (GObject * object, guint prop_id,
237 GValue * value, GParamSpec * pspec)
238 {
239 GstCvLaplace *filter = GST_CV_LAPLACE (object);
240
241 switch (prop_id) {
242 case PROP_APERTURE_SIZE:
243 g_value_set_int (value, filter->aperture_size);
244 break;
245 case PROP_SCALE:
246 g_value_set_double (value, filter->scale);
247 break;
248 case PROP_SHIFT:
249 g_value_set_double (value, filter->shift);
250 break;
251 case PROP_MASK:
252 g_value_set_boolean (value, filter->mask);
253 break;
254 default:
255 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
256 break;
257 }
258 }
259
260 static GstFlowReturn
gst_cv_laplace_transform(GstOpencvVideoFilter * base,GstBuffer * buf,cv::Mat img,GstBuffer * outbuf,cv::Mat outimg)261 gst_cv_laplace_transform (GstOpencvVideoFilter * base, GstBuffer * buf,
262 cv::Mat img, GstBuffer * outbuf, cv::Mat outimg)
263 {
264 GstCvLaplace *filter = GST_CV_LAPLACE (base);
265
266 cv::cvtColor (img, filter->cvGray, cv::COLOR_RGB2GRAY);
267 cv::Laplacian (filter->cvGray, filter->intermediary_img,
268 filter->intermediary_img.depth (), filter->aperture_size);
269 filter->intermediary_img.convertTo (filter->Laplace, filter->Laplace.type (),
270 filter->scale, filter->shift);
271
272 outimg.setTo (cv::Scalar::all (0));
273 if (filter->mask) {
274 img.copyTo (outimg, filter->Laplace);
275 } else {
276 cv::cvtColor (filter->Laplace, outimg, cv::COLOR_GRAY2RGB);
277 }
278
279 return GST_FLOW_OK;
280 }
281