close

高斯混合模型 (GMM) 是一種機器學習方法,用於估計數據的概率分佈。在圖像閾值分割的上下文中,GMM 可用於將像素值的直方圖建模為多個高斯分佈的混合。當您認為圖像有多於兩個的亮度級別(例如,前景、背景和陰影等),GMM 是一個好選擇。

  1. 首先,將圖像像素值重新整形為一個列表。
  2. 使用 sklearn 的 GaussianMixture 來擬合 GMM。您可以選擇要擬合的高斯分佈數量(例如,2 表示前景和背景)。
  3. 使用擬合的模型預測每個像素的標籤。
  4. 重新整形預測的標籤,使其具有與圖像相同的形狀。
  5. 選擇一個閾值(例如,根據預測的標籤或 GMM 的平均值)將圖像二值化。

以下是如何在 Python 中使用 GMM 進行圖像閾值分割的範例:

import cv2
import numpy as np
import matplotlib.pyplot as plt
from sklearn.mixture import GaussianMixture

# Read the newly provided image
new_provided_img = cv2.imread('/mnt/data/Camera MV-GEC2000C#C0A800C9-Snapshot-20230316161733-1979637792559_5.jpg', cv2.IMREAD_GRAYSCALE)

def gmm_threshold(image, n_components=2):
    """
    Segment an image using Gaussian Mixture Model.
    
    Parameters:
    - image: 2D numpy array representing the grayscale image
    - n_components: number of Gaussian distributions to fit
    
    Returns:
    - segmented image
    """
    # Reshape the image to be a list of pixels
    pixels = image.reshape(-1, 1)
    
    # Fit the Gaussian Mixture Model
    gmm = GaussianMixture(n_components=n_components, random_state=0).fit(pixels)
    
    # Predict the labels for each pixel
    labels = gmm.predict(pixels)
    
    # Reshape the labels to have the same shape as the image
    segmented = labels.reshape(image.shape)
    
    # Convert segmented image to binary where the largest component is considered as foreground
    binary_img = np.where(segmented == segmented.max(), 255, 0).astype(np.uint8)
    
    return binary_img

# Segment the new provided image using GMM
gmm_img_new = gmm_threshold(new_provided_img)

# Display the original and GMM segmented images for the new provided image
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(gmm_img_new, cmap='gray')
ax[1].set_title('GMM Segmented Image')
ax[1].axis('off')

plt.tight_layout()
plt.show()

如上所示,左側是您提供的原始圖像,而右側是使用高斯混合模型 (GMM) 進行分割的結果。

使用 GMM 進行分割可以得到比傳統閾值方法更細緻的結果,特別是當圖像有多個亮度級別時。從結果中,您可以看到 GMM 是如何將圖像分割為前景和背景的。

這種方法的效果可能會因圖像和所選擇的 GMM 成分數量而異。您可以根據需要進一步調整和優化這個方法。

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

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