from math import cos, sin, atan2, sqrt, hypot, tan class TagValidator: def __init__(self, tag, px_tolerance=30): self.tag = tag self.px_tolerance = px_tolerance self.tag_coord = [] self.tag_center = self.get_center(self.tag) for j in self.tag: self.tag_coord.append(self.get_pos_to_center(j[0], j[1], tolerances=True)) def check_matches(self, matches): tolerance = 0.5 found = [] # This should be an int, but a list for diagnostic purposes # Split the coordinates into pairs for j, k in zip(matches[0::2], matches[1::2]): match_pos = self.get_pos_to_center(int(j), int(k)) for i in self.tag_coord: # This works if we allow for .5 degree tolerences if (i['theta'] - tolerance) <= match_pos['theta'] <= (i['theta'] + tolerance): # if (i['theta'] - i['tolerance_angle']) <= match_pos['theta'] <= (i['theta'] + i['tolerance_angle']): found.append((j, k)) break if len(found) == 5: print found return True return False @staticmethod def get_angle(a, b): ax, ay = a bx, by = b return atan2(by-ay, bx-ax) @staticmethod def get_center(points): x_total = 0 y_total = 0 tag_count = len(points) for t in points: x_total += t[0] y_total += t[1] return { 'x': x_total/tag_count, 'y': y_total/tag_count } # Calculate distance and angle to center def get_pos_to_center(self, x, y, tolerances=False): pos = { 'dist': hypot(self.tag_center['x'] - x, self.tag_center['y'] - y), 'theta': self.get_angle((self.tag_center['x'], self.tag_center['y']), (x, y)), 'tolerance_angle': 0 } # Attempting to exact a tolerance based on the pixel tolerance so that we can add/subtract to the key angle if tolerances: # These are yielding fractions of an angle that don't add much buffer: .0507864255156 pos['tolerance_angle'] = tan(self.px_tolerance/pos['dist']) return pos tv = TagValidator([[238, 276], [698, 324], [853, 574], [148, 1146], [786, 1215]]) print tv.check_matches([795,1411,854,568,696,328,141,1138,238,276]) # Good print tv.check_matches([733,1372,903,748,783,487,117,1209,335,358]) # Bad
Run
Reset
Share
Import
Link
Embed
Language▼
English
中文
Python Fiddle
Python Cloud IDE
Follow @python_fiddle
Browser Version Not Supported
Due to Python Fiddle's reliance on advanced JavaScript techniques, older browsers might have problems running it correctly. Please download the latest version of your favourite browser.
Chrome 10+
Firefox 4+
Safari 5+
IE 10+
Let me try anyway!
url:
Go
Python Snippet
Stackoverflow Question