From 6e43f75df3340fca4b9d084c51933a191f7737e2 Mon Sep 17 00:00:00 2001 From: Josh Doe Date: Fri, 19 Nov 2010 15:23:41 -0500 Subject: [PATCH] opencv: fix caps issues and extend supported caps for some elements Some elements had vague caps, such as "video/x-raw-rgb", which caused problems at least with textwrite. For other elements, the underlying OpenCV functions support more than just one image type, so I increased the number of supported caps. I created a utility function "gst_opencv_caps_from_cv_image_type", so each element creates caps directly from OpenCV image types, such as CV_8UC1 for 8-bit grayscale. This function uses gstvideo to create uniform caps. https://bugzilla.gnome.org/show_bug.cgi?id=635304 --- ext/opencv/gstcvdilateerode.c | 32 +++++++++++++++----------------- ext/opencv/gstcvequalizehist.c | 5 +++-- ext/opencv/gstcvlaplace.c | 20 +++++++++++++++----- ext/opencv/gstcvsmooth.c | 33 ++++++++++++++------------------- ext/opencv/gstcvsobel.c | 20 +++++++++++++++----- ext/opencv/gstedgedetect.c | 5 +++-- ext/opencv/gstfaceblur.c | 5 +++-- ext/opencv/gstopencvutils.c | 30 ++++++++++++++++++++++++++++++ ext/opencv/gstopencvutils.h | 4 ++++ ext/opencv/gstpyramidsegment.c | 5 +++-- ext/opencv/gsttemplatematch.c | 5 +++-- ext/opencv/gsttextwrite.c | 5 +++-- 12 files changed, 111 insertions(+), 58 deletions(-) diff --git a/ext/opencv/gstcvdilateerode.c b/ext/opencv/gstcvdilateerode.c index c304a4ffa3..8b689f21b7 100644 --- a/ext/opencv/gstcvdilateerode.c +++ b/ext/opencv/gstcvdilateerode.c @@ -52,6 +52,7 @@ #include +#include "gstopencvutils.h" #include "gstcvdilateerode.h" /* @@ -59,19 +60,6 @@ GST_DEBUG_CATEGORY_STATIC (gst_cv_dilate_erode_debug); #define GST_CAT_DEFAULT gst_cv_dilate_erode_debug */ -static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", - GST_PAD_SINK, - GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb, depth=(int)24, bpp=(int)24;" - "video/x-raw-gray") - ); - -static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", - GST_PAD_SRC, - GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb, depth=(int)24, bpp=(int)24;" - "video/x-raw-gray") - ); /* Filter signals and args */ enum @@ -143,11 +131,21 @@ static void gst_cv_dilate_erode_base_init (gpointer gclass) { GstElementClass *element_class = GST_ELEMENT_CLASS (gclass); + GstCaps *caps; + GstPadTemplate *templ; - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&src_factory)); - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&sink_factory)); + /* add sink and source pad templates */ + caps = gst_opencv_caps_from_cv_image_type (CV_16UC1); + gst_caps_append (caps, gst_opencv_caps_from_cv_image_type (CV_8UC4)); + gst_caps_append (caps, gst_opencv_caps_from_cv_image_type (CV_8UC3)); + gst_caps_append (caps, gst_opencv_caps_from_cv_image_type (CV_8UC1)); + templ = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, + gst_caps_ref (caps)); + gst_element_class_add_pad_template (element_class, templ); + gst_object_unref (templ); + templ = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps); + gst_element_class_add_pad_template (element_class, templ); + gst_object_unref (templ); } /* initialize the cvdilate_erode's class */ diff --git a/ext/opencv/gstcvequalizehist.c b/ext/opencv/gstcvequalizehist.c index 9e2b0306e4..9f2ebf2a2a 100644 --- a/ext/opencv/gstcvequalizehist.c +++ b/ext/opencv/gstcvequalizehist.c @@ -47,6 +47,7 @@ #include +#include "gstopencvutils.h" #include "gstcvequalizehist.h" GST_DEBUG_CATEGORY_STATIC (gst_cv_equalize_hist_debug); @@ -55,12 +56,12 @@ GST_DEBUG_CATEGORY_STATIC (gst_cv_equalize_hist_debug); static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-gray, depth=(int)8, bpp=(int)8")); + GST_STATIC_CAPS (GST_VIDEO_CAPS_GRAY8)); static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-gray, depth=(int)8, bpp=(int)8")); + GST_STATIC_CAPS (GST_VIDEO_CAPS_GRAY8)); GST_BOILERPLATE (GstCvEqualizeHist, gst_cv_equalize_hist, GstOpencvVideoFilter, GST_TYPE_OPENCV_VIDEO_FILTER); diff --git a/ext/opencv/gstcvlaplace.c b/ext/opencv/gstcvlaplace.c index f528428719..eddcbb12cc 100644 --- a/ext/opencv/gstcvlaplace.c +++ b/ext/opencv/gstcvlaplace.c @@ -47,6 +47,7 @@ #include +#include "gstopencvutils.h" #include "gstcvlaplace.h" GST_DEBUG_CATEGORY_STATIC (gst_cv_laplace_debug); @@ -55,14 +56,19 @@ GST_DEBUG_CATEGORY_STATIC (gst_cv_laplace_debug); static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-gray, depth=(int)8, bpp=(int)8") + GST_STATIC_CAPS (GST_VIDEO_CAPS_GRAY8) ); +#if G_BYTE_ORDER == G_BIG_ENDIAN + #define BYTE_ORDER_STRING "BIG_ENDIAN" +#else + #define BYTE_ORDER_STRING "LITTLE_ENDIAN" +#endif + static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS - ("video/x-raw-gray, depth=(int)16, bpp=(int)16, endianness=(int)4321") + GST_STATIC_CAPS (GST_VIDEO_CAPS_GRAY16 (BYTE_ORDER_STRING)) ); /* Filter signals and args */ @@ -213,14 +219,18 @@ gst_cv_laplace_transform_caps (GstBaseTransform * trans, GstPadDirection dir, structure = gst_caps_get_structure (output, i); gst_structure_set (structure, "depth", G_TYPE_INT, 16, - "bpp", G_TYPE_INT, 16, "endianness", G_TYPE_INT, 4321, NULL); + "bpp", G_TYPE_INT, 16, + "endianness", G_TYPE_INT, G_BYTE_ORDER, + NULL); } break; case GST_PAD_SRC: for (i = 0; i < gst_caps_get_size (output); i++) { structure = gst_caps_get_structure (output, i); gst_structure_set (structure, - "depth", G_TYPE_INT, 8, "bpp", G_TYPE_INT, 8, NULL); + "depth", G_TYPE_INT, 8, + "bpp", G_TYPE_INT, 8, + NULL); gst_structure_remove_field (structure, "endianness"); } break; diff --git a/ext/opencv/gstcvsmooth.c b/ext/opencv/gstcvsmooth.c index 46d880dc60..62f54b27fe 100644 --- a/ext/opencv/gstcvsmooth.c +++ b/ext/opencv/gstcvsmooth.c @@ -47,25 +47,12 @@ #include +#include "gstopencvutils.h" #include "gstcvsmooth.h" GST_DEBUG_CATEGORY_STATIC (gst_cv_smooth_debug); #define GST_CAT_DEFAULT gst_cv_smooth_debug -static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", - GST_PAD_SINK, - GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb, depth=(int)24, bpp=(int)24;" - "video/x-raw-gray, depth=(int)8, bpp=(int)8") - ); - -static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", - GST_PAD_SRC, - GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb, depth=(int)24, bpp=(int)24;" - "video/x-raw-gray, depth=(int)8, bpp=(int)8") - ); - /* Filter signals and args */ enum { @@ -146,17 +133,25 @@ static void gst_cv_smooth_base_init (gpointer gclass) { GstElementClass *element_class = GST_ELEMENT_CLASS (gclass); - - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&src_factory)); - gst_element_class_add_pad_template (element_class, - gst_static_pad_template_get (&sink_factory)); + GstCaps *caps; + GstPadTemplate *templ; gst_element_class_set_details_simple (element_class, "cvsmooth", "Transform/Effect/Video", "Applies cvSmooth OpenCV function to the image", "Thiago Santos"); + + /* add sink and source pad templates */ + caps = gst_opencv_caps_from_cv_image_type (CV_8UC3); + gst_caps_append (caps, gst_opencv_caps_from_cv_image_type (CV_8UC1)); + templ = gst_pad_template_new ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, + gst_caps_ref (caps)); + gst_element_class_add_pad_template (element_class, templ); + gst_object_unref (templ); + templ = gst_pad_template_new ("src", GST_PAD_SRC, GST_PAD_ALWAYS, caps); + gst_element_class_add_pad_template (element_class, templ); + gst_object_unref (templ); } /* initialize the cvsmooth's class */ diff --git a/ext/opencv/gstcvsobel.c b/ext/opencv/gstcvsobel.c index eaeeeb8044..9ee55d9158 100644 --- a/ext/opencv/gstcvsobel.c +++ b/ext/opencv/gstcvsobel.c @@ -47,6 +47,7 @@ #include +#include "gstopencvutils.h" #include "gstcvsobel.h" GST_DEBUG_CATEGORY_STATIC (gst_cv_sobel_debug); @@ -55,14 +56,19 @@ GST_DEBUG_CATEGORY_STATIC (gst_cv_sobel_debug); static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-gray, depth=(int)8, bpp=(int)8") + GST_STATIC_CAPS (GST_VIDEO_CAPS_GRAY8) ); +#if G_BYTE_ORDER == G_BIG_ENDIAN + #define BYTE_ORDER_STRING "BIG_ENDIAN" +#else + #define BYTE_ORDER_STRING "LITTLE_ENDIAN" +#endif + static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS - ("video/x-raw-gray, depth=(int)16, bpp=(int)16, endianness=(int)4321") + GST_STATIC_CAPS (GST_VIDEO_CAPS_GRAY16 (BYTE_ORDER_STRING)) ); /* Filter signals and args */ @@ -188,14 +194,18 @@ gst_cv_sobel_transform_caps (GstBaseTransform * trans, GstPadDirection dir, structure = gst_caps_get_structure (output, i); gst_structure_set (structure, "depth", G_TYPE_INT, 16, - "bpp", G_TYPE_INT, 16, "endianness", G_TYPE_INT, 4321, NULL); + "bpp", G_TYPE_INT, 16, + "endianness", G_TYPE_INT, G_BYTE_ORDER, + NULL); } break; case GST_PAD_SRC: for (i = 0; i < gst_caps_get_size (output); i++) { structure = gst_caps_get_structure (output, i); gst_structure_set (structure, - "depth", G_TYPE_INT, 8, "bpp", G_TYPE_INT, 8, NULL); + "depth", G_TYPE_INT, 8, + "bpp", G_TYPE_INT, 8, + NULL); gst_structure_remove_field (structure, "endianness"); } break; diff --git a/ext/opencv/gstedgedetect.c b/ext/opencv/gstedgedetect.c index 19094eae69..f6d90e09b3 100644 --- a/ext/opencv/gstedgedetect.c +++ b/ext/opencv/gstedgedetect.c @@ -62,6 +62,7 @@ #include +#include "gstopencvutils.h" #include "gstedgedetect.h" GST_DEBUG_CATEGORY_STATIC (gst_edgedetect_debug); @@ -90,13 +91,13 @@ enum static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb") + GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB) ); static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb") + GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB) ); GST_BOILERPLATE (Gstedgedetect, gst_edgedetect, GstElement, GST_TYPE_ELEMENT); diff --git a/ext/opencv/gstfaceblur.c b/ext/opencv/gstfaceblur.c index ddbfcdc288..8f901af90c 100644 --- a/ext/opencv/gstfaceblur.c +++ b/ext/opencv/gstfaceblur.c @@ -62,6 +62,7 @@ #include +#include "gstopencvutils.h" #include "gstfaceblur.h" GST_DEBUG_CATEGORY_STATIC (gst_faceblur_debug); @@ -87,13 +88,13 @@ enum static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb") + GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB) ); static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb") + GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB) ); GST_BOILERPLATE (Gstfaceblur, gst_faceblur, GstElement, GST_TYPE_ELEMENT); diff --git a/ext/opencv/gstopencvutils.c b/ext/opencv/gstopencvutils.c index 0dbdde3f35..1f5502a158 100644 --- a/ext/opencv/gstopencvutils.c +++ b/ext/opencv/gstopencvutils.c @@ -92,3 +92,33 @@ gst_opencv_parse_iplimage_params_from_caps (GstCaps * caps, gint * width, gst_opencv_parse_iplimage_params_from_structure (gst_caps_get_structure (caps, 0), width, height, ipldepth, channels, err); } + +GstCaps * +gst_opencv_caps_from_cv_image_type (int cv_type) +{ + GstCaps * caps = gst_caps_new_empty (); + switch (cv_type) { + case CV_8UC1: + gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_GRAY8)); + break; + case CV_8UC3: + gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_RGB)); + gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_BGR)); + break; + case CV_8UC4: + gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_RGBx)); + gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_xRGB)); + gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_BGRx)); + gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_xBGR)); + gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_RGBA)); + gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_ARGB)); + gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_BGRA)); + gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_ABGR)); + break; + case CV_16UC1: + gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_GRAY16("1234"))); + gst_caps_append (caps, gst_caps_from_string (GST_VIDEO_CAPS_GRAY16("4321"))); + break; + } + return caps; +} \ No newline at end of file diff --git a/ext/opencv/gstopencvutils.h b/ext/opencv/gstopencvutils.h index d4602a7c6a..66efac71b5 100644 --- a/ext/opencv/gstopencvutils.h +++ b/ext/opencv/gstopencvutils.h @@ -27,6 +27,8 @@ #endif #include +#include + #include gboolean @@ -39,4 +41,6 @@ gboolean gst_opencv_parse_iplimage_params_from_structure (GstStructure * structure, gint * width, gint * height, gint * depth, gint * channels, GError ** err); +GstCaps * gst_opencv_caps_from_cv_image_type (int cv_type); + #endif /* __GST_OPENCV_UTILS__ */ diff --git a/ext/opencv/gstpyramidsegment.c b/ext/opencv/gstpyramidsegment.c index 53bdfd207c..760c571345 100644 --- a/ext/opencv/gstpyramidsegment.c +++ b/ext/opencv/gstpyramidsegment.c @@ -62,6 +62,7 @@ #include +#include "gstopencvutils.h" #include "gstpyramidsegment.h" #define BLOCK_SIZE 1000 @@ -92,13 +93,13 @@ enum static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb") + GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB) ); static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb") + GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB) ); GST_BOILERPLATE (Gstpyramidsegment, gst_pyramidsegment, GstElement, diff --git a/ext/opencv/gsttemplatematch.c b/ext/opencv/gsttemplatematch.c index f8fde9c3d7..8e96d1ac1d 100644 --- a/ext/opencv/gsttemplatematch.c +++ b/ext/opencv/gsttemplatematch.c @@ -63,6 +63,7 @@ #include +#include "gstopencvutils.h" #include "gsttemplatematch.h" GST_DEBUG_CATEGORY_STATIC (gst_templatematch_debug); @@ -90,13 +91,13 @@ enum static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb") + GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB) ); static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb") + GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB) ); GST_BOILERPLATE (GstTemplateMatch, gst_templatematch, GstElement, diff --git a/ext/opencv/gsttextwrite.c b/ext/opencv/gsttextwrite.c index 947c723f82..0a84bbab4f 100644 --- a/ext/opencv/gsttextwrite.c +++ b/ext/opencv/gsttextwrite.c @@ -62,6 +62,7 @@ #include +#include "gstopencvutils.h" #include "gsttextwrite.h" GST_DEBUG_CATEGORY_STATIC (gst_textwrite_debug); @@ -104,13 +105,13 @@ enum static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("video/x-raw-rgb") + GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB) ); static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS ("ANY") + GST_STATIC_CAPS (GST_VIDEO_CAPS_RGB) ); GST_BOILERPLATE (Gsttextwrite, gst_textwrite, GstElement, GST_TYPE_ELEMENT);