Ask Your Question

Way to filter out False Positives in Template Matching

asked 2017-07-21 03:23:50 -0500

Raki gravatar image

updated 2017-07-25 08:08:00 -0500

Hi all,

I am trying to detect the screws on metal surfaces so I've implemented a trivial template matcher, in which I feed templates of screws in and spot them in the scene. As you know, in such a case, we have a threshold value (like a sensitivity value) that we tweak based on the false/true negatives we get in the output.

So the very typical problem is that if I set to to a high value, it filters too much and loses the true positives, and when I set it to too low, it gives way too many false positives. The way I see it, there is going to be an optimum value for me, and after that it all will depend on "how further I could filter the false positives out". In this part exactly, I need your help.

image description

How do you think I can filter out the false positives? They are caused by similarities of pixels (of templates) and the parts of the scene, but this is something I cannot change. So far I thought about dividing the surface in the regions and marking down the expected regions where screws are usually expected, kinda like ROI thingy.

image description

But not sure if it makes sense in the long run, since this holds true for harddisks perhaps (screws are always on certain locations) but not other devices.

How do you think one can overcome this problem?

EDIT: So instead using partial templates, I've decided to use "complete" templates (full screws), however now I am increasing number of templates I am looping over. That is, I am creating screw templates from images (of scenes) which are different from each other. For instance, some screw templates now has sunlight reflecting on them whereas others look quite dark. See the following templates;

image description image description image description

and then the results I am having are looking like:

image description

Then I think I am now down to the question of " How to find the screws which are not completely visible?"

edit retag flag offensive close merge delete


How do you perform template match (method) ? Has your template same size/rotation of finding ? How do you perform multiple matching filtering (do you clean scores around) ?

read this is-template-matching-the-best-approach-to-go-about-when-finding-the-exact-image-on-the-screen-multiple-times, Template Matching is wrong with specific Reference image

pklab gravatar imagepklab ( 2017-07-24 09:16:36 -0500 )edit

I am using the sample given by OpenCV here. I have several templates though (quarter screw, half screw, etc), instead of 1. I loop over each and look for it in the image, mark it. And BTW I am using the TM_CCOEFF_NORMED way, which I think is the best resulting one. I tried all others as well but this seems to be the one that I should use.

Raki gravatar imageRaki ( 2017-07-25 05:27:41 -0500 )edit

1 answer

Sort by ยป oldest newest most voted

answered 2017-07-25 06:16:18 -0500

pklab gravatar image

updated 2017-07-25 09:53:06 -0500

1st: It looks you do not clear the score around your match before to locate next match.

This is needed because around the highest match you have a lot of quasi max matches due to small translation of the template around best location. For example your 1st match is 0.9, your 2nd best match has score 0.8 but around your 1st match you might have scores like 0.85 that would produces false match. That's why you have to ignore all scores around a match within an area same as your template.

You can achieve this drawing a filled rectangle in the score images, centred in the location match, using a "color value" lower out of your threshold (the tutorial should be updated with this).

2nd: Using half or quarter screw is too simple template this would produces a lot of false matches. I suggest to use full screw with a bit of frame around as template. If the screw is '+' type than you can use 2 templates 45deg rotated. But because the screw looks "torks" type you might forget about rotation.

edit flag offensive delete link more


Thanks for pointing out the issue with clearing the score around the match. I'll look into this.

Yeah the screw's rotation is not a deal, as you said it is not of + type. However if you go for a full screw template then you miss out the half screws (due to perspective). In other words, you would have false negatives. That was the point of using half screws,

Raki gravatar imageRaki ( 2017-07-25 06:21:51 -0500 )edit

Template matching is not so good in case of occlusion but really can't take picture with (almost) frontal camera and frontal light/diffuse light/2 or 4 lights from different directions? If you can't, you might try perspective correction (since your working plane is static) even if this can't solve screw occlusion.

You might also use 2 camera from opposite direction to catch screw from both sides. I think you should try to control the environment (lights and cam position)

pklab gravatar imagepklab ( 2017-07-25 09:48:35 -0500 )edit

I mean yeah, we cannot expect template matching to "match" something that is not entirely visible, since the method was not designed for such. I get your point with occlusions. I cannot introduce another camera due to restrictions, but I can change my perspective and take a birds-eye-view in which all screws would be visible. I can conduct the matching only when the looking angle is perpendicular, this way there would be no occlusion. Thanks for the insights btw.

Raki gravatar imageRaki ( 2017-07-26 04:33:03 -0500 )edit

yeah, algorithm can be powerful but sometimes right environment settings can improve reliability and reduce complexity.... at the end vision starts with light path :)

pklab gravatar imagepklab ( 2017-07-26 05:47:49 -0500 )edit
Login/Signup to Answer

Question Tools

1 follower


Asked: 2017-07-21 03:23:50 -0500

Seen: 714 times

Last updated: Jul 25 '17