3 * Copyright (C) 2005 Thomas Vander Stichele <thomas@apestaart.org>
4 * Copyright (C) 2005 Ronald S. Bultje <rbultje@ronald.bitfreak.net>
5 * Copyright (C) 2010 Sreerenj Balachandran <bsreerenj@gmail.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-opencvtextoverlay
49 * opencvtextoverlay renders the text on top of the video frames
51 * ## Example launch line
54 * gst-launch-1.0 videotestsrc ! videoconvert ! opencvtextoverlay text="Opencv Text Overlay " ! videoconvert ! xvimagesink
62 #include "gsttextoverlay.h"
64 GST_DEBUG_CATEGORY_STATIC (gst_opencv_text_overlay_debug);
65 #define GST_CAT_DEFAULT gst_opencv_opencv_text_overlay_debug
67 #define DEFAULT_PROP_TEXT "Opencv Text Overlay"
68 #define DEFAULT_PROP_WIDTH 1
69 #define DEFAULT_PROP_HEIGHT 1
70 #define DEFAULT_PROP_XPOS 50
71 #define DEFAULT_PROP_YPOS 50
72 #define DEFAULT_PROP_THICKNESS 2
73 #define DEFAULT_PROP_COLOR 0
76 /* Filter signals and args */
82 #define DEFAULT_WIDTH 1.0
83 #define DEFAULT_HEIGHT 1.0
98 /* the capabilities of the inputs and outputs.
100 * describe the real formats here.
102 static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink",
105 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
108 static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
111 GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
114 G_DEFINE_TYPE (GstOpencvTextOverlay, gst_opencv_text_overlay,
115 GST_TYPE_OPENCV_VIDEO_FILTER);
117 static void gst_opencv_text_overlay_set_property (GObject * object,
118 guint prop_id, const GValue * value, GParamSpec * pspec);
119 static void gst_opencv_text_overlay_get_property (GObject * object,
120 guint prop_id, GValue * value, GParamSpec * pspec);
122 static GstFlowReturn gst_opencv_text_overlay_transform_ip (GstOpencvVideoFilter
123 * filter, GstBuffer * buf, cv::Mat img);
127 gst_opencv_text_overlay_finalize (GObject * obj)
129 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (obj);
131 g_free (filter->textbuf);
133 G_OBJECT_CLASS (gst_opencv_text_overlay_parent_class)->finalize (obj);
136 /* initialize the opencvtextoverlay's class */
138 gst_opencv_text_overlay_class_init (GstOpencvTextOverlayClass * klass)
140 GObjectClass *gobject_class;
141 GstOpencvVideoFilterClass *gstopencvbasefilter_class;
142 GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
144 gobject_class = (GObjectClass *) klass;
145 gobject_class->finalize =
146 GST_DEBUG_FUNCPTR (gst_opencv_text_overlay_finalize);
147 gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
149 gstopencvbasefilter_class->cv_trans_ip_func =
150 gst_opencv_text_overlay_transform_ip;
152 gobject_class->set_property = gst_opencv_text_overlay_set_property;
153 gobject_class->get_property = gst_opencv_text_overlay_get_property;
155 g_object_class_install_property (gobject_class, PROP_TEXT,
156 g_param_spec_string ("text", "text",
157 "Text to be display.", DEFAULT_PROP_TEXT,
158 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
160 g_object_class_install_property (gobject_class, PROP_XPOS,
161 g_param_spec_int ("xpos", "horizontal position",
162 "Sets the Horizontal position", 0, G_MAXINT,
164 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
166 g_object_class_install_property (gobject_class, PROP_YPOS,
167 g_param_spec_int ("ypos", "vertical position",
168 "Sets the Vertical position", 0, G_MAXINT,
170 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
172 g_object_class_install_property (gobject_class, PROP_THICKNESS,
173 g_param_spec_int ("thickness", "font thickness",
174 "Sets the Thickness of Font", 0, G_MAXINT,
175 DEFAULT_PROP_THICKNESS,
176 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
178 g_object_class_install_property (gobject_class, PROP_COLOR_R,
179 g_param_spec_int ("colorR", "color -Red ",
180 "Sets the color -R", 0, 255,
182 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
184 g_object_class_install_property (gobject_class, PROP_COLOR_G,
185 g_param_spec_int ("colorG", "color -Green",
186 "Sets the color -G", 0, 255,
188 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
190 g_object_class_install_property (gobject_class, PROP_COLOR_B,
191 g_param_spec_int ("colorB", "color -Blue",
192 "Sets the color -B", 0, 255,
194 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
196 g_object_class_install_property (gobject_class, PROP_HEIGHT,
197 g_param_spec_double ("height", "Height",
198 "Sets the height of fonts", 1.0, 5.0,
200 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
202 g_object_class_install_property (gobject_class, PROP_WIDTH,
203 g_param_spec_double ("width", "Width",
204 "Sets the width of fonts", 1.0, 5.0,
206 (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)));
208 gst_element_class_set_static_metadata (element_class,
210 "Filter/Effect/Video",
211 "Write text on the top of video", "sreerenj<bsreerenj@gmail.com>");
213 gst_element_class_add_static_pad_template (element_class, &src_factory);
214 gst_element_class_add_static_pad_template (element_class, &sink_factory);
218 /* initialize the new element
219 * instantiate pads and add them to element
220 * set pad calback functions
221 * initialize instance structure
224 gst_opencv_text_overlay_init (GstOpencvTextOverlay * filter)
226 filter->textbuf = g_strdup (DEFAULT_PROP_TEXT);
227 filter->width = DEFAULT_PROP_WIDTH;
228 filter->height = DEFAULT_PROP_HEIGHT;
229 filter->xpos = DEFAULT_PROP_XPOS;
230 filter->ypos = DEFAULT_PROP_YPOS;
231 filter->thickness = DEFAULT_PROP_THICKNESS;
232 filter->colorR = DEFAULT_PROP_COLOR;
233 filter->colorG = DEFAULT_PROP_COLOR;
234 filter->colorB = DEFAULT_PROP_COLOR;
236 gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
241 gst_opencv_text_overlay_set_property (GObject * object, guint prop_id,
242 const GValue * value, GParamSpec * pspec)
244 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (object);
248 g_free (filter->textbuf);
249 filter->textbuf = g_value_dup_string (value);
252 filter->xpos = g_value_get_int (value);
255 filter->ypos = g_value_get_int (value);
258 filter->thickness = g_value_get_int (value);
262 filter->colorR = g_value_get_int (value);
265 filter->colorG = g_value_get_int (value);
268 filter->colorB = g_value_get_int (value);
272 filter->height = g_value_get_double (value);
275 filter->width = g_value_get_double (value);
278 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
284 gst_opencv_text_overlay_get_property (GObject * object, guint prop_id,
285 GValue * value, GParamSpec * pspec)
287 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (object);
291 g_value_set_string (value, filter->textbuf);
294 g_value_set_int (value, filter->xpos);
297 g_value_set_int (value, filter->ypos);
300 g_value_set_int (value, filter->thickness);
303 g_value_set_int (value, filter->colorR);
306 g_value_set_int (value, filter->colorG);
309 g_value_set_int (value, filter->colorB);
312 g_value_set_double (value, filter->height);
315 g_value_set_double (value, filter->width);
318 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
324 * this function does the actual processing
327 gst_opencv_text_overlay_transform_ip (GstOpencvVideoFilter * base,
328 GstBuffer * buf, cv::Mat img)
330 GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (base);
332 cv::putText (img, filter->textbuf, cv::Point (filter->xpos,
333 filter->ypos), cv::FONT_HERSHEY_SIMPLEX,
334 (filter->width + filter->height) * 0.5, cv::Scalar (filter->colorR,
335 filter->colorG, filter->colorB), filter->thickness);
341 /* entry point to initialize the plug-in
342 * initialize the plug-in itself
343 * register the element factories and other features
346 gst_opencv_text_overlay_plugin_init (GstPlugin * plugin)
348 /* debug category for fltering log messages
350 * exchange the string 'Template opencvtextoverlay' with your description
352 GST_DEBUG_CATEGORY_INIT (gst_opencv_text_overlay_debug, "opencvtextoverlay",
353 0, "Template opencvtextoverlay");
355 return gst_element_register (plugin, "opencvtextoverlay", GST_RANK_NONE,
356 GST_TYPE_OPENCV_TEXT_OVERLAY);