-
Notifications
You must be signed in to change notification settings - Fork 1
/
contours.py
43 lines (35 loc) · 1.24 KB
/
contours.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
import numpy as np
import cv2
class CountoursDetector:
"""
Class that packs techniques and parameters to perform contours detection
"""
def __init__(self, threshold, maxval, threshold_type):
self.threshold = threshold
self.maxval = maxval
self.threshold_type = threshold_type
def work(self, frame, mode=cv2.RETR_EXTERNAL, remove_shadows=False):
"""
Method that starts contours detection with the configured parameters
"""
if not remove_shadows:
_, thresholded = cv2.threshold(
frame, self.threshold, self.maxval, self.threshold_type
)
else:
_, thresholded = cv2.threshold(frame, 20, self.maxval, self.threshold_type)
contours, _ = cv2.findContours(
image=thresholded, mode=mode, method=cv2.CHAIN_APPROX_NONE
)
# prepare black frame
canvas = np.zeros((len(frame), len(frame[0]), 3), np.uint8)
# draw contours on the original image
cv2.drawContours(
image=canvas,
contours=contours,
contourIdx=-1,
color=(255, 255, 255),
thickness=1,
lineType=cv2.LINE_AA,
)
return canvas, contours