Pattern recognition with location feedback of missing patterns

asked 2018-11-12 03:20:47 -0500

louberry gravatar image

Hi Guys,

I am building my first app with opencv ever and try to recognize openings of defined size, arranged in known patterns on a white foil/paper. If some openings are missing I need to recognize it and need the location feedback. Openings will be black, so contrast should be of no issue.

My question: what opencv algorithm would be the best to achieve this.

Explanatory note: Since the opencv tool box is huge their might be a lot of tools that would do the job in general, but neither I want to shoot on birds with canons nor use a dull knife to cut precisely. I already tried similarity measurement as described in the official documentation, it somehow works but doesn't seem to really fit in that situation.

Thanks so much for your help and ideas!

edit retag flag offensive close merge delete


an example image, and the code you're trying with would be helpful here ;)

berak gravatar imageberak ( 2018-11-12 03:27:02 -0500 )edit

Just FYI - Here is a crosspost: [SO] (

Grillteller gravatar imageGrillteller ( 2018-11-12 03:51:02 -0500 )edit

The question is more about the general approach of such issues. There is not much code to show yet, I am still in concept phase. I am still trying to get my head around the principles of opencv. There are so many tools to choose from :)

Do you think template matching would be fine to detect multiple templates in an image and get their locations back.


@Grillteller This is my question as well, but seems like it is to specific for stackoverflow.

louberry gravatar imagelouberry ( 2018-11-12 03:52:16 -0500 )edit

I think the question is still too general. An image would be really nice here. Just some ideas here because it is not very specific. It is clear that you need a detection of all openings in a first phase (depends on the shape of these openings - you could use templates, linedetection, shapedetection....). Then you can e.g draw a rectangle around your openings and compare the results with a given (complete) shape. Then mark sections where the original image does not match your template.

Grillteller gravatar imageGrillteller ( 2018-11-12 04:43:33 -0500 )edit

Alright so I was able to fetch some sheet and take a picture: Sample with pattern This could serve as a master template and I would need to check for unopened areas on similar parts and draw some rectangle, circle, whatever around the failing area.

louberry gravatar imagelouberry ( 2018-11-12 06:35:55 -0500 )edit

You really want an answer but I won't do the complete job for you ;). I think a lot of different strategies can lead to success here. E.g. if you can always make an image without reflections you could easily 'cv::findContours' in that image (or even better a binary thresholded image so you only have your white surface - as a mask - left). In your next image (with missing holes) you can do the same and then compare the resulting contours and their positions with some kind of distance measure (e.g. Hu moments). A completely different way could be the registering of two images using a homography (since you are having planar surfaces) and tie points leading to an easy comparison of the two images.

Grillteller gravatar imageGrillteller ( 2018-11-12 07:09:10 -0500 )edit

Thanks a lot, your answer is totally fine, I didn't expected any code from you :) But now I have few new keywords I can check out. Being new to opencv I find it quite hard to figure out what is used for what and how to combine stuff. It is a little harder than just using a gui tool from one of the big players (Cognex) and click your recipe together.

louberry gravatar imagelouberry ( 2018-11-12 07:30:08 -0500 )edit