Skip to content

Python-based image segmentation using morphological transformations

Comprehensive Education Platform Empowers Learners: This platform encompasses a wide range of subjects, including computer science and programming, traditional school subjects, upskilling, business, software tools, competitive exams, and more, catering to learners in various domains.

Utilizing Morphological Transformations in Python for Image Segmentation
Utilizing Morphological Transformations in Python for Image Segmentation

Python-based image segmentation using morphological transformations

In the realm of image processing, segmenting objects from a background plays a crucial role. One such example is the segmentation of coins in an image. This process is achieved using a combination of Otsu's thresholding, watershed algorithm, and morphological operations (dilation and erosion) in OpenCV.

The Process

  1. Convert the image to grayscale: This simplifies processing by reducing colors to intensity values.
  2. Apply Otsu's thresholding: This automatically finds an optimal threshold value to binarize the image, separating foreground (coins) from background. It produces a binary image where coins are white and background is black (or vice versa).
  3. Perform morphological operations:
  4. Erosion removes noise and shrinks the white regions, helping to separate objects that are close.
  5. Dilation then expands objects back to their original size and closes small holes inside the objects. These operations help clean the binary mask and make the objects more distinct.
  6. Calculate sure background and sure foreground:
  7. Use additional dilation on the binary image to get the sure background area.
  8. Use distance transform on the binary image followed by thresholding to estimate sure foreground objects (the centers of coins).
  9. Create markers for the watershed algorithm:
  10. Label the sure foreground areas with unique markers.
  11. Label the unknown region (areas where background and foreground are ambiguous) as zero.
  12. Apply the watershed algorithm: It treats the grayscale image as a topographic surface and floods basins starting from the markers. This results in boundaries (watershed lines) that separate touching or overlapping objects (coins).
  13. Mark the watershed boundaries on the original image to visually separate the coins.

A Coins Example

  • Start by loading a coins image and convert it to grayscale.
  • Use Otsu's method to automatically binarize coins vs. background.
  • Clean the binary image with erosion and dilation to reduce noise and separate joined coins.
  • Use distance transform and thresholding to find certain foreground coin centers.
  • Mark sure background and unknown regions.
  • Run watershed to delineate boundaries between touching coins.
  • The output is a segmented image where each coin is individually separated.

This approach leverages Otsu's thresholding to get a good initial mask, morphological operations to refine the mask, and watershed to precisely separate touching coins in the image.

Python Code Snippet

The following is a high-level code snippet using OpenCV in Python for coins segmentation:

```python import cv2 import numpy as np

img = cv2.imread('coins.jpg') gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)

kernel = np.ones((3,3), np.uint8) opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)

sure_bg = cv2.dilate(opening, kernel, iterations=3)

dist_transform = cv2.distanceTransform(opening, cv2.DIST_L2, 5) ret, sure_fg = cv2.threshold(dist_transform, 0.7*dist_transform.max(), 255, 0)

sure_fg = np.uint8(sure_fg) unknown = cv2.subtract(sure_bg, sure_fg)

ret, markers = cv2.connectedComponents(sure_fg)

markers = markers + 1

markers[unknown == 255] = 0

markers = cv2.watershed(img, markers)

img[markers == -1] = [0, 0, 255]

cv2.imshow('Segmented Coins', img) cv2.waitKey(0) cv2.destroyAllWindows() ```

This code demonstrates the full process mentioned above, focusing explicitly on coins segmentation using Otsu's thresholding, morphological operations (dilation and erosion as part of opening and dilation), and watershed. This sequence is widely used in OpenCV-based object segmentation tasks, especially where objects overlap or touch, such as coins on a flat surface—leveraging Otsu to binarize, morphology to clean, and watershed to separate.

In the realm of data-and-cloud-computing, scientific computing could potentially incorporate and optimize the segmentation process used in image processing. For instance, a trie data structure could be utilized to more efficiently manage the images and binarization methods such as Otsu's thresholding.

Additionally, the advancements in technology can facilitate the implementation of this segmentation process on various platforms through libraries such as OpenCV, enabling researchers to work on larger datasets effortlessly and delineate objects within images with increased precision.

Read also:

    Latest