From f5992a6cdd13c5460a9dd2bef17c7502a0816237 Mon Sep 17 00:00:00 2001 From: David Rothlisberger Date: Fri, 15 Jun 2012 13:19:06 +0100 Subject: [PATCH] opencv templatematch: Set caps to BGR order templatematch operates on BGR data. In fact, OpenCV's IplImage always stores color image data in BGR order -- this isn't documented at all in the OpenCV source code, but there are hints around the web (see for example http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html#SECTION00041000000000000000 and http://www.comp.leeds.ac.uk/vision/opencv/iplimage.html ). gst_templatematch_load_template loads the template (the image to find) from disk using OpenCV's cvLoadImage, so it is stored in an IplImage in BGR order. But in gst_templatematch_chain, no OpenCV conversion functions are used: the imageData pointer of the IplImage for the video frame (the image to search in) is just set to point to the raw buffer data. Without this fix, that raw data is in RGB order, so the call to cvMatchTemplate ends up comparing the template's Blue channel against the frame's Red channel, producing very poor results. --- ext/opencv/gsttemplatematch.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ext/opencv/gsttemplatematch.c b/ext/opencv/gsttemplatematch.c index 1256db8607..99b9594766 100644 --- a/ext/opencv/gsttemplatematch.c +++ b/ext/opencv/gsttemplatematch.c @@ -92,13 +92,13 @@ enum static GstStaticPadTemplate sink_factory = GST_STATIC_PAD_TEMPLATE ("sink", GST_PAD_SINK, GST_PAD_ALWAYS, - GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB")) + GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("BGR")) ); static GstStaticPadTemplate src_factory = GST_STATIC_PAD_TEMPLATE ("src", GST_PAD_SRC, GST_PAD_ALWAYS, - GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("RGB")) + GST_STATIC_CAPS (GST_VIDEO_CAPS_MAKE ("BGR")) ); G_DEFINE_TYPE (GstTemplateMatch, gst_template_match, GST_TYPE_ELEMENT);