# Extracting intersecting objects of the same colour from an image

What is the good way to extract the objects(circles) from image as shown in picture below?

respectively create two images one with lines and one with circles from source image.

I started with detecting contours in image to filter everything that may be circle from image which gives me results like this:

I'm new to computer vision and dealing with this specific task, so want thank you for any help and guidance.

Define "best"

a good way to do it

Which techniques do you know that have something like that?

Today I looked at template matching, created a template for the black and white circle and localised them in an original image. Once I have this extracting lines is straightforward (just subtracting circles from the original image). This seems working well in the simplest scenario when circles are the same size as templates.

The more robust way I think will be to use Haar Cascade classifier, train it on different size circles, and continue as with results from template matching.

Correct me if there maybe a better way?

The main idea is to digitalize diagrams consisting of black and white circles connected by lines, The size of diagrams and quality of images may vary. Also, the circles may contain numbers (Extracting those is something for future)

The lines connecting circles are axis aligned, they define position of circles in diagram (similar to cartesian plane)

I mentioned Python (js) and OpenCV languages in case there are some specific implementation details I should be aware of. I'm also learning Python while doing this :).

I don't expect the solution or code, rather pushing to right direction and strategy to deal with this kind of problems.

• Define "best". It's clear that you'll have to be able to tell the algorithm what kind of shape you're looking for. Which techniques do you know that have something like that? Alternatively, you could do some tailored post-processing on the contour data; what have you tried in this direction? – Raphael Aug 31 '16 at 13:05
• what does python( javascript) mean ? – atakanyenel Aug 31 '16 at 14:44
• Welcome to CS.SE! I don't understand what your question is. You start by asking how to extract circles, but then it becomes clear that you already know how to do that. Your results look great. Your title suggests you want to know how to detect the intersection between a circle and a line, but isn't that straightforward? Why are you having difficulty with that? What are you stuck on? Also, your title doesn't match the body of your post. Please edit the question to clarify what you are asking. – D.W. Aug 31 '16 at 16:45
• Note that coding/programming questions are off-topic here, so "how do I do this in Javascript/Python/OpenCV?" is off-topic. However, questions about algorithms and techniques are fine. – D.W. Aug 31 '16 at 16:45
• "a good way to do it" -- sorry, is that supposed to be funny? You can't define "best" by saying "good". Okay, deep breaths. There are many metrics you may conceivably want to optimize for: runing time, memory consumption, accuracy, recall, range of admissible inputs, ... -- and these are usually opposing each other. So, which are important to you? – Raphael Aug 31 '16 at 21:46

First, I would suggest you to take a look at morphological operations on images, especially dilation and erosion. Dilation grows the boundary of foreground pixels (gets rid of holes), and erosion shrinks the boundary of foreground pixels (gets rid of bridges). In your case, erosion with an appropriate kernel might get rid of those unwanted lines. Then you apply dilation with the same kernel to preserve the circles. The two of them applied together in this order is called opening. Those functions are predefined in OpenCV, check out morphologyEx function.