Using pattern recognition to identify random shapes but same category

Hi, I would like to use pattern recognition with live feedback from camera to identify random shapes (i.e. a line with random number of branches) that are classified as the same type (a line with branches) just like in the picture below:

I was thinking that this might be difficult to just use the standard OpenCV’s pattern recognition and it might require a little machine learning, am I right? The important questions are which addon should I use and how does this could be implemented?


Hi Kenny,

From a computer vision point of view, you should first decide what algorthms you need, before looking for a library/addon that solves your problem.

About the branches: What do you want to identfy?

  • Detecting them in the image? Position, size etc?
  • Classify them? Number of branches?

Depending on this, you may need:

  • A feature that detects the “lines”
  • A feature that counts the number of branches on each line

Once all of this is set, then you can start implementing “the” feature, etc


Hi Hennio,

Computer vision is such a widespread topic so that’s why I was assuming that anyone here who has experience in this domain would point me straight to the appropriate libraries that needed to be implemented. But yeah, I agree, algorithm first then addon.

Regarding the algorithm, I would like to do a number of things; I need to detect these random lines (thick lines, lines with branches, etc) in real-time because it will be from camera and each time the lines pop up, they are not the same graphically but they have the same properties ( for example, these lines have branches, they’re in category A meanwhile these lines are thick, means they’re in category B), just like in the picture.

Which means I need to detect the thickness, identify whether there is a branch or not and so on and so forth. But another problem that I might need to solve is that all these lines are going to intersect with one another like so:

Any idea on any algorithm that might work well for all of these tasks?

Hi Kenny,

As a first approximation I would start with detecting the lines in the image. To do so, if the background is as white as the paper, a simple thresholding should work.

This will give you a mask such as:

Then some morphological operations would work:

First fill in the holes:

Apply opening with the smallest size enough to remove the thin lines:

Probably now a Hough Transform will give you the lines…

As for the thin lines… Subscract this image from the one before applying the opening, this will give you the thin lines only, but there will be an issue in the intersection

I hope this is a good start for you.

If its not then you can still check out papers on “intersecting line segmentation” “handwritten text” “vectorization of handwritten lines” etc…

1 Like

Awesome, just what I needed to know! Thanks mate!