close

二維閾值分割通常用於彩色圖像,它考慮像素的顏色信息進行分割。在二維閾值中,我們不僅僅基於像素的亮度,還基於其在色彩空間中的位置(例如RGB或HSV)來設定閾值。

以下是一個簡單的二維閾值分割的例子,我們會使用RGB空間的兩個通道(例如R和G)進行二維閾值。這只是一個簡單的示例,實際的二維閾值可能需要更複雜的計算和策略。

在此例中,我們將選擇R和G通道的一個閾值範圍來進行分割。

# Load the image in color
color_img = cv2.imread('/mnt/data/Camera MV-GEC2000C#C0A800C9-Snapshot-20230316161733-1979637792559_5.jpg')

# Define the threshold range for R and G channels
r_threshold = (50, 200)
g_threshold = (50, 150)

# Create a binary mask where the conditions are met
binary_mask = ((color_img[:,:,2] >= r_threshold[0]) & (color_img[:,:,2] <= r_threshold[1]) &
               (color_img[:,:,1] >= g_threshold[0]) & (color_img[:,:,1] <= g_threshold[1]))

# Convert the boolean mask to uint8
binary_mask = (binary_mask * 255).astype('uint8')

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

ax[0].imshow(cv2.cvtColor(color_img, cv2.COLOR_BGR2RGB))
ax[0].set_title('Original Image')
ax[0].axis('off')

ax[1].imshow(binary_mask, cmap='gray')
ax[1].set_title('2D Thresholded Image')
ax[1].axis('off')

plt.tight_layout()
plt.show()

如上所示,左側是原始彩色圖像,右側是使用二維閾值(基於R和G通道)進行二值化的結果。

二維閾值分割考慮到像素的顏色信息,從結果中可以看到在特定的R和G範圍內的像素被設置為白色,而其他像素則為黑色。這種方法可以用於特定顏色的區域分割,但可能需要根據圖像和應用需求進行調整。

arrow
arrow
    文章標籤
    圖像前處理
    全站熱搜

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