Image Segmentation Based on Watershed Algorithm

1. Concept

        The watershed algorithm can be compared to the process of pouring water on an image. Suppose we choose some points in the image as "watershed points", each watershed point represents a color or label. Then we pour water on the image at these water points, and the water will flow from high to low until the streams of different colors come together or stop when encountering obstacles, forming the boundaries of the segmented regions. These places without water are watersheds, representing different divisional areas. In the end, we can get an image that is divided into multiple regions.

2. Example: splitting coins

        Take the example of segmenting coins to separate the coins from the background:

(1) Import numpy, cv2, matplotlib libraries, load pictures

import numpy as np
import cv2
from matplotlib import pyplot as plt

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

(2) Convert the image from color to grayscale and threshold it

ret, thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)  # 将图像分成黑白两个区域

  (3) Applying Morphological Denoising

        Morphological noise reduction is a noise removal technology based on morphological operations in digital image processing. This technology uses morphological operations (such as dilation, erosion, opening and closing operations, etc.) to process images to remove noise and improve image quality. It is used here cv2.morphologyEx()函数, as one of OpenCV's image morphology processing functions, which can perform morphological operations on images such as expansion, erosion, opening, closing, and gradient.

# Remove noise
kernel = np.noes((3, 3), np.uint8)
opening = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)

(4) Obtain the area in the image that is most certain to be the background through the expansion result of the open transformation

# finding the sure background region
sure_bg = cv2.dilate(opening, kernel, iterations=3)

(5) Get the foreground area

# find the sure foreground region
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 = sure_fg.astype(np.uint8)

(6) The middle area, that is, the uncertain or unknown area

# find the unknown region
unknown = cv2.subtract(sure_bg, sure_fg)  # 减法操作,背景减去前景

  (7) Build a "barrier" to prevent the merger of "water"

# label the foreground objects
ret, markers = cv2.connectedComponents(sure_fg)

(8) Add 1 to the labels of all regions, and 0 for unknown regions

# add one to all labels so that sure background is not 0,but 1
markers += 1
# label the unknown region as 0
markers[unknown == 255] = 0

(9) Finally, open the gate and release the water

        The cv2.watershed() function assigns the label '-1' to the pixels between the components, coloring these edges in the original image blue:

markers = cv2.watershed(img, markers)
img[markers == -1] = [255, 0, 0]

  Display the results using matplotlib:

plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))
plt.show()

Experimental results:

 3. Application Prospect of Watershed Segmentation

The following are the application prospects of the watershed segmentation algorithm in different fields:

  1. Computer Vision: Watershed segmentation algorithms can be used for tasks such as object detection, face recognition, image analysis, and image search. For example, automatic recognition and classification can be achieved by segmenting an image containing multiple objects into multiple parts, and then performing object detection and classification on each part.

  2. Image processing: Watershed segmentation algorithms can be used for tasks such as image denoising, image enhancement, and image reconstruction. For example, image denoising, enhancement, and reconstruction can be achieved by segmenting an image and then processing each region differently.

  3. Medical image analysis: Watershed segmentation algorithms can be used for tasks such as segmentation, quantitative analysis, and visualization of medical images. For example, different tissues and organs in medical images can be segmented, and then each region can be quantitatively analyzed and visualized, thereby realizing disease diagnosis and treatment.

  4. Autonomous driving and robotics: Watershed segmentation algorithms can be used for tasks such as environment perception and path planning for unmanned vehicles and robots. For example, different objects and obstacles in the environment can be segmented, and then environment perception and path planning can be performed for each area, so as to achieve safe driving and motion control.

In summary, the watershed segmentation algorithm has broad application prospects in the fields of computer vision, image processing, medical image analysis, unmanned driving and robotics. Finally, click on the opencv document: Image Segmentation Based on Watershed Algorithm to get more knowledge.

Guess you like

Origin blog.csdn.net/weixin_44686138/article/details/130257603