@@ -17,8 +17,7 @@ def categorize(patch, label_black, label_white):
17
17
18
18
19
19
def find_stones (board ):
20
- gray = cv2 .cvtColor (board , cv2 .COLOR_BGR2GRAY )
21
- labeled_image , label_black , label_white = get_clusters (gray )
20
+ labeled_image , label_black , label_white = get_clusters (board )
22
21
height , width , _ = board .shape
23
22
dx = width // 18
24
23
dy = height // 18
@@ -55,10 +54,11 @@ def draw_patches(image, coords, color):
55
54
cv2 .rectangle (image , (left , top ), (right , bottom ), color , 2 )
56
55
57
56
58
- def get_clusters (image ):
59
- width , height = image .shape
60
- pixels = np .float32 (image .reshape ((width * height )))
57
+ def get_clusters (board ):
58
+ pixels = np .float32 (board .reshape (- 1 , 3 ))
61
59
criteria = (cv2 .TERM_CRITERIA_EPS + cv2 .TERM_CRITERIA_MAX_ITER , 200 , .1 )
62
60
flags = cv2 .KMEANS_RANDOM_CENTERS
63
- _ , labels , palette = cv2 .kmeans (pixels , 5 , None , criteria , 10 , flags )
64
- return labels .reshape (image .shape ), np .argmin (palette ), np .argmax (palette )
61
+ _ , labels , palette = cv2 .kmeans (pixels , 6 , None , criteria , 10 , flags )
62
+ width , height , _ = board .shape
63
+ means = np .mean (palette , axis = 1 )
64
+ return labels .reshape (width , height ), np .argmin (means ), np .argmax (means )
0 commit comments