Hysteresis 閾值分割是一種兩階段的方法,主要用於邊緣檢測。它使用兩個閾值:一個高閾值和一個低閾值。當圖像的某一部分的亮度超過高閾值時,它被視為前景;而當亮度在兩個閾值之間時,只有當這部分與已確定的前景相鄰時,它才被視為前景。這有助於確保邊緣的連續性並消除噪音。
以下是使用 Hysteresis 進行圖像閾值分割的 Python 範例:
from skimage import filters, feature
def hysteresis_thresholding(image, low=0.1, high=0.2):
"""
Segment an image using Hysteresis thresholding.
Parameters:
- image: 2D numpy array representing the grayscale image
- low: lower threshold value
- high: higher threshold value
Returns:
- segmented image
"""
# Compute the Canny edges using Hysteresis thresholding
edges = feature.canny(image, low_threshold=low, high_threshold=high)
# Convert boolean edges to binary image
binary_img = (edges * 255).astype(np.uint8)
return binary_img
# Segment the provided image using Hysteresis thresholding
hysteresis_img = hysteresis_thresholding(new_provided_img)
# Display the original and Hysteresis segmented images
fig, ax = plt.subplots(1, 2, figsize=(10, 5))
ax[0].imshow(new_provided_img, cmap='gray')
ax[0].set_title('Original Image')
ax[0].axis('off')
ax[1].imshow(hysteresis_img, cmap='gray')
ax[1].set_title('Hysteresis Thresholded Image')
ax[1].axis('off')
plt.tight_layout()
plt.show()
如上所示,左側是您提供的原始圖像,而右側是使用 Hysteresis 閾值分割得到的結果。
Hysteresis 閾值分割特別適用於邊緣檢測,它考慮了像素之間的空間關係,確保邊緣的連續性。從結果中,您可以看到這種方法是如何提取圖像中的主要邊緣的。
這種方法在消除小的噪音和保持邊緣的完整性方面表現得很好。您可以根據需要進一步調整和優化這個方法。