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:
Thanks
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:
Start from the first pixel in the image. Assign a label to it, and then check its adjacent pixels.
If the adjacent pixels are the same color (intensity value), assign them the same label.
Continue this process, assigning new labels whenever you come across a new connected component (shape).
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.