[Image Segmentation] Practical Chapter (1) Traditional Image Segmentation

Cluster image segmentation

K-means clustering is a commonly used clustering algorithm that divides image pixels into K different clusters so that pixels within each cluster have similar colors or intensities. This can be used to segment objects with different colors or brightness.

import numpy as np
import matplotlib.pyplot as plt
from sklearn.cluster import KMeans
import cv2

# 读取图像
image = cv2.imread('1.png')

# 将图像像素转换为特征向量
rows, cols, channels = image.shape
features = image.reshape(rows * cols, channels)

# 使用K均值聚类
num_clusters = 2
kmeans = KMeans(n_clusters=num_clusters)
kmeans.fit(features)

# 获取聚类中心和标签
centroids = kmeans.cluster_centers_
labels = kmeans.labels_

# 重构图像
reconstructed_image = centroids[labels].reshape(rows, cols, channels)





# 显示结果
plt.figure(figsize=(12, 6))
plt.subplot(121)
plt.title('Original Image')
plt.imshow(image)
plt.axis('off')

plt.subplot(122)
plt.title('Clustered Image')
plt.imshow(reconstructed_image.astype(np.uint8))
plt.axis('off')

plt.show()

HSV space color segmentation

HSV (Hue, Saturation, Value) color space is a color representation method commonly used in image processing and computer vision. In the HSV color space, color information is divided into three components:

  1. H (hue) : Indicates the type or type of color. It measures the type of color in degrees, from 0° to 360°, corresponding to different colors such as red, green, blue, etc. For example, 0° is red, 120° is green, and 240° is blue.

  2. S (saturation) : Indicates the vividness or purity of a color. An S value of 0 represents a grayscale color, while an S value of 1 represents a fully saturated color. Saturation values ​​are between 0 and 1.

  3. V (value) : Indicates the brightness of the color. A V value of 0 represents black, and a V value of 1 represents the maximum brightness color. The lightness value is between 0 and 1.

The HSV color space is very suitable for color segmentation because it separates color information from brightness information, making color segmentation easier. When performing HSV color segmentation, you can usually select the color range of interest based on hue (H) and saturation (S) and segment it.

import cv2
import numpy as np
import matplotlib.pyplot as plt

# 读取彩色图像
image = cv2.imread('1.png')

# 将图像转换到HSV颜色空间
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# 定义亮绿色和浅绿色的HSV范围
bright_green_lower = np.array([38, 120, 100])
bright_green_upper = np.array([80, 255, 255])

pale_green_lower = np.array([35, 100, 100])
pale_green_upper = np.array([40, 255, 255])

# 创建颜色掩膜
bright_green_mask = cv2.inRange(hsv_image, bright_green_lower, bright_green_upper)
pale_green_mask = cv2.inRange(hsv_image, pale_green_lower, pale_green_upper)

# 合并掩膜,得到亮绿色和浅绿色区域
green_regions = cv2.bitwise_or(bright_green_mask, pale_green_mask)

# 提取绿色区域
green_image = cv2.bitwise_and(image, image, mask=green_regions)

# 显示结果
plt.figure(figsize=(12, 6))
plt.subplot(231)
plt.imshow(cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
plt.title('Original Image')
plt.axis('off')

plt.subplot(232)
plt.imshow(pale_green_mask, cmap='gray')
plt.title('pale Green Mask')
plt.axis('off')

plt.subplot(233)
plt.imshow(cv2.cvtColor(green_image, cv2.COLOR_BGR2RGB))
plt.title('Green Regions')
plt.axis('off')

plt.subplot(234)
plt.imshow(cv2.cvtColor(bright_green_mask, cv2.COLOR_BGR2RGB))
plt.title('Bright Green Mask')
plt.axis('off')

plt.show()

for i in range(height):
    for j in range(150):
        if(pale_green_mask[i][j]==255):
            pale_green_mask[i][j]=0
pale_green_mask

plt.figure(figsize=(12, 6))
plt.subplot(234)
plt.imshow(cv2.cvtColor(pale_green_mask, cv2.COLOR_BGR2RGB))
plt.title('Bright Green Mask')
plt.axis('off')

plt.show()

#膨胀
pale_green_mask2=pale_green_mask
kernel_size = 2
kernel = np.ones((kernel_size, kernel_size), np.uint8)

# 执行膨胀
pale_green_mask2 = cv2.dilate(pale_green_mask2, kernel, iterations=2)

plt.figure(figsize=(12, 6))
plt.subplot(234)
plt.imshow(cv2.cvtColor(pale_green_mask2, cv2.COLOR_BGR2RGB))
plt.title('Bright Green Mask')
plt.axis('off')

plt.show()

Guess you like

Origin blog.csdn.net/qq_46644680/article/details/132642867