documentation: fixed a heap o' typos
[platform/upstream/gstreamer.git] / ext / opencv / gstedgedetect.cpp
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 (GstEdgeDetect, gst_edge_detect, GST_TYPE_OPENCV_VIDEO_FILTER);
101
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);
106
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);
112
113 /* Clean up */
114 static void
115 gst_edge_detect_finalize (GObject * obj)
116 {
117   GstEdgeDetect *filter = GST_EDGE_DETECT (obj);
118
119   filter->cvGray.release ();
120   filter->cvEdge.release ();
121
122   G_OBJECT_CLASS (gst_edge_detect_parent_class)->finalize (obj);
123 }
124
125 /* initialize the edgedetect's class */
126 static void
127 gst_edge_detect_class_init (GstEdgeDetectClass * klass)
128 {
129   GObjectClass *gobject_class;
130   GstOpencvVideoFilterClass *gstopencvbasefilter_class;
131
132   GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
133   gobject_class = (GObjectClass *) klass;
134   gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
135
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;
139
140   gstopencvbasefilter_class->cv_trans_func = gst_edge_detect_transform;
141   gstopencvbasefilter_class->cv_set_caps = gst_edge_detect_set_caps;
142
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)));
159
160   gst_element_class_set_static_metadata (element_class,
161       "edgedetect",
162       "Filter/Effect/Video",
163       "Performs canny edge detection on videos and images.",
164       "Michael Sheldon <mike@mikeasoft.com>");
165
166   gst_element_class_add_static_pad_template (element_class, &src_factory);
167   gst_element_class_add_static_pad_template (element_class, &sink_factory);
168 }
169
170 /* initialize the new element
171  * instantiate pads and add them to element
172  * set pad callback functions
173  * initialize instance structure
174  */
175 static void
176 gst_edge_detect_init (GstEdgeDetect * filter)
177 {
178   filter->mask = TRUE;
179   filter->threshold1 = 50;
180   filter->threshold2 = 150;
181   filter->aperture = 3;
182
183   gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
184       FALSE);
185 }
186
187 static void
188 gst_edge_detect_set_property (GObject * object, guint prop_id,
189     const GValue * value, GParamSpec * pspec)
190 {
191   GstEdgeDetect *filter = GST_EDGE_DETECT (object);
192
193   switch (prop_id) {
194     case PROP_MASK:
195       filter->mask = g_value_get_boolean (value);
196       break;
197     case PROP_THRESHOLD1:
198       filter->threshold1 = g_value_get_int (value);
199       break;
200     case PROP_THRESHOLD2:
201       filter->threshold2 = g_value_get_int (value);
202       break;
203     case PROP_APERTURE:
204       filter->aperture = g_value_get_int (value);
205       break;
206     default:
207       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
208       break;
209   }
210 }
211
212 static void
213 gst_edge_detect_get_property (GObject * object, guint prop_id,
214     GValue * value, GParamSpec * pspec)
215 {
216   GstEdgeDetect *filter = GST_EDGE_DETECT (object);
217
218   switch (prop_id) {
219     case PROP_MASK:
220       g_value_set_boolean (value, filter->mask);
221       break;
222     case PROP_THRESHOLD1:
223       g_value_set_int (value, filter->threshold1);
224       break;
225     case PROP_THRESHOLD2:
226       g_value_set_int (value, filter->threshold2);
227       break;
228     case PROP_APERTURE:
229       g_value_set_int (value, filter->aperture);
230       break;
231     default:
232       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
233       break;
234   }
235 }
236
237 /* GstElement vmethod implementations */
238
239 /* this function handles the link with other elements */
240 static gboolean
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)
244 {
245   GstEdgeDetect *filter = GST_EDGE_DETECT (transform);
246
247   filter->cvGray.create (cv::Size (in_width, in_height), CV_8UC1);
248   filter->cvEdge.create (cv::Size (in_width, in_height), CV_8UC1);
249
250   return TRUE;
251 }
252
253 static GstFlowReturn
254 gst_edge_detect_transform (GstOpencvVideoFilter * base, GstBuffer * buf,
255     cv::Mat img, GstBuffer * outbuf, cv::Mat outimg)
256 {
257   GstEdgeDetect *filter = GST_EDGE_DETECT (base);
258
259   cv::cvtColor (img, filter->cvGray, cv::COLOR_RGB2GRAY);
260   cv::Canny (filter->cvGray, filter->cvEdge, filter->threshold1,
261       filter->threshold2, filter->aperture);
262
263   outimg.setTo (cv::Scalar::all (0));
264   if (filter->mask) {
265     img.copyTo (outimg, filter->cvEdge);
266   } else {
267     cv::cvtColor (filter->cvEdge, outimg, cv::COLOR_GRAY2RGB);
268   }
269
270   return GST_FLOW_OK;
271 }
272
273 /* entry point to initialize the plug-in
274  * initialize the plug-in itself
275  * register the element factories and other features
276  */
277 gboolean
278 gst_edge_detect_plugin_init (GstPlugin * plugin)
279 {
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");
283
284   return gst_element_register (plugin, "edgedetect", GST_RANK_NONE,
285       GST_TYPE_EDGE_DETECT);
286 }