我正在尝试使用OpenCV中的kmeans算法来在HSV颜色空间中对图像进行分割。
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=2的kmeans算法对HSVImage[0]进行处理,那么我如何根据结果得到类似于阈值的图像呢?
为了澄清问题: 我有基于颜色的验证码,我想要分割出每个数字。
图片如下:
![6](https://istack.dev59.com/7zovC.webp)
![1](https://istack.dev59.com/EWP8g.webp)