-
Notifications
You must be signed in to change notification settings - Fork 0
/
target.py
68 lines (58 loc) · 1.97 KB
/
target.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
68
import cv2
from utils import (
mm_to_in
)
class Target:
def __init__(self, min_x, max_x, min_y, max_y, contours):
self.min_x = min_x
self.max_x = max_x
self.min_y = min_y
self.max_y = max_y
self.contours = contours
self.draw_len = 5
self.draw_thickness = 2
self.draw_color = (0, 0, 255)
self.avg_mm = -1
self.theta = 1000
self.theta_v = 1000
@property
def center_x(self):
return (self.min_x + self.max_x) // 2
@property
def center_y(self):
return (self.min_y + self.max_y) // 2
@property
def avg_in(self):
return mm_to_in(self.avg_mm)
@property
def avg_ft(self):
return self.avg_in / 12.
@staticmethod
def merge(target1, target2):
return Target(
min_x=min(target1.min_x, target2.min_x),
max_x=max(target1.max_x, target2.max_x),
min_y=min(target1.min_y, target2.min_y),
max_y=max(target1.max_y, target2.max_y),
contours=target1.contours + target2.contours,
)
def line(self, pt1, pt2, img):
cv2.line(img, pt1, pt2, self.draw_color, self.draw_thickness)
def draw(self, img):
LN = self.draw_len
cx = self.center_x
cy = self.center_y
min_x, max_x = self.min_x, self.max_x
min_y, max_y = self.min_y, self.max_y
# crosshairs
self.line((cx, cy-LN), (cx, cy+LN), img)
self.line((cx-LN, cy), (cx+LN, cy), img)
# corners
self.line((min_x, min_y), (min_x+LN, min_y), img)
self.line((min_x, min_y), (min_x, min_y+LN), img)
self.line((min_x, max_y), (min_x+LN, max_y), img)
self.line((min_x, max_y), (min_x, max_y-LN), img)
self.line((max_x, max_y), (max_x-LN, max_y), img)
self.line((max_x, max_y), (max_x, max_y-LN), img)
self.line((max_x, min_y), (max_x-LN, min_y), img)
self.line((max_x, min_y), (max_x, min_y+LN), img)