forked from afikanyati/cadenCV
-
Notifications
You must be signed in to change notification settings - Fork 0
/
box.py
executable file
·46 lines (37 loc) · 1.35 KB
/
box.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
import cv2
import math
class BoundingBox(object):
def __init__(self, x, y, w, h):
self.x = x;
self.y = y;
self.w = w;
self.h = h;
self.middle = self.x + self.w/2, self.y + self.h/2
self.area = self.w * self.h
def overlap(self, other):
overlap_x = max(0, min(self.x + self.w, other.x + other.w) - max(self.x, other.x));
overlap_y = max(0, min(self.y + self.h, other.y + other.h) - max(self.y, other.y));
overlap_area = overlap_x * overlap_y
return overlap_area / self.area
def distance(self, other):
dx = self.middle[0] - other.middle[0]
dy = self.middle[1] - other.middle[1]
return math.sqrt(dx*dx + dy*dy)
def merge(self, other):
x = min(self.x, other.x)
y = min(self.y, other.y)
w = max(self.x + self.w, other.x + other.w) - x
h = max(self.y + self.h, other.y + other.h) - y
return BoundingBox(x, y, w, h)
def draw(self, img, color, thickness):
pos = ((int)(self.x), (int)(self.y))
size = ((int)(self.x + self.w), (int)(self.y + self.h))
cv2.rectangle(img, pos, size, color, thickness)
def getCorner(self):
return self.x, self.y
def getWidth(self):
return self.w
def getHeight(self):
return self.h
def getCenter(self):
return self.middle