Python Image Processing (11): k-means
K-means is a classic clustering algorithm. We try to use it in python.
First, the random coordinate values of 10 points are generated at the centers (-1.5,-1.5) and (1.5, 1.5). We hope to use the K-means algorithm to classify them correctly.
# Create a test data point. There are two types. # Use (-1.5,-1.5) as the center rand1 = np. ones (10, 2) * (-2) + np. random. rand (10, 2) print (rand1) # centered on (1.5, 1.5) rand2 = np. ones (10, 2) + np. random. rand (10, 2) print (rand2) # merge random vertex data = np. vstack (rand1, rand2 ))
Next, kmeans will appear.
The python prototype of this function is:
In [14]: help (cv2.kmeans)
Help on built-in function kmeans:
Kmeans (...)
Kmeans (data, K, bestLabels, criteria, attempts, flags [, centers])-> retval, bestLabels, centers
Call it in our script:
# kmeans(ret, label, center) = cv2.kmeans(data, 2, None, (cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_MAX_ITER, 10, 0.1), 10, cv2.KMEANS_RANDOM_CENTERS )
Through this function, we get the center of two classes and the category of each vertex.
Display by category:
# Display idx = np by label. hstack (label, label) for I in range (0, 2): type_data = data [idx = I] type_data = np. reshape (type_data, (type_data.shape [0]/2, 2) plt. plot (type_data [:, 0], type_data [:, 1], 'O') plt. show ()
A good result: