Python + 使用 Kmeans 的 OpenCV 颜色分割

Python + OpenCV color segmentation using Kmeans

我正在尝试应用来自 opencv 的 kmeans 以便以 HSV 颜色分割图像 space。

def leftOffset(src, p_countours):
    height, width, size = src.shape

    p_width = width/p_countours
    o_left = src[0:height, 0:p_width]

    HSV_img = cv2.cvtColor(o_left, cv2.COLOR_BGR2HSV)
    hue = HSV_img[0]
    hue = np.float32(HSV_img)

    # Define criteria = ( type, max_iter = 10 , epsilon = 1.0 )
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)

    # Set flags (Just to avoid line break in the code)
    flags = cv2.KMEANS_RANDOM_CENTERS

    # Apply KMeans
    compactness,labels,centers = cv2.kmeans(hue,2,criteria,10,flags)

    centers = np.uint8(centers)
    res = centers[labels.flatten()]
    res2 = res.reshape((hue.shape))
    cv2.imshow("o_left", hue)
    cv2.waitKey(0)

我现在可以将kmeans算法应用于K=2的HSVImage[0],如何根据结果得到像阈值一样的图像?

谢谢

澄清问题: 我有基于颜色的验证码,我想分割每个数字。

图片很像

我准备用k-means方法找出主色,然后分割里面的数字

1) 如果你只需要找到主色,为什么不找到每个颜色通道的直方图呢?找到主要频道然后使用 otsu 仅分割该频道?例如,如果我只对色调设置阈值,我可以获得不错的结果。 K-means 对于这个任务来说可能有点矫枉过正:

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

## Simple Otsu over hue
six = cv2.imread('7zovC.jpg')

##convert to hsv
hsv = cv2.cvtColor(six, cv2.COLOR_BGR2HSV)
hue = hsv[:, :, 0]

binary_img = cv2.threshold(hue, 128, 255, cv2.THRESH_OTSU)

plt.figure()
plt.imshow(binary_img*255)
plt.show()

2) 为什么不使用所有通道进行聚类而不是仅使用色调?你需要的是聚类->颜色量化这个link应该有用。这是针对 opencv 版本 > 3.0.0

注意 python 2.4.11,cv2.kmeans 的界面略有不同,您可以改用它:

def color_quantize(img, K):
    Z = img.reshape((-1, 3))

    # convert to np.float32
    Z = np.float32(Z)

    # define criteria, number of clusters(K) and apply kmeans()
    criteria = (cv2.TERM_CRITERIA_EPS + cv2.TERM_CRITERIA_MAX_ITER, 10, 1.0)
    ret, label, center = cv2.kmeans(Z, 2, criteria, 10, cv2.KMEANS_RANDOM_CENTERS)

    # Now convert back into uint8, and make original image
    center = np.uint8(center)
    res = center[label.flatten()]
    quantized_img = res.reshape((img.shape))

    label_img = label.reshape((img.shape[:2]))
    return label_img, quantized_img



six = cv2.imread('7zovC.jpg')


##convert to hsv
hsv = cv2.cvtColor(six, cv2.COLOR_BGR2HSV)

K = 2
label_img, six_q = color_quantize(hsv, K)



plt.figure()
plt.imshow(label_img)

plt.show()

我的颜色量化结果并不令人印象深刻。

我可以推荐一个传统的替代方案吗?如果你先去掉非常暗和亮的区域,你可以简单地依赖从直方图计算出的色调分量的最频繁值。

请注意,数字的边界永远不会绝对准确,因为周围的颜色相似。

此外,您可以 select 仅使用最大 blob(根据大小)来抑制外部剩余的小 blob。

结果:

代码:

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


img = cv2.imread('image1.jpg')

#get rid of very bright and very dark regions
delta=30
lower_gray = np.array([delta, delta,delta])
upper_gray = np.array([255-delta,255-delta,255-delta])
# Threshold the image to get only selected
mask = cv2.inRange(img, lower_gray, upper_gray)
# Bitwise-AND mask and original image
res = cv2.bitwise_and(img,img, mask= mask)

#Convert to HSV space
HSV_img = cv2.cvtColor(res, cv2.COLOR_BGR2HSV)
hue = HSV_img[:, :, 0]

#select maximum value of H component from histogram
hist = cv2.calcHist([hue],[0],None,[256],[0,256])
hist= hist[1:, :] #suppress black value
elem = np.argmax(hist)
print np.max(hist), np.argmax(hist)

tolerance=10
lower_gray = np.array([elem-tolerance, 0,0])
upper_gray = np.array([elem+tolerance,255,255])
# Threshold the image to get only selected
mask = cv2.inRange(HSV_img, lower_gray, upper_gray)
# Bitwise-AND mask and original image
res2 = cv2.bitwise_and(img,img, mask= mask)


titles = ['Original Image', 'Selected Gray Values', 'Hue', 'Result']
images = [img, res, hue, res2]
for i in xrange(4):
    plt.subplot(2,2,i+1),plt.imshow(images[i],'gray')
    plt.title(titles[i])
    plt.xticks([]),plt.yticks([])
plt.show()