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_WITH_CODE (GstEdgeDetect, gst_edge_detect,
101 GST_TYPE_OPENCV_VIDEO_FILTER,
102 GST_DEBUG_CATEGORY_INIT (gst_edge_detect_debug, "edgedetect", 0,
103 "Performs canny edge detection on videos and images"););
104 GST_ELEMENT_REGISTER_DEFINE (edgedetect, "edgedetect", GST_RANK_NONE,
105 GST_TYPE_EDGE_DETECT);
107 static void gst_edge_detect_set_property (GObject * object, guint prop_id,
108 const GValue * value, GParamSpec * pspec);
109 static void gst_edge_detect_get_property (GObject * object, guint prop_id,
110 GValue * value, GParamSpec * pspec);
112 static GstFlowReturn gst_edge_detect_transform (GstOpencvVideoFilter * filter,
113 GstBuffer * buf, cv::Mat img, GstBuffer * outbuf, cv::Mat outimg);
114 static gboolean gst_edge_detect_set_caps (GstOpencvVideoFilter * transform,
115 gint in_width, gint in_height, int in_cv_type,
116 gint out_width, gint out_height, int out_cv_type);
120 gst_edge_detect_finalize (GObject * obj)
122 GstEdgeDetect *filter = GST_EDGE_DETECT (obj);
124 filter->cvGray.release ();
125 filter->cvEdge.release ();
127 G_OBJECT_CLASS (gst_edge_detect_parent_class)->finalize (obj);
130 /* initialize the edgedetect's class */
132 gst_edge_detect_class_init (GstEdgeDetectClass * klass)
134 GObjectClass *gobject_class;
135 GstOpencvVideoFilterClass *gstopencvbasefilter_class;
137 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_edge_detect_finalize);
142 gobject_class->set_property = gst_edge_detect_set_property;
143 gobject_class->get_property = gst_edge_detect_get_property;
145 gstopencvbasefilter_class->cv_trans_func = gst_edge_detect_transform;
146 gstopencvbasefilter_class->cv_set_caps = gst_edge_detect_set_caps;
148 g_object_class_install_property (gobject_class, PROP_MASK,
149 g_param_spec_boolean ("mask", "Mask",
150 "Sets whether the detected edges should be used as a mask on the original input or not",
151 TRUE, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
152 g_object_class_install_property (gobject_class, PROP_THRESHOLD1,
153 g_param_spec_int ("threshold1", "Threshold1",
154 "Threshold value for canny edge detection", 0, 1000, 50,
155 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
156 g_object_class_install_property (gobject_class, PROP_THRESHOLD2,
157 g_param_spec_int ("threshold2", "Threshold2",
158 "Second threshold value for canny edge detection", 0, 1000, 150,
159 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
160 g_object_class_install_property (gobject_class, PROP_APERTURE,
161 g_param_spec_int ("aperture", "Aperture",
162 "Aperture size for Sobel operator (Must be either 3, 5 or 7", 3, 7, 3,
163 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
165 gst_element_class_set_static_metadata (element_class,
167 "Filter/Effect/Video",
168 "Performs canny edge detection on videos and images.",
169 "Michael Sheldon <mike@mikeasoft.com>");
171 gst_element_class_add_static_pad_template (element_class, &src_factory);
172 gst_element_class_add_static_pad_template (element_class, &sink_factory);
175 /* initialize the new element
176 * instantiate pads and add them to element
177 * set pad callback functions
178 * initialize instance structure
181 gst_edge_detect_init (GstEdgeDetect * filter)
184 filter->threshold1 = 50;
185 filter->threshold2 = 150;
186 filter->aperture = 3;
188 gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
193 gst_edge_detect_set_property (GObject * object, guint prop_id,
194 const GValue * value, GParamSpec * pspec)
196 GstEdgeDetect *filter = GST_EDGE_DETECT (object);
200 filter->mask = g_value_get_boolean (value);
202 case PROP_THRESHOLD1:
203 filter->threshold1 = g_value_get_int (value);
205 case PROP_THRESHOLD2:
206 filter->threshold2 = g_value_get_int (value);
209 filter->aperture = g_value_get_int (value);
212 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
218 gst_edge_detect_get_property (GObject * object, guint prop_id,
219 GValue * value, GParamSpec * pspec)
221 GstEdgeDetect *filter = GST_EDGE_DETECT (object);
225 g_value_set_boolean (value, filter->mask);
227 case PROP_THRESHOLD1:
228 g_value_set_int (value, filter->threshold1);
230 case PROP_THRESHOLD2:
231 g_value_set_int (value, filter->threshold2);
234 g_value_set_int (value, filter->aperture);
237 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
242 /* GstElement vmethod implementations */
244 /* this function handles the link with other elements */
246 gst_edge_detect_set_caps (GstOpencvVideoFilter * transform,
247 gint in_width, gint in_height, int in_cv_type,
248 gint out_width, gint out_height, int out_cv_type)
250 GstEdgeDetect *filter = GST_EDGE_DETECT (transform);
252 filter->cvGray.create (cv::Size (in_width, in_height), CV_8UC1);
253 filter->cvEdge.create (cv::Size (in_width, in_height), CV_8UC1);
259 gst_edge_detect_transform (GstOpencvVideoFilter * base, GstBuffer * buf,
260 cv::Mat img, GstBuffer * outbuf, cv::Mat outimg)
262 GstEdgeDetect *filter = GST_EDGE_DETECT (base);
264 cv::cvtColor (img, filter->cvGray, cv::COLOR_RGB2GRAY);
265 cv::Canny (filter->cvGray, filter->cvEdge, filter->threshold1,
266 filter->threshold2, filter->aperture);
268 outimg.setTo (cv::Scalar::all (0));
270 img.copyTo (outimg, filter->cvEdge);
272 cv::cvtColor (filter->cvEdge, outimg, cv::COLOR_GRAY2RGB);