From 14dd6b708823e58aa213a9c36957be82fa5fe860 Mon Sep 17 00:00:00 2001 From: Luis de Bethencourt Date: Fri, 8 Jan 2016 12:39:29 +0000 Subject: [PATCH] opencv: do pyramidsegment's transformation in place Run the transform function of pyramidsegment in place, reusing the image data as both source and destination in cvPyrSegmentation. This avoids copying the image back and forth and the extra memory. --- ext/opencv/gstpyramidsegment.cpp | 28 ++++++++-------------------- ext/opencv/gstpyramidsegment.h | 2 -- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/ext/opencv/gstpyramidsegment.cpp b/ext/opencv/gstpyramidsegment.cpp index 0c9f1e9735..d8247c06d7 100644 --- a/ext/opencv/gstpyramidsegment.cpp +++ b/ext/opencv/gstpyramidsegment.cpp @@ -108,8 +108,8 @@ static void gst_pyramid_segment_set_property (GObject * object, guint prop_id, static void gst_pyramid_segment_get_property (GObject * object, guint prop_id, GValue * value, GParamSpec * pspec); -static GstFlowReturn gst_pyramid_segment_transform (GstOpencvVideoFilter * base, - GstBuffer * buf, IplImage * img, GstBuffer * outbuf, IplImage * outimg); +static GstFlowReturn gst_pyramid_segment_transform_ip (GstOpencvVideoFilter * + base, GstBuffer * buf, IplImage * img); /* Clean up */ static void @@ -137,7 +137,8 @@ gst_pyramid_segment_class_init (GstPyramidSegmentClass * klass) gobject_class->set_property = gst_pyramid_segment_set_property; gobject_class->get_property = gst_pyramid_segment_get_property; - gstopencvbasefilter_class->cv_trans_func = gst_pyramid_segment_transform; + gstopencvbasefilter_class->cv_trans_ip_func = + gst_pyramid_segment_transform_ip; g_object_class_install_property (gobject_class, PROP_SILENT, g_param_spec_boolean ("silent", "Silent", "Produce verbose output ?", @@ -187,7 +188,7 @@ gst_pyramid_segment_init (GstPyramidSegment * filter) filter->level = 4; gst_opencv_video_filter_set_in_place (GST_OPENCV_VIDEO_FILTER_CAST (filter), - FALSE); + TRUE); } static void @@ -244,27 +245,14 @@ gst_pyramid_segment_get_property (GObject * object, guint prop_id, * this function does the actual processing */ static GstFlowReturn -gst_pyramid_segment_transform (GstOpencvVideoFilter * base, GstBuffer * buf, - IplImage * img, GstBuffer * outbuf, IplImage * outimg) +gst_pyramid_segment_transform_ip (GstOpencvVideoFilter * base, GstBuffer * buf, + IplImage * img) { GstPyramidSegment *filter = GST_PYRAMID_SEGMENT (base); - GstMapInfo outinfo; - filter->cvSegmentedImage = cvCloneImage (img); - - cvPyrSegmentation (img, filter->cvSegmentedImage, filter->storage, + cvPyrSegmentation (img, img, filter->storage, &(filter->comp), filter->level, filter->threshold1, filter->threshold2); - /* TODO look if there is a way in opencv to reuse the image data and - * delete only the struct headers. Would avoid a memcpy here */ - - gst_buffer_map (outbuf, &outinfo, GST_MAP_WRITE); - memcpy (outinfo.data, filter->cvSegmentedImage->imageData, - gst_buffer_get_size (outbuf)); - - cvReleaseImage (&filter->cvSegmentedImage); - g_assert (filter->cvSegmentedImage == NULL); - return GST_FLOW_OK; } diff --git a/ext/opencv/gstpyramidsegment.h b/ext/opencv/gstpyramidsegment.h index bb87253186..20d0142d58 100644 --- a/ext/opencv/gstpyramidsegment.h +++ b/ext/opencv/gstpyramidsegment.h @@ -77,8 +77,6 @@ struct _GstPyramidSegment gboolean silent; - IplImage *cvSegmentedImage; - CvMemStorage *storage; CvSeq *comp;