Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- from cv2_45 import cv2
- import numpy as np
- points = [(522, 114),
- (523, 114),
- (524, 114),
- (522, 115),
- (523, 115),
- (524, 115),
- (522, 116),
- (523, 116),
- (524, 116),
- (523, 117),
- (191, 178),
- (192, 178),
- (193, 178)]
- def partition(items, predicate):
- N = len(items)
- # // The first O(N) pass: create N single-vertex trees
- parents = [-1] * N
- ranks = [0] * N
- # The main O(N^2) pass: merge connected components
- for i in range(N):
- # Find root
- root = i
- while parents[root] >= 0:
- root = parents[root]
- for j in range(N):
- if i == j or not predicate(items[i], items[j]):
- continue
- root2 = j
- while parents[root2] >= 0:
- root2 = parents[root2]
- if root != root2:
- # Unite both trees
- rank, rank2 = ranks[root], ranks[root2]
- if rank > rank2:
- parents[root2] = root
- else:
- parents[root] = root2
- ranks[root2] += 1 if rank == rank2 else 0
- root = root2
- assert parents[root] < 0
- # compress the path from node2 to root
- k = j
- while True:
- parent = parents[k]
- if parent < 0:
- break
- parents[k] = root
- k = parent
- #compress the path from node to root
- k = i
- while True:
- parent = parents[k]
- if parent < 0:
- break
- parents[k] = root
- k = parent
- # Final O(N) pass: enumerate classes
- labels = [0] * N
- nclasses = 0
- for i in range(N):
- root = i
- while parents[root] >= 0:
- root = parents[root]
- # re-use the rank as the class label
- if ranks[root] >= 0:
- ranks[root] = ~nclasses
- nclasses += 1
- labels[i] = ~ranks[root]
- return nclasses, labels
- def predicate(point1, point2):
- threshold = 10
- return (abs(point1[0] - point2[0]) < threshold) and (abs(point1[1] - point2[1]) < threshold)
- print(partition(points, predicate))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement