Mahotas 简明教程

Mahotas - Conditional Watershed of Image

术语“分水岭”源自物理分水岭的概念,它是分隔不同流域的边界线。类似地,分水岭算法旨在寻找图像中的分界线或分离区域。

The term "watershed" is derived from the concept of a physical watershed, which is the boundary line separating different drainage basins. Similarly, the watershed algorithm aims to find boundaries or regions of separation in an image.

分水岭算法是用于图像分割的常用方法,即把图像分割成不同区域的过程。

The watershed algorithm is a popular method used for image segmentation, which is the process of dividing an image into different regions.

因此,在图像处理中,分水岭图像是指经过称为分水岭分割的处理的图像。

Therefore, in image processing, a watershed image refers to an image that has undergone a process called watershed segmentation.

分水岭分割技术将图像中的像素强度视为地形表面,其中亮区代表高程,暗区代表低程。

The watershed segmentation technique treats the pixel intensities in the image as a topographic surface, where the bright areas represent high elevations and the dark areas represent low elevations.

Watershed in Mahotas

Mahotas 提供条件分水岭函数,而不是传统的分水岭算法。

Mahotas provides the conditional watershed function instead of the traditional watershed algorithm.

Mahotas 中的条件分水岭是分水岭算法的增强版本,它允许我们通过提供特定标记来指导分割过程。

The conditional watershed in Mahotas is an enhanced version of the watershed algorithm that allows us to guide the segmentation process by providing specific markers.

让我们看一下条件分水岭算法在 Mahotas 中如何工作的逐步过程 −

Let us see the step−by−step procedure of how conditional watershed algorithm works in Mahotas −

Step 1 − 假设我们有一张图像,想将其分为不同的区域。使用条件分水岭,我们可以将图像中的某些区域标记为标记,表示我们感兴趣的区域。

Step 1 − Imagine we have an image and we want to divide it into different regions. With conditional watershed, we can mark certain areas in the image as markers that represent the regions we are interested in.

Step 2 − 然后算法开始用水填充这些标记区域。水只会流入每个标记区域内,不会越过其他标记的边界。

Step 2 − The algorithm then starts by filling these marked areas with water. The water will only flow within each marked region and won’t cross the boundaries of other markers.

Step 3 − 结果是一个分段图像,其中每个区域由您提供的标记定义的边界分隔。

Step 3 − The result is a segmented image where each region is delineated by the boundaries defined by the markers you provided.

The mahotas.cwatershed() function

Mahotas 中的 cwatershed() 函数接受两个输入− 输入图像和标记图像,并返回一个划分为不同区域的输出图像。

The cwatershed() function in Mahotas takes two inputs− the input image and a marker image, and returns an an output image which is segmented into distinct regions.

标记图像是一个二进制图像,其中前景像素(布尔值 1)表示不同区域的边界。

The marker image is a binary image where the foreground pixels (Boolean value 1) represent the boundaries for different regions.

Syntax

以下是 mahotas 中 cwatershed() 函数的基本语法 −

Following is the basic syntax of cwatershed() function in mahotas −

mahotas.cwatershed(surface, markers, Bc=None, return_lines=False) W, WL =
cwatershed(surface, markers, Bc=None, return_lines=True)

Parameters

cwatershed() 函数接受的参数如下 −

The parameters accepted by the cwatershed() function is as follows −

  1. surface − It represents the input image on which watershed segmentation will be performed. It is usually a grayscale image.

  2. markers − It represents the markers for the watershed segmentation. The markers indicate regions of interest in an image.

  3. Bc (optional) − It represents the structuring element used for neighborhood operations. If set to None, a default connectivity is used.

  4. return_lines − It is a boolean flag that specifies whether to return the watershed lines in addition to the labeled image. If True, the function returns both the labeled image and the watershed lines. If False, only the labeled image is returned. By default, it is set to False.

Return Values

  1. W or WL − It represents the labeled image obtained from the watershed segmentation, where each region is assigned a unique label. The shape of the labeled image is the same as the input image.

  2. WL (optional) − This is only returned when return_lines parameter is set to True. It represents the watershed lines, which are the boundaries between the segmented regions in the image.

Example

在以下示例中,我们尝试显示图像的基本条件分水岭分割 −

In the following example, we are trying to display the basic conditional watershed segmentation of an image −

import mahotas as mh
import matplotlib.pyplot as plt
# Loading the input image
image = mh.imread('sea.bmp')
# Creating markers or seeds
markers = mh.imread('tree.tiff')
# Perform conditional watershed segmentation
segmented_image = mh.cwatershed(image, markers)
# Display all three images in one plot
plt.figure(figsize=(10, 5))
# Display image1
plt.subplot(1, 3, 1)
plt.imshow(image)
plt.title('Sea')
plt.axis('off')
# Display image2
plt.subplot(1, 3, 2)
plt.imshow(markers)
plt.title('Tree')
plt.axis('off')
# Display the segmented image
plt.subplot(1, 3, 3)
plt.imshow(segmented_image, cmap='gray')
plt.title('Segmented Image')
plt.axis('off')
plt.tight_layout()
plt.show()

输出结果如下:

The output produced is as follows −

watershed image mohatas

Conditional Watershed with Custom Structuring Element

结构元素是一个通常表示为矩阵的小二进制图像。它用于分析参考像素的局部邻域。

A structuring element is a small binary image commonly represented as a matrix. It is used to analyze the local neighborhood of a reference pixel.

在条件分水岭的上下文中,自定义结构元素允许我们在分水岭进程期间定义像素之间的连接性。

In the context of conditional watershed, a custom structuring element allows us to define the connectivity between pixels during the watershed process.

通过自定义结构元素,我们可以控制每个像素的邻域如何影响图像的分割。

By customizing the structuring element, we can control how the the neighborhood of each pixel influences segmentation of an image.

Example

import mahotas as mh
import numpy as np
from pylab import imshow, show
# Load the image
image = mh.imread('nature.jpeg')
# Convert the image to grayscale
image_gray = mh.colors.rgb2grey(image).astype(np.uint8)
# Threshold the image
threshold = mh.thresholding.otsu(image_gray)
image_thresholded = image_gray > threshold
# Perform conditional watershed with custom structuring element
struct_element = np.array([[0, 1, 0], [1, 1, 1], [0, 1, 0]])
labels, _ = mh.label(image_thresholded, struct_element)
watershed = mh.cwatershed(image_gray.max() - image_gray, labels)
# Show the result
imshow(watershed)
show()

上述代码的输出如下:

Output of the above code is as follows −

watershed image mohatas1