3 * Copyright (C) 2010 Thiago Santos <thiago.sousa.santos@collabora.co.uk>
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:
12 * The above copyright notice and this permission notice shall be included in
13 * all copies or substantial portions of the Software.
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.
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
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.
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.
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.
45 * SECTION:element-cvlaplace
47 * Applies cvLaplace OpenCV function to the image.
52 * gst-launch-1.0 videotestsrc ! cvlaplace ! videoconvert ! autovideosink
60 #include "gstcvlaplace.h"
61 #include <opencv2/imgproc.hpp>
64 GST_DEBUG_CATEGORY_STATIC (gst_cv_laplace_debug);
65 #define GST_CAT_DEFAULT gst_cv_laplace_debug
67 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
70 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
73 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
76 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
79 /* Filter signals and args */
94 #define DEFAULT_APERTURE_SIZE 3
95 #define DEFAULT_SCALE_FACTOR 1.0
96 #define DEFAULT_SHIFT 0.0
97 #define DEFAULT_MASK TRUE
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");
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);
110 static GstFlowReturn gst_cv_laplace_transform (GstOpencvVideoFilter * filter,
111 GstBuffer * buf, cv::Mat img, GstBuffer * outbuf, cv::Mat outimg);
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);
119 gst_cv_laplace_finalize (GObject * obj)
121 GstCvLaplace *filter = GST_CV_LAPLACE (obj);
123 filter->intermediary_img.release ();
124 filter->cvGray.release ();
125 filter->Laplace.release ();
127 G_OBJECT_CLASS (gst_cv_laplace_parent_class)->finalize (obj);
130 /* initialize the cvlaplace's class */
132 gst_cv_laplace_class_init (GstCvLaplaceClass * klass)
134 GObjectClass *gobject_class;
135 GstOpencvVideoFilterClass *gstopencvbasefilter_class;
136 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
138 gobject_class = (GObjectClass *) klass;
139 gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
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;
145 gstopencvbasefilter_class->cv_trans_func = gst_cv_laplace_transform;
146 gstopencvbasefilter_class->cv_set_caps = gst_cv_laplace_cv_set_caps;
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,
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",
166 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
168 gst_element_class_add_static_pad_template (element_class, &src_factory);
169 gst_element_class_add_static_pad_template (element_class, &sink_factory);
171 gst_element_class_set_static_metadata (element_class,
173 "Transform/Effect/Video",
174 "Applies cvLaplace OpenCV function to the image",
175 "Thiago Santos<thiago.sousa.santos@collabora.co.uk>");
179 gst_cv_laplace_init (GstCvLaplace * filter)
181 filter->aperture_size = DEFAULT_APERTURE_SIZE;
182 filter->scale = DEFAULT_SCALE_FACTOR;
183 filter->shift = DEFAULT_SHIFT;
184 filter->mask = DEFAULT_MASK;
186 gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
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)
195 GstCvLaplace *filter = GST_CV_LAPLACE (trans);
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);
205 gst_cv_laplace_set_property (GObject * object, guint prop_id,
206 const GValue * value, GParamSpec * pspec)
208 GstCvLaplace *filter = GST_CV_LAPLACE (object);
211 case PROP_APERTURE_SIZE:{
212 gint as = g_value_get_int (value);
215 GST_WARNING_OBJECT (filter, "Invalid value %d for aperture size", as);
217 filter->aperture_size = g_value_get_int (value);
221 filter->scale = g_value_get_double (value);
224 filter->shift = g_value_get_double (value);
227 filter->mask = g_value_get_boolean (value);
230 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
236 gst_cv_laplace_get_property (GObject * object, guint prop_id,
237 GValue * value, GParamSpec * pspec)
239 GstCvLaplace *filter = GST_CV_LAPLACE (object);
242 case PROP_APERTURE_SIZE:
243 g_value_set_int (value, filter->aperture_size);
246 g_value_set_double (value, filter->scale);
249 g_value_set_double (value, filter->shift);
252 g_value_set_boolean (value, filter->mask);
255 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
261 gst_cv_laplace_transform (GstOpencvVideoFilter * base, GstBuffer * buf,
262 cv::Mat img, GstBuffer * outbuf, cv::Mat outimg)
264 GstCvLaplace *filter = GST_CV_LAPLACE (base);
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);
272 outimg.setTo (cv::Scalar::all (0));
274 img.copyTo (outimg, filter->Laplace);
276 cv::cvtColor (filter->Laplace, outimg, cv::COLOR_GRAY2RGB);