我想从opencv应用kmeans为了在HSV色彩空间中分割图像。Python + OpenCV颜色分割使用Kmeans
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)
我现在能够在k均值算法应用于HSVImage [0] K = 2,我怎么能得到这样的阈值的图像根据结果呢?
感谢
要澄清一个问题: 我有基于颜色的验证码,我想每一个细分的数字。
我将使用K-means法,找出主色和段内的数字。
我不能完全理解这个问题。你能否通过加入图像来澄清? – tfv
@tfv我已添加2个测试图像 – Mix
您使用的是什么版本的opencv? –