-
Notifications
You must be signed in to change notification settings - Fork 79
/
rectangle.py
34 lines (29 loc) · 1.15 KB
/
rectangle.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
import cv2
import math
class Rectangle(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 Rectangle(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)