4

As per the question title. I'm trying to find a set of templates in a set of scenes. The images of the templated objects appear exactly as-is in the images except for image quality issues, background of the template image, and size differences. Think of it like finding the Fox News logo in news footage given a set of news station logo template images with a blank background. In the news footage the logo would have a natural image background.

I have implemented multi-scale template matching using an approach similar to this link. However, I am not getting good results. I use a Canny edge representation of the template, and use cv2.matchTemplate. For each template, I find the highest score for that particular template in the image, and I pick the template that returns the highest score as the correct result. I've tried every metric available to the template matcher and the results are still poor.

I'm thinking that the color information might be useful as well, but RGB template matching is not included in OpenCV. I thought of doing it per-channel, but if I get a different result for each channel, I don't know how to reconcile the position and score for that template.

Any ideas?

Hal T
  • 527
  • 7
  • 22
  • why do you want to do template matching? why not training a small classifier? – Elmira Oct 31 '16 at 20:31
  • I would need to find the logo within the image to classify it first, no? I can do a region proposal which I can then feed to a classifier but I need some way to find the region of the item. – Hal T Nov 01 '16 at 13:22
  • yeah ! But nothing is different for template matching as well. In fact, the classifier will give you a template which is more general/robust to different situations. I don't know how much you're comfortable with training a detector instead of classifier but that would be an option too. – Elmira Nov 01 '16 at 16:12
  • One issue I see with this is that, since the logos do not vary per class, I will have 1 sample per class. I'm not aware of a method that can produce strong classification with this, and I was in fact trying to use template matching as a detector. What would you suggest? Feel free to post it as an answer so I can mark this solved and we can move the discussion into its own thread. – Hal T Nov 02 '16 at 13:02

1 Answers1

1

The tutorial you are referencing does the Canny Edge detection as a preliminary step to template matching, you do not have to completely follow this example. You can pass a 3 layer image to the match template functions. There is no need to split the image into 3 layers.

Referencing the code in the links example you simply can omit the step where the image is converted to greyscale.

for imagePath in glob.glob(args["images"] + "/*.jpg"):
    # load the image, convert it to grayscale, and initialize the
    # bookkeeping variable to keep track of the matched region
    image = cv2.imread(imagePath)
    #remove the following line
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    found = None

The matchTemplate function in openCV does not require the template to be greyscale. You can just pass the RGB template to the function.

see: https://docs.opencv.org/4.1.2/df/dfb/group__imgproc__object.html#ga586ebfb0a7fb604b35a23d85391329be

I personally use RGB images for template matching, a drastic color difference sometimes affects my results but generally using color templates works fine for me. This theoretically, should improve your results if you are looking for a logo.

Josh Sharkey
  • 1,008
  • 9
  • 34