forked from stijnla/Ahold_product_detector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathopencv_helpers.py
52 lines (37 loc) · 1.89 KB
/
opencv_helpers.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
import cv2
import numpy as np
class RotatedRect():
def __init__(self, frame, center, width, height, angle, color, thickness) -> None:
self.center = center
self.width = width
self.height = height
self.angle = angle
self.determine_rect_corners()
self.frame = frame
self.color = color
self.thickness = 2
cv2.line(self.frame, self.corners[0], self.corners[1], self.color, self.thickness)
cv2.line(self.frame, self.corners[1], self.corners[2], self.color, self.thickness)
cv2.line(self.frame, self.corners[2], self.corners[3], self.color, self.thickness)
cv2.line(self.frame, self.corners[3], self.corners[0], self.color, self.thickness)
def determine_rect_corners(self):
rot_mat = np.array([[np.cos(self.angle), -np.sin(self.angle)],
[np.sin(self.angle), np.cos(self.angle)]])
w = self.width/2
h = self.height/2
non_rotated_corner_vectors = np.array([[ w, h],
[-w, h],
[-w, -h],
[ w, -h]])
rotated_corner_vectors = rot_mat @ non_rotated_corner_vectors.T
self.corners = np.array(rotated_corner_vectors.T, dtype=np.int32) + self.center
class RotatedRectCorners():
def __init__(self, frame, corners, color, thickness) -> None:
self.corners = corners
self.frame = frame
self.color = color
self.thickness = 2
cv2.line(self.frame, self.corners[0], self.corners[1], self.color, self.thickness)
cv2.line(self.frame, self.corners[1], self.corners[2], self.color, self.thickness)
cv2.line(self.frame, self.corners[2], self.corners[3], self.color, self.thickness)
cv2.line(self.frame, self.corners[3], self.corners[0], self.color, self.thickness)