I use colour quantization in my project:
color = color_quantization(filter, k)
def color_quantization(img, k):
# Defining input data for clustering
data = np.float32(img).reshape((-1, 3))
# Defining criteria
criteria = (cv.TERM_CRITERIA_EPS + cv.TERM_CRITERIA_MAX_ITER, 20, 1.0)
# Applying cv2.kmeans function
ret, label, center = cv.kmeans(data, k, None, criteria, 10, cv.KMEANS_RANDOM_CENTERS)
center = np.uint8(center)
result = center[label.flatten()]
result = result.reshape(img.shape)
return result
I know that k is the number of colors, but I don't know how this code works and I could find nothing on the internet. Can someone explain to me how this works?
question from:
https://stackoverflow.com/questions/65882539/python-color-quantization 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…