close

自適應閾值分割會根據圖像的局部區域計算閾值。這在圖像的亮度或對比度不均勻時特別有用。OpenCV提供了adaptiveThreshold方法來實現自適應閾值分割。

以下是使用自適應閾值進行圖像分割的範例代碼及結果:

 

# Apply adaptive thresholding
adaptive_thresh_img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, 11, 2)

# Display the original and adaptive thresholded images
fig, ax = plt.subplots(1, 2, figsize=(10, 5))

ax[0].imshow(img, cmap='gray')
ax[0].set_title('Original Image')
ax[0].axis('off')

ax[1].imshow(adaptive_thresh_img, cmap='gray')
ax[1].set_title('Adaptive Thresholded Image')
ax[1].axis('off')

plt.tight_layout()
plt.show()
上面的圖像展示了原始圖像和使用自適應閾值進行二值化後的結果。自適應閾值會根據圖像的每個像素及其鄰域計算一個閾值,這使得結果可以考慮到圖像的局部亮度變化。從結果中,您可以看到自適應閾值提供了一個對比度更強烈的二值化結果,特別是在圖像的不同亮度區域。

arrow
arrow
    文章標籤
    圖像前處理
    全站熱搜
    創作者介紹
    創作者 Rocky 的頭像
    Rocky

    Rocky的部落格

    Rocky 發表在 痞客邦 留言(0) 人氣()