-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathl2_cluster.py
67 lines (51 loc) · 2.05 KB
/
l2_cluster.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
import sys, os
import numpy as np
import cv2
from sklearn import cluster
import l0_image, l1_match, config
'''
Find the biggest cluster of matched points.
'''
def get_match_points(needle, hay):
# Group each point by its nearest mean.
points1, points2, dists = l1_match.get_match_points(needle, hay)
kmeans, biggest_group = None, []
if len(points2) > 0:
kmeans = progressive_kmeans(points2)
groups = {}
for point_index, point in enumerate(points2):
group = kmeans.predict(point)[0]
groups.setdefault(group, [])
groups[group].append((point_index, point))
biggest_group = max(groups.values(), key=lambda x: len(x))
return points1, points2, dists, kmeans, biggest_group
def progressive_kmeans(points):
# Find k-means with an increasing k until improvement slows.
prev_kmeans = None
for i in range(1, 9):
kmeans = cluster.KMeans(n_clusters=i)
kmeans.fit(points)
if kmeans.inertia_ == 0:
return kmeans
prev_inertia = prev_kmeans.inertia_ if prev_kmeans else None
improvement = None if prev_inertia is None else prev_inertia / kmeans.inertia_
if improvement is not None and improvement < 2:
break
prev_kmeans = kmeans
return prev_kmeans
def create_match_image(img1, img2, points1, points2, match_dists, ring_points):
# Draw thick rings at each cluster centroid.
canvas = l1_match.create_match_image(img1, img2, points1, points2, match_dists)
h1, w1 = img1.shape[:2]
for x, y in ring_points:
cv2.circle(canvas, (int(x + w1), int(y)), 10, (0,255,0), 4)
return canvas
if __name__ == '__main__':
# needle = l0_image.Image('stuff/needles/jb.jpg')
# hay_path = os.path.expanduser('~/Desktop/frames/out0301.png')
needle = l0_image.Image('stuff/needles/bud-light.jpeg')
hay_path = os.path.expanduser(config.get_frame('out0271.png'))
hay = l0_image.Image(hay_path)
points1, points2, sdists, kmeans, biggest_group = get_match_points(needle, hay)
canvas = create_match_image(needle.image, hay.image, points1, points2, sdists, kmeans.cluster_centers_)
l0_image.display(canvas)