From f58cd983b6a8460458e6ef406520f2c8e24a1e7b Mon Sep 17 00:00:00 2001 From: Luis de Bethencourt Date: Wed, 14 Oct 2015 15:30:05 +0100 Subject: [PATCH] opencv: switch remaining to C++ Switch remaining C files to C++ for consistency with other OpeCV elements, and support of the new 2.4.11 API. https://bugzilla.gnome.org/show_bug.cgi?id=754148 --- ext/opencv/Makefile.am | 6 +- .../{gstopencvutils.c => gstopencvutils.cpp} | 6 +- ...videofilter.c => gstopencvvideofilter.cpp} | 6 +- ...sttemplatematch.c => gsttemplatematch.cpp} | 133 ++++++++---------- 4 files changed, 71 insertions(+), 80 deletions(-) rename ext/opencv/{gstopencvutils.c => gstopencvutils.cpp} (98%) rename ext/opencv/{gstopencvvideofilter.c => gstopencvvideofilter.cpp} (98%) rename ext/opencv/{gsttemplatematch.c => gsttemplatematch.cpp} (96%) diff --git a/ext/opencv/Makefile.am b/ext/opencv/Makefile.am index c1d6b1ad90..0eee38c450 100644 --- a/ext/opencv/Makefile.am +++ b/ext/opencv/Makefile.am @@ -2,8 +2,8 @@ plugin_LTLIBRARIES = libgstopencv.la # sources used to compile this plug-in libgstopencv_la_SOURCES = gstopencv.cpp \ - gstopencvvideofilter.c \ - gstopencvutils.c \ + gstopencvvideofilter.cpp \ + gstopencvutils.cpp \ gstcvdilate.cpp \ gstcvdilateerode.cpp \ gstcvequalizehist.cpp \ @@ -15,7 +15,7 @@ libgstopencv_la_SOURCES = gstopencv.cpp \ gstfaceblur.cpp \ gsthanddetect.cpp \ gstpyramidsegment.cpp \ - gsttemplatematch.c \ + gsttemplatematch.cpp \ gsttextoverlay.cpp \ gstmotioncells.cpp \ gstskindetect.cpp \ diff --git a/ext/opencv/gstopencvutils.c b/ext/opencv/gstopencvutils.cpp similarity index 98% rename from ext/opencv/gstopencvutils.c rename to ext/opencv/gstopencvutils.cpp index 89901bfefc..f146b5ad6d 100644 --- a/ext/opencv/gstopencvutils.c +++ b/ext/opencv/gstopencvutils.cpp @@ -32,7 +32,8 @@ gst_opencv_get_ipl_depth_and_channels (GstStructure * structure, { GstVideoFormat format = GST_VIDEO_FORMAT_UNKNOWN; const GstVideoFormatInfo *info; - gint depth = 0, i; + gint depth = 0; + guint i; const gchar *s; if (gst_structure_has_name (structure, "video/x-raw")) { @@ -97,7 +98,8 @@ gst_opencv_parse_iplimage_params_from_caps (GstCaps * caps, gint * width, gint * height, gint * ipldepth, gint * channels, GError ** err) { GstVideoInfo info; - gint i, depth = 0; + gint depth = 0; + guint i; if (!gst_video_info_from_caps (&info, caps)) { GST_ERROR ("Failed to get the videoinfo from caps"); diff --git a/ext/opencv/gstopencvvideofilter.c b/ext/opencv/gstopencvvideofilter.cpp similarity index 98% rename from ext/opencv/gstopencvvideofilter.c rename to ext/opencv/gstopencvvideofilter.cpp index c53c7f5b15..5197b49b60 100644 --- a/ext/opencv/gstopencvvideofilter.c +++ b/ext/opencv/gstopencvvideofilter.cpp @@ -1,7 +1,7 @@ /* * GStreamer * Copyright (C) 2010 Thiago Santos - * + * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation @@ -135,7 +135,7 @@ gst_opencv_video_filter_class_init (GstOpencvVideoFilterClass * klass) gobject_class = (GObjectClass *) klass; basetrans_class = (GstBaseTransformClass *) klass; - parent_class = g_type_class_peek_parent (klass); + parent_class = (GstElementClass *) g_type_class_peek_parent (klass); GST_DEBUG_CATEGORY_INIT (gst_opencv_video_filter_debug, "opencvbasetransform", 0, "opencvbasetransform element"); @@ -217,7 +217,7 @@ gst_opencv_video_filter_transform_ip (GstBaseTransform * trans, g_return_val_if_fail (fclass->cv_trans_ip_func != NULL, GST_FLOW_ERROR); g_return_val_if_fail (transform->cvImage != NULL, GST_FLOW_ERROR); - if (!gst_buffer_map (buffer, &info, GST_MAP_READWRITE)) + if (!gst_buffer_map (buffer, &info, (GstMapFlags) (GST_MAP_READWRITE))) goto map_failed; transform->cvImage->imageData = (char *) info.data; diff --git a/ext/opencv/gsttemplatematch.c b/ext/opencv/gsttemplatematch.cpp similarity index 96% rename from ext/opencv/gsttemplatematch.c rename to ext/opencv/gsttemplatematch.cpp index 21d8cd4015..b064a5f002 100644 --- a/ext/opencv/gsttemplatematch.c +++ b/ext/opencv/gsttemplatematch.cpp @@ -4,7 +4,7 @@ * Copyright (C) 2005 Ronald S. Bultje * Copyright (C) 2008 Michael Sheldon * Copyright (C) 2009 Noam Lewis - * + * * Permission is hereby granted, free of charge, to any person obtaining a * copy of this software and associated documentation files (the "Software"), * to deal in the Software without restriction, including without limitation @@ -115,13 +115,6 @@ static gboolean gst_template_match_handle_sink_event (GstPad * pad, static GstFlowReturn gst_template_match_chain (GstPad * pad, GstObject * parent, GstBuffer * buf); -static void gst_template_match_load_template (GstTemplateMatch * filter, - gchar * template); -static void gst_template_match_match (IplImage * input, IplImage * template, - IplImage * dist_image, double *best_res, CvPoint * best_pos, int method); - - - /* initialize the templatematch's class */ static void gst_template_match_class_init (GstTemplateMatchClass * klass) @@ -138,14 +131,14 @@ gst_template_match_class_init (GstTemplateMatchClass * klass) g_object_class_install_property (gobject_class, PROP_METHOD, g_param_spec_int ("method", "Method", "Specifies the way the template must be compared with image regions. 0=SQDIFF, 1=SQDIFF_NORMED, 2=CCOR, 3=CCOR_NORMED, 4=CCOEFF, 5=CCOEFF_NORMED.", - 0, 5, DEFAULT_METHOD, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + 0, 5, DEFAULT_METHOD, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS))); g_object_class_install_property (gobject_class, PROP_TEMPLATE, g_param_spec_string ("template", "Template", "Filename of template image", - NULL, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + NULL, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS))); g_object_class_install_property (gobject_class, PROP_DISPLAY, g_param_spec_boolean ("display", "Display", "Sets whether the detected template should be highlighted in the output", - TRUE, G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS)); + TRUE, (GParamFlags) (G_PARAM_READWRITE | G_PARAM_STATIC_STRINGS))); gst_element_class_set_static_metadata (element_class, "templatematch", @@ -187,6 +180,42 @@ gst_template_match_init (GstTemplateMatch * filter) filter->method = DEFAULT_METHOD; } +/* We take ownership of template here */ +static void +gst_template_match_load_template (GstTemplateMatch * filter, gchar * templ) +{ + gchar *oldTemplateFilename = NULL; + IplImage *oldTemplateImage = NULL, *newTemplateImage = NULL, *oldDistImage = + NULL; + + if (templ) { + newTemplateImage = cvLoadImage (templ, CV_LOAD_IMAGE_COLOR); + if (!newTemplateImage) { + /* Unfortunately OpenCV doesn't seem to provide any way of finding out + why the image load failed, so we can't be more specific than FAILED: */ + GST_ELEMENT_WARNING (filter, RESOURCE, FAILED, + (_("OpenCV failed to load template image")), + ("While attempting to load template '%s'", templ)); + g_free (templ); + templ = NULL; + } + } + + GST_OBJECT_LOCK (filter); + oldTemplateFilename = filter->templ; + filter->templ = templ; + oldTemplateImage = filter->cvTemplateImage; + filter->cvTemplateImage = newTemplateImage; + oldDistImage = filter->cvDistImage; + /* This will be recreated in the chain function as required: */ + filter->cvDistImage = NULL; + GST_OBJECT_UNLOCK (filter); + + cvReleaseImage (&oldDistImage); + cvReleaseImage (&oldTemplateImage); + g_free (oldTemplateFilename); +} + static void gst_template_match_set_property (GObject * object, guint prop_id, const GValue * value, GParamSpec * pspec) @@ -312,6 +341,26 @@ gst_template_match_finalize (GObject * object) G_OBJECT_CLASS (gst_template_match_parent_class)->finalize (object); } +static void +gst_template_match_match (IplImage * input, IplImage * templ, + IplImage * dist_image, double *best_res, CvPoint * best_pos, int method) +{ + double dist_min = 0, dist_max = 0; + CvPoint min_pos, max_pos; + cvMatchTemplate (input, templ, dist_image, method); + cvMinMaxLoc (dist_image, &dist_min, &dist_max, &min_pos, &max_pos, NULL); + if ((CV_TM_SQDIFF_NORMED == method) || (CV_TM_SQDIFF == method)) { + *best_res = dist_min; + *best_pos = min_pos; + if (CV_TM_SQDIFF_NORMED == method) { + *best_res = 1 - *best_res; + } + } else { + *best_res = dist_max; + *best_pos = max_pos; + } +} + /* chain function * this function does the actual processing */ @@ -332,7 +381,7 @@ gst_template_match_chain (GstPad * pad, GstObject * parent, GstBuffer * buf) GST_LOG_OBJECT (filter, "Buffer size %u", (guint) gst_buffer_get_size (buf)); buf = gst_buffer_make_writable (buf); - gst_buffer_map (buf, &info, GST_MAP_READWRITE); + gst_buffer_map (buf, &info, (GstMapFlags) (GST_MAP_READWRITE)); filter->cvImage->imageData = (char *) info.data; GST_OBJECT_LOCK (filter); @@ -402,66 +451,6 @@ gst_template_match_chain (GstPad * pad, GstObject * parent, GstBuffer * buf) return gst_pad_push (filter->srcpad, buf); } - - -static void -gst_template_match_match (IplImage * input, IplImage * templ, - IplImage * dist_image, double *best_res, CvPoint * best_pos, int method) -{ - double dist_min = 0, dist_max = 0; - CvPoint min_pos, max_pos; - cvMatchTemplate (input, templ, dist_image, method); - cvMinMaxLoc (dist_image, &dist_min, &dist_max, &min_pos, &max_pos, NULL); - if ((CV_TM_SQDIFF_NORMED == method) || (CV_TM_SQDIFF == method)) { - *best_res = dist_min; - *best_pos = min_pos; - if (CV_TM_SQDIFF_NORMED == method) { - *best_res = 1 - *best_res; - } - } else { - *best_res = dist_max; - *best_pos = max_pos; - } -} - - -/* We take ownership of template here */ -static void -gst_template_match_load_template (GstTemplateMatch * filter, gchar * templ) -{ - gchar *oldTemplateFilename = NULL; - IplImage *oldTemplateImage = NULL, *newTemplateImage = NULL, *oldDistImage = - NULL; - - if (templ) { - newTemplateImage = cvLoadImage (templ, CV_LOAD_IMAGE_COLOR); - if (!newTemplateImage) { - /* Unfortunately OpenCV doesn't seem to provide any way of finding out - why the image load failed, so we can't be more specific than FAILED: */ - GST_ELEMENT_WARNING (filter, RESOURCE, FAILED, - (_("OpenCV failed to load template image")), - ("While attempting to load template '%s'", templ)); - g_free (templ); - templ = NULL; - } - } - - GST_OBJECT_LOCK (filter); - oldTemplateFilename = filter->templ; - filter->templ = templ; - oldTemplateImage = filter->cvTemplateImage; - filter->cvTemplateImage = newTemplateImage; - oldDistImage = filter->cvDistImage; - /* This will be recreated in the chain function as required: */ - filter->cvDistImage = NULL; - GST_OBJECT_UNLOCK (filter); - - cvReleaseImage (&oldDistImage); - cvReleaseImage (&oldTemplateImage); - g_free (oldTemplateFilename); -} - - /* entry point to initialize the plug-in * initialize the plug-in itself * register the element factories and other features