1
$\begingroup$

I'm wondering what's an algorithm I can use to segment shapes on a binary image. Below is an example of the wanted result:

before

after

Thanks

$\endgroup$
1
  • $\begingroup$ hough transform $\endgroup$
    – Mohammad M
    Commented Jul 28, 2023 at 20:19

1 Answer 1

0
$\begingroup$

For simple shapes in a binary image, a simple and intuitive approach is to use connected component labeling (also known as blob detection). This algorithm works by scanning an image, pixel-by-pixel (from top to bottom and left to right), to identify connected pixel regions, i.e., regions of adjacent pixels which share the same set of intensity values.

Here is a simple version of the algorithm:

  1. Start from the first pixel in the image. Assign a label to it, and then check its adjacent pixels.

  2. If the adjacent pixels are the same color (intensity value), assign them the same label.

  3. Continue this process, assigning new labels whenever you come across a new connected component (shape).

  4. Once this process is complete, every different label corresponds to a different shape in the image.

However, for your specific example the connected component labeling won't directly help in this scenario as it won't distinguish between different shapes of the same color.

A more sophisticated approach could be to use edge detection (e.g., with the Sobel operator or Canny edge detector) to identify the boundaries of the shapes, and then apply a line detection algorithm (like the Hough transform) to distinguish between straight and zigzag lines. The Hough transform could be used to detect the straight line, but it's not so good for irregular shapes. For the zigzag line, you might need a more complex shape descriptor. One possible descriptor is the chain code, which records the direction of the boundary as you follow it around the shape. A straight line will have a chain code with only two directions, while a zigzag line will have many changes in direction.

You can easily find more information regarding all of these concepts on this very site using a quick search.

$\endgroup$
3
  • $\begingroup$ There is no point in using Canny or Sobel on a binary image. Edges are easily found by looking for foreground pixels with a background neighbor, for which you can write a bespoke algorithm or you can quickly implement as the difference between the image and its erosion by 1 pixel. Sobel estimates the derivative, which is not meaningful in a binary image. And Canny’s advance was to distinguish important edges from minor ones (and noise), which is not useful in a binary image. $\endgroup$ Commented Jul 28, 2023 at 22:03
  • $\begingroup$ @CrisLuengo I think that would be the case if the straight line above and the zigzag line were not exactly the same color... $\endgroup$ Commented Jul 28, 2023 at 23:41
  • $\begingroup$ Thank all of you for the insights $\endgroup$ Commented Jul 31, 2023 at 9:44

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.