3 * Copyright (C) 2005 Thomas Vander Stichele <thomas@apestaart.org>
4 * Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
5 * Copyright (C) 2008 Michael Sheldon <mike@mikeasoft.com>
7 * Permission is hereby granted, free of charge, to any person obtaining a
8 * copy of this software and associated documentation files (the "Software"),
9 * to deal in the Software without restriction, including without limitation
10 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
11 * and/or sell copies of the Software, and to permit persons to whom the
12 * Software is furnished to do so, subject to the following conditions:
14 * The above copyright notice and this permission notice shall be included in
15 * all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23 * DEALINGS IN THE SOFTWARE.
25 * Alternatively, the contents of this file may be used under the
26 * GNU Lesser General Public License Version 2.1 (the "LGPL"), in
27 * which case the following provisions apply instead of the ones
30 * This library is free software; you can redistribute it and/or
31 * modify it under the terms of the GNU Library General Public
32 * License as published by the Free Software Foundation; either
33 * version 2 of the License, or (at your option) any later version.
35 * This library is distributed in the hope that it will be useful,
36 * but WITHOUT ANY WARRANTY; without even the implied warranty of
37 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
38 * Library General Public License for more details.
40 * You should have received a copy of the GNU Library General Public
41 * License along with this library; if not, write to the
42 * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor,
43 * Boston, MA 02110-1301, USA.
47 * SECTION:element-edgedetect
49 * Performs canny edge detection on videos and images
51 * ## Example launch line
54 * gst-launch-1.0 videotestsrc ! videoconvert ! edgedetect ! videoconvert ! xvimagesink
62 #include "gstedgedetect.h"
63 #include <opencv2/imgproc.hpp>
65 GST_DEBUG_CATEGORY_STATIC (gst_edge_detect_debug);
66 #define GST_CAT_DEFAULT gst_edge_detect_debug
68 /* Filter signals and args */
84 /* the capabilities of the inputs and outputs.
86 * describe the real formats here.
88 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
91 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
94 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
97 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
100 G_DEFINE_TYPE (GstEdgeDetect, gst_edge_detect, GST_TYPE_OPENCV_VIDEO_FILTER);
102 static void gst_edge_detect_set_property (GObject * object, guint prop_id,
103 const GValue * value, GParamSpec * pspec);
104 static void gst_edge_detect_get_property (GObject * object, guint prop_id,
105 GValue * value, GParamSpec * pspec);
107 static GstFlowReturn gst_edge_detect_transform (GstOpencvVideoFilter * filter,
108 GstBuffer * buf, cv::Mat img, GstBuffer * outbuf, cv::Mat outimg);
109 static gboolean gst_edge_detect_set_caps (GstOpencvVideoFilter * transform,
110 gint in_width, gint in_height, int in_cv_type,
111 gint out_width, gint out_height, int out_cv_type);
115 gst_edge_detect_finalize (GObject * obj)
117 GstEdgeDetect *filter = GST_EDGE_DETECT (obj);
119 filter->cvGray.release ();
120 filter->cvEdge.release ();
122 G_OBJECT_CLASS (gst_edge_detect_parent_class)->finalize (obj);
125 /* initialize the edgedetect's class */
127 gst_edge_detect_class_init (GstEdgeDetectClass * klass)
129 GObjectClass *gobject_class;
130 GstOpencvVideoFilterClass *gstopencvbasefilter_class;
132 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
133 gobject_class = (GObjectClass *) klass;
134 gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
136 gobject_class->finalize = GST_DEBUG_FUNCPTR (gst_edge_detect_finalize);
137 gobject_class->set_property = gst_edge_detect_set_property;
138 gobject_class->get_property = gst_edge_detect_get_property;
140 gstopencvbasefilter_class->cv_trans_func = gst_edge_detect_transform;
141 gstopencvbasefilter_class->cv_set_caps = gst_edge_detect_set_caps;
143 g_object_class_install_property (gobject_class, PROP_MASK,
144 g_param_spec_boolean ("mask", "Mask",
145 "Sets whether the detected edges should be used as a mask on the original input or not",
146 TRUE, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
147 g_object_class_install_property (gobject_class, PROP_THRESHOLD1,
148 g_param_spec_int ("threshold1", "Threshold1",
149 "Threshold value for canny edge detection", 0, 1000, 50,
150 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
151 g_object_class_install_property (gobject_class, PROP_THRESHOLD2,
152 g_param_spec_int ("threshold2", "Threshold2",
153 "Second threshold value for canny edge detection", 0, 1000, 150,
154 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
155 g_object_class_install_property (gobject_class, PROP_APERTURE,
156 g_param_spec_int ("aperture", "Aperture",
157 "Aperture size for Sobel operator (Must be either 3, 5 or 7", 3, 7, 3,
158 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
160 gst_element_class_set_static_metadata (element_class,
162 "Filter/Effect/Video",
163 "Performs canny edge detection on videos and images.",
164 "Michael Sheldon <mike@mikeasoft.com>");
166 gst_element_class_add_static_pad_template (element_class, &src_factory);
167 gst_element_class_add_static_pad_template (element_class, &sink_factory);
170 /* initialize the new element
171 * instantiate pads and add them to element
172 * set pad calback functions
173 * initialize instance structure
176 gst_edge_detect_init (GstEdgeDetect * filter)
179 filter->threshold1 = 50;
180 filter->threshold2 = 150;
181 filter->aperture = 3;
183 gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
188 gst_edge_detect_set_property (GObject * object, guint prop_id,
189 const GValue * value, GParamSpec * pspec)
191 GstEdgeDetect *filter = GST_EDGE_DETECT (object);
195 filter->mask = g_value_get_boolean (value);
197 case PROP_THRESHOLD1:
198 filter->threshold1 = g_value_get_int (value);
200 case PROP_THRESHOLD2:
201 filter->threshold2 = g_value_get_int (value);
204 filter->aperture = g_value_get_int (value);
207 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213 gst_edge_detect_get_property (GObject * object, guint prop_id,
214 GValue * value, GParamSpec * pspec)
216 GstEdgeDetect *filter = GST_EDGE_DETECT (object);
220 g_value_set_boolean (value, filter->mask);
222 case PROP_THRESHOLD1:
223 g_value_set_int (value, filter->threshold1);
225 case PROP_THRESHOLD2:
226 g_value_set_int (value, filter->threshold2);
229 g_value_set_int (value, filter->aperture);
232 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
237 /* GstElement vmethod implementations */
239 /* this function handles the link with other elements */
241 gst_edge_detect_set_caps (GstOpencvVideoFilter * transform,
242 gint in_width, gint in_height, int in_cv_type,
243 gint out_width, gint out_height, int out_cv_type)
245 GstEdgeDetect *filter = GST_EDGE_DETECT (transform);
247 filter->cvGray.create (cv::Size (in_width, in_height), CV_8UC1);
248 filter->cvEdge.create (cv::Size (in_width, in_height), CV_8UC1);
254 gst_edge_detect_transform (GstOpencvVideoFilter * base, GstBuffer * buf,
255 cv::Mat img, GstBuffer * outbuf, cv::Mat outimg)
257 GstEdgeDetect *filter = GST_EDGE_DETECT (base);
259 cv::cvtColor (img, filter->cvGray, cv::COLOR_RGB2GRAY);
260 cv::Canny (filter->cvGray, filter->cvEdge, filter->threshold1,
261 filter->threshold2, filter->aperture);
263 outimg.setTo (cv::Scalar::all (0));
265 img.copyTo (outimg, filter->cvEdge);
267 cv::cvtColor (filter->cvEdge, outimg, cv::COLOR_GRAY2RGB);
273 /* entry point to initialize the plug-in
274 * initialize the plug-in itself
275 * register the element factories and other features
278 gst_edge_detect_plugin_init (GstPlugin * plugin)
280 /* debug category for fltering log messages */
281 GST_DEBUG_CATEGORY_INIT (gst_edge_detect_debug, "edgedetect",
282 0, "Performs canny edge detection on videos and images");
284 return gst_element_register (plugin, "edgedetect", GST_RANK_NONE,
285 GST_TYPE_EDGE_DETECT);