• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * GStreamer
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>
6  *
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:
13  *
14  * The above copyright notice and this permission notice shall be included in
15  * all copies or substantial portions of the Software.
16  *
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.
24  *
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
28  * mentioned above:
29  *
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.
34  *
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.
39  *
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.
44  */
45 
46 /**
47  * SECTION:element-edgedetect
48  *
49  * Performs canny edge detection on videos and images
50  *
51  * ## Example launch line
52  *
53  * |[
54  * gst-launch-1.0 videotestsrc ! videoconvert ! edgedetect ! videoconvert ! xvimagesink
55  * ]|
56  */
57 
58 #ifdef HAVE_CONFIG_H
59 #  include <config.h>
60 #endif
61 
62 #include "gstedgedetect.h"
63 #include <opencv2/imgproc.hpp>
64 
65 GST_DEBUG_CATEGORY_STATIC (gst_edge_detect_debug);
66 #define GST_CAT_DEFAULT gst_edge_detect_debug
67 
68 /* Filter signals and args */
69 enum
70 {
71   /* FILL ME */
72   LAST_SIGNAL
73 };
74 
75 enum
76 {
77   PROP_0,
78   PROP_THRESHOLD1,
79   PROP_THRESHOLD2,
80   PROP_APERTURE,
81   PROP_MASK
82 };
83 
84 /* the capabilities of the inputs and outputs.
85  *
86  * describe the real formats here.
87  */
88 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
89     GST_PAD_SINK,
90     GST_PAD_ALWAYS,
91     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
92     );
93 
94 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
95     GST_PAD_SRC,
96     GST_PAD_ALWAYS,
97     GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
98     );
99 
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);
106 
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);
111 
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);
117 
118 /* Clean up */
119 static void
gst_edge_detect_finalize(GObject * obj)120 gst_edge_detect_finalize (GObject * obj)
121 {
122   GstEdgeDetect *filter = GST_EDGE_DETECT (obj);
123 
124   filter->cvGray.release ();
125   filter->cvEdge.release ();
126 
127   G_OBJECT_CLASS (gst_edge_detect_parent_class)->finalize (obj);
128 }
129 
130 /* initialize the edgedetect's class */
131 static void
gst_edge_detect_class_init(GstEdgeDetectClass * klass)132 gst_edge_detect_class_init (GstEdgeDetectClass * klass)
133 {
134   GObjectClass *gobject_class;
135   GstOpencvVideoFilterClass *gstopencvbasefilter_class;
136 
137   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
138   gobject_class = (GObjectClass *) klass;
139   gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
140 
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;
144 
145   gstopencvbasefilter_class->cv_trans_func = gst_edge_detect_transform;
146   gstopencvbasefilter_class->cv_set_caps = gst_edge_detect_set_caps;
147 
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)));
164 
165   gst_element_class_set_static_metadata (element_class,
166       "edgedetect",
167       "Filter/Effect/Video",
168       "Performs canny edge detection on videos and images.",
169       "Michael Sheldon <mike@mikeasoft.com>");
170 
171   gst_element_class_add_static_pad_template (element_class, &src_factory);
172   gst_element_class_add_static_pad_template (element_class, &sink_factory);
173 }
174 
175 /* initialize the new element
176  * instantiate pads and add them to element
177  * set pad callback functions
178  * initialize instance structure
179  */
180 static void
gst_edge_detect_init(GstEdgeDetect * filter)181 gst_edge_detect_init (GstEdgeDetect * filter)
182 {
183   filter->mask = TRUE;
184   filter->threshold1 = 50;
185   filter->threshold2 = 150;
186   filter->aperture = 3;
187 
188   gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
189       FALSE);
190 }
191 
192 static void
gst_edge_detect_set_property(GObject * object,guint prop_id,const GValue * value,GParamSpec * pspec)193 gst_edge_detect_set_property (GObject * object, guint prop_id,
194     const GValue * value, GParamSpec * pspec)
195 {
196   GstEdgeDetect *filter = GST_EDGE_DETECT (object);
197 
198   switch (prop_id) {
199     case PROP_MASK:
200       filter->mask = g_value_get_boolean (value);
201       break;
202     case PROP_THRESHOLD1:
203       filter->threshold1 = g_value_get_int (value);
204       break;
205     case PROP_THRESHOLD2:
206       filter->threshold2 = g_value_get_int (value);
207       break;
208     case PROP_APERTURE:
209       filter->aperture = g_value_get_int (value);
210       break;
211     default:
212       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
213       break;
214   }
215 }
216 
217 static void
gst_edge_detect_get_property(GObject * object,guint prop_id,GValue * value,GParamSpec * pspec)218 gst_edge_detect_get_property (GObject * object, guint prop_id,
219     GValue * value, GParamSpec * pspec)
220 {
221   GstEdgeDetect *filter = GST_EDGE_DETECT (object);
222 
223   switch (prop_id) {
224     case PROP_MASK:
225       g_value_set_boolean (value, filter->mask);
226       break;
227     case PROP_THRESHOLD1:
228       g_value_set_int (value, filter->threshold1);
229       break;
230     case PROP_THRESHOLD2:
231       g_value_set_int (value, filter->threshold2);
232       break;
233     case PROP_APERTURE:
234       g_value_set_int (value, filter->aperture);
235       break;
236     default:
237       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
238       break;
239   }
240 }
241 
242 /* GstElement vmethod implementations */
243 
244 /* this function handles the link with other elements */
245 static gboolean
gst_edge_detect_set_caps(GstOpencvVideoFilter * transform,gint in_width,gint in_height,int in_cv_type,gint out_width,gint out_height,int out_cv_type)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)
249 {
250   GstEdgeDetect *filter = GST_EDGE_DETECT (transform);
251 
252   filter->cvGray.create (cv::Size (in_width, in_height), CV_8UC1);
253   filter->cvEdge.create (cv::Size (in_width, in_height), CV_8UC1);
254 
255   return TRUE;
256 }
257 
258 static GstFlowReturn
gst_edge_detect_transform(GstOpencvVideoFilter * base,GstBuffer * buf,cv::Mat img,GstBuffer * outbuf,cv::Mat outimg)259 gst_edge_detect_transform (GstOpencvVideoFilter * base, GstBuffer * buf,
260     cv::Mat img, GstBuffer * outbuf, cv::Mat outimg)
261 {
262   GstEdgeDetect *filter = GST_EDGE_DETECT (base);
263 
264   cv::cvtColor (img, filter->cvGray, cv::COLOR_RGB2GRAY);
265   cv::Canny (filter->cvGray, filter->cvEdge, filter->threshold1,
266       filter->threshold2, filter->aperture);
267 
268   outimg.setTo (cv::Scalar::all (0));
269   if (filter->mask) {
270     img.copyTo (outimg, filter->cvEdge);
271   } else {
272     cv::cvtColor (filter->cvEdge, outimg, cv::COLOR_GRAY2RGB);
273   }
274 
275   return GST_FLOW_OK;
276 }
277