-
Notifications
You must be signed in to change notification settings - Fork 0
/
segmentation.py
76 lines (61 loc) · 2.45 KB
/
segmentation.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
69
70
71
72
73
74
75
76
# Import python libraries
import numpy as np
import cv2
# set to 1 for pipeline images
debug = 0
class Segmentation(object):
# Segmentation class to detect objects
def __init__(self):
self.fgbg = cv2.createBackgroundSubtractorMOG2()
def detect(self, frame):
'''Detect objects in video frame using following pipeline
- Convert captured frame from BGR to GRAY
- Perform Background Subtraction
- Detect edges using Canny Edge Detection
http://docs.opencv.org/trunk/da/d22/tutorial_py_canny.html
- Retain only edges within the threshold
- Dilation for objects
- Find contours
- Find centroids for each valid contours
Args:
frame: single image frame
Return:
centers: vector of object centroids in the source frame
'''
# Convert BGR to GRAY
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Perform Background Subtraction
fgmask = self.fgbg.apply(gray)
#cv2.imshow('bgsub', fgmask)
# Detect edges
edges = cv2.Canny(fgmask, 50, 190, 3)
# Retain only edges within the threshold
ret, thresh = cv2.threshold(edges, 127, 255, 0)
cv2.imshow('thresh',thresh)
# Dilate objects
kernel = np.ones((5, 5), 'uint8')
dilated = cv2.dilate(thresh, kernel, iterations=2)
cv2.imshow('dilated',dilated)
# Find contours
contours, hierarchy = cv2.findContours(dilated,
cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
centers = [] # vector of object centroids in a frame
# setting threshold size for cells
blob_radius_thresh = 15
# Find centroid for each valid contours
for cnt in contours:
try:
# Calculate and draw circle
(x, y), radius = cv2.minEnclosingCircle(cnt)
centeroid = (int(x), int(y))
radius = int(radius)
if (radius > blob_radius_thresh):
cv2.circle(frame, centeroid, radius, (0, 255, 0), 2)
b = np.array([[x], [y]])
centers.append(np.round(b))
except ZeroDivisionError:
pass
# Show contours of tracking objects
cv2.imshow('Track Cells', frame)
return centers