Template Matcher for simple geometric shapes

asked 2014-11-17 12:03:07 -0500

Doombot gravatar image

updated 2014-11-18 12:42:14 -0500

The question : Is it possible to do template matching in OpenCV with a "masked" template? I mean, imagine you want to mask a binary image containing a simple black diamond shape on a white background with a binary image containing lots simple shapes (rectangles, squares, smaller diamonds). To my understanding, doing a simple template matching would yield poor results since it would try to match the white background of the image too, yielding to poor results. So does anyone knows how to use a "mask" with the template to just match the black pixels?

Context :

Let's say that I have a lot of rectangular-ly shapes on an image, about all of the same size, rotated arbitrarily around the plane but some occluded by the others. The problem is that they are not pure rectangles; some sides might be a little wavy, a little bit like if someone drew them by hand, and there might be discontinuities in the sides, a little bit like if someone scanned a drawing and that bits are missing. But they are mostly rectangles on a binary image.

I would like to be able to locate the rectangles (well at least one of them would be ok) in real-time. If the pseudo-rectangles had been laid regularly on a grid with a constant orientation, I could have used a basic template matching with a "perfect" rectangle. While there would have been no 100% match because of the imperfection on the query image, the correlation would be good enough to tell the location of the rectangles.

The problem is with the rotation. I cannot use feature descriptors such as ORB, etc. since, well apart from the line segments, there are no other features and I don't think they are discriminate enough to do the job. Using Hough lines is by experience not that conclusive since the lines are not almost perfect lines but that was on Matlab. And Hough Lines tend to be relatively slow.

So I realized that using a template matching algorithm that would simply rotate the template and attempt to match at different orientation would probably be my best best. I know this is a brute force method and that it is relatively inefficient, but I am pretty sure it could work since I am matching binary images.

edit retag flag offensive close merge delete