mirror of
https://gitlab.freedesktop.org/gstreamer/gstreamer.git
synced 2025-06-05 15:08:53 +00:00
opencv: update opencvtextoverlay to GstOpencvVideoFilter
Update opencvtextoverlay to inherit from GstOpencvVideoFilter instead of from GstElement. This means less code and more uniformity with other OpenCV elements. The chain/transform function is now a third of the size than before.
This commit is contained in:
parent
8b6ea95bee
commit
fa1775acee
2 changed files with 22 additions and 53 deletions
|
@ -112,7 +112,8 @@ static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src",
|
||||||
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
|
GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB"))
|
||||||
);
|
);
|
||||||
|
|
||||||
G_DEFINE_TYPE (GstOpencvTextOverlay, gst_opencv_text_overlay, GST_TYPE_ELEMENT);
|
G_DEFINE_TYPE (GstOpencvTextOverlay, gst_opencv_text_overlay,
|
||||||
|
GST_TYPE_OPENCV_VIDEO_FILTER);
|
||||||
|
|
||||||
static void gst_opencv_text_overlay_set_property (GObject * object,
|
static void gst_opencv_text_overlay_set_property (GObject * object,
|
||||||
guint prop_id, const GValue * value, GParamSpec * pspec);
|
guint prop_id, const GValue * value, GParamSpec * pspec);
|
||||||
|
@ -121,8 +122,8 @@ static void gst_opencv_text_overlay_get_property (GObject * object,
|
||||||
|
|
||||||
static gboolean gst_opencv_text_overlay_handle_sink_event (GstPad * pad,
|
static gboolean gst_opencv_text_overlay_handle_sink_event (GstPad * pad,
|
||||||
GstObject * parent, GstEvent * event);
|
GstObject * parent, GstEvent * event);
|
||||||
static GstFlowReturn gst_opencv_text_overlay_chain (GstPad * pad,
|
static GstFlowReturn gst_opencv_text_overlay_transform_ip (GstOpencvVideoFilter
|
||||||
GstObject * parent, GstBuffer * buf);
|
* filter, GstBuffer * buf, IplImage * img);
|
||||||
|
|
||||||
/* Clean up */
|
/* Clean up */
|
||||||
static void
|
static void
|
||||||
|
@ -130,10 +131,6 @@ gst_opencv_text_overlay_finalize (GObject * obj)
|
||||||
{
|
{
|
||||||
GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (obj);
|
GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (obj);
|
||||||
|
|
||||||
if (filter->cvImage) {
|
|
||||||
cvReleaseImage (&filter->cvImage);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_free (filter->textbuf);
|
g_free (filter->textbuf);
|
||||||
|
|
||||||
G_OBJECT_CLASS (gst_opencv_text_overlay_parent_class)->finalize (obj);
|
G_OBJECT_CLASS (gst_opencv_text_overlay_parent_class)->finalize (obj);
|
||||||
|
@ -144,12 +141,16 @@ static void
|
||||||
gst_opencv_text_overlay_class_init (GstOpencvTextOverlayClass * klass)
|
gst_opencv_text_overlay_class_init (GstOpencvTextOverlayClass * klass)
|
||||||
{
|
{
|
||||||
GObjectClass *gobject_class;
|
GObjectClass *gobject_class;
|
||||||
|
GstOpencvVideoFilterClass *gstopencvbasefilter_class;
|
||||||
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
GstElementClass *element_class = GST_ELEMENT_CLASS (klass);
|
||||||
gobject_class = (GObjectClass *) klass;
|
|
||||||
|
|
||||||
|
gobject_class = (GObjectClass *) klass;
|
||||||
gobject_class->finalize =
|
gobject_class->finalize =
|
||||||
GST_DEBUG_FUNCPTR (gst_opencv_text_overlay_finalize);
|
GST_DEBUG_FUNCPTR (gst_opencv_text_overlay_finalize);
|
||||||
|
gstopencvbasefilter_class = (GstOpencvVideoFilterClass *) klass;
|
||||||
|
|
||||||
|
gstopencvbasefilter_class->cv_trans_ip_func =
|
||||||
|
gst_opencv_text_overlay_transform_ip;
|
||||||
|
|
||||||
gobject_class->set_property = gst_opencv_text_overlay_set_property;
|
gobject_class->set_property = gst_opencv_text_overlay_set_property;
|
||||||
gobject_class->get_property = gst_opencv_text_overlay_get_property;
|
gobject_class->get_property = gst_opencv_text_overlay_get_property;
|
||||||
|
@ -227,20 +228,9 @@ gst_opencv_text_overlay_class_init (GstOpencvTextOverlayClass * klass)
|
||||||
static void
|
static void
|
||||||
gst_opencv_text_overlay_init (GstOpencvTextOverlay * filter)
|
gst_opencv_text_overlay_init (GstOpencvTextOverlay * filter)
|
||||||
{
|
{
|
||||||
filter->sinkpad = gst_pad_new_from_static_template (&sink_factory, "sink");
|
gst_pad_set_event_function (GST_BASE_TRANSFORM_SINK_PAD (filter),
|
||||||
GST_PAD_SET_PROXY_CAPS (filter->sinkpad);
|
|
||||||
gst_element_add_pad (GST_ELEMENT (filter), filter->sinkpad);
|
|
||||||
|
|
||||||
gst_pad_set_event_function (filter->sinkpad,
|
|
||||||
GST_DEBUG_FUNCPTR (gst_opencv_text_overlay_handle_sink_event));
|
GST_DEBUG_FUNCPTR (gst_opencv_text_overlay_handle_sink_event));
|
||||||
|
|
||||||
gst_pad_set_chain_function (filter->sinkpad,
|
|
||||||
GST_DEBUG_FUNCPTR (gst_opencv_text_overlay_chain));
|
|
||||||
|
|
||||||
filter->srcpad = gst_pad_new_from_static_template (&src_factory, "src");
|
|
||||||
GST_PAD_SET_PROXY_CAPS (filter->srcpad);
|
|
||||||
gst_element_add_pad (GST_ELEMENT (filter), filter->srcpad);
|
|
||||||
|
|
||||||
filter->textbuf = g_strdup (DEFAULT_PROP_TEXT);
|
filter->textbuf = g_strdup (DEFAULT_PROP_TEXT);
|
||||||
filter->width = DEFAULT_PROP_WIDTH;
|
filter->width = DEFAULT_PROP_WIDTH;
|
||||||
filter->height = DEFAULT_PROP_HEIGHT;
|
filter->height = DEFAULT_PROP_HEIGHT;
|
||||||
|
@ -250,6 +240,9 @@ gst_opencv_text_overlay_init (GstOpencvTextOverlay * filter)
|
||||||
filter->colorR = DEFAULT_PROP_COLOR;
|
filter->colorR = DEFAULT_PROP_COLOR;
|
||||||
filter->colorG = DEFAULT_PROP_COLOR;
|
filter->colorG = DEFAULT_PROP_COLOR;
|
||||||
filter->colorB = DEFAULT_PROP_COLOR;
|
filter->colorB = DEFAULT_PROP_COLOR;
|
||||||
|
|
||||||
|
gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter),
|
||||||
|
TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void
|
static void
|
||||||
|
@ -339,13 +332,10 @@ static gboolean
|
||||||
gst_opencv_text_overlay_handle_sink_event (GstPad * pad, GstObject * parent,
|
gst_opencv_text_overlay_handle_sink_event (GstPad * pad, GstObject * parent,
|
||||||
GstEvent * event)
|
GstEvent * event)
|
||||||
{
|
{
|
||||||
GstOpencvTextOverlay *filter;
|
|
||||||
gint width, height;
|
gint width, height;
|
||||||
GstStructure *structure;
|
GstStructure *structure;
|
||||||
gboolean res = TRUE;
|
gboolean res = TRUE;
|
||||||
|
|
||||||
filter = GST_OPENCV_TEXT_OVERLAY (parent);
|
|
||||||
|
|
||||||
switch (GST_EVENT_TYPE (event)) {
|
switch (GST_EVENT_TYPE (event)) {
|
||||||
case GST_EVENT_CAPS:
|
case GST_EVENT_CAPS:
|
||||||
{
|
{
|
||||||
|
@ -356,11 +346,6 @@ gst_opencv_text_overlay_handle_sink_event (GstPad * pad, GstObject * parent,
|
||||||
gst_structure_get_int (structure, "width", &width);
|
gst_structure_get_int (structure, "width", &width);
|
||||||
gst_structure_get_int (structure, "height", &height);
|
gst_structure_get_int (structure, "height", &height);
|
||||||
|
|
||||||
if (filter->cvImage) {
|
|
||||||
cvReleaseImage (&filter->cvImage);
|
|
||||||
}
|
|
||||||
filter->cvImage = cvCreateImage (cvSize (width, height), IPL_DEPTH_8U, 3);
|
|
||||||
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
default:
|
default:
|
||||||
|
@ -376,30 +361,18 @@ gst_opencv_text_overlay_handle_sink_event (GstPad * pad, GstObject * parent,
|
||||||
* this function does the actual processing
|
* this function does the actual processing
|
||||||
*/
|
*/
|
||||||
static GstFlowReturn
|
static GstFlowReturn
|
||||||
gst_opencv_text_overlay_chain (GstPad * pad, GstObject * parent,
|
gst_opencv_text_overlay_transform_ip (GstOpencvVideoFilter * base,
|
||||||
GstBuffer * buf)
|
GstBuffer * buf, IplImage * img)
|
||||||
{
|
{
|
||||||
GstOpencvTextOverlay *filter;
|
GstOpencvTextOverlay *filter = GST_OPENCV_TEXT_OVERLAY (base);
|
||||||
GstMapInfo map_info;
|
|
||||||
guint8 *data;
|
|
||||||
|
|
||||||
filter = GST_OPENCV_TEXT_OVERLAY (parent);
|
|
||||||
|
|
||||||
gst_buffer_map (buf, &map_info, GST_MAP_READ);
|
|
||||||
data = map_info.data;
|
|
||||||
|
|
||||||
filter->cvImage->imageData = (char *) data;
|
|
||||||
|
|
||||||
cvInitFont (&(filter->font), CV_FONT_VECTOR0, filter->width, filter->height,
|
cvInitFont (&(filter->font), CV_FONT_VECTOR0, filter->width, filter->height,
|
||||||
0, filter->thickness, 0);
|
0, filter->thickness, 0);
|
||||||
|
cvPutText (img, filter->textbuf, cvPoint (filter->xpos,
|
||||||
buf = gst_buffer_make_writable (buf);
|
|
||||||
cvPutText (filter->cvImage, filter->textbuf, cvPoint (filter->xpos,
|
|
||||||
filter->ypos), &(filter->font), cvScalar (filter->colorR,
|
filter->ypos), &(filter->font), cvScalar (filter->colorR,
|
||||||
filter->colorG, filter->colorB, 0));
|
filter->colorG, filter->colorB, 0));
|
||||||
|
|
||||||
gst_buffer_unmap (buf, &map_info);
|
return GST_FLOW_OK;
|
||||||
return gst_pad_push (filter->srcpad, buf);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -46,8 +46,7 @@
|
||||||
#ifndef __GST_OPENCV_TEXT_OVERLAY_H__
|
#ifndef __GST_OPENCV_TEXT_OVERLAY_H__
|
||||||
#define __GST_OPENCV_TEXT_OVERLAY_H__
|
#define __GST_OPENCV_TEXT_OVERLAY_H__
|
||||||
|
|
||||||
#include <gst/gst.h>
|
#include <gstopencvvideofilter.h>
|
||||||
#include <gst/video/video.h>
|
|
||||||
#include <opencv2/core/core_c.h>
|
#include <opencv2/core/core_c.h>
|
||||||
G_BEGIN_DECLS
|
G_BEGIN_DECLS
|
||||||
|
|
||||||
|
@ -68,11 +67,8 @@ typedef struct _GstOpencvTextOverlayClass GstOpencvTextOverlayClass;
|
||||||
|
|
||||||
struct _GstOpencvTextOverlay
|
struct _GstOpencvTextOverlay
|
||||||
{
|
{
|
||||||
GstElement element;
|
GstOpencvVideoFilter element;
|
||||||
|
|
||||||
GstPad *sinkpad, *srcpad;
|
|
||||||
|
|
||||||
IplImage *cvImage;
|
|
||||||
CvFont font;
|
CvFont font;
|
||||||
|
|
||||||
gint xpos;
|
gint xpos;
|
||||||
|
@ -87,7 +83,7 @@ struct _GstOpencvTextOverlay
|
||||||
|
|
||||||
struct _GstOpencvTextOverlayClass
|
struct _GstOpencvTextOverlayClass
|
||||||
{
|
{
|
||||||
GstElementClass parent_class;
|
GstOpencvVideoFilterClass parent_class;
|
||||||
};
|
};
|
||||||
|
|
||||||
GType gst_opencv_text_overlay_get_type (void);
|
GType gst_opencv_text_overlay_get_type (void);
|
||||||
|
|
Loading…
Reference in a new issue