-
Notifications
You must be signed in to change notification settings - Fork 2
/
detection.py
158 lines (127 loc) · 4.17 KB
/
detection.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import numpy as np
import scipy.signal
import imageProc
class IdArray(object):
def __init__(self, n):
self._data = np.zeros(n + 2, dtype=int)
def __getitem__(self, i):
return self._data[i + 1]
def __setitem__(self, i, val):
self._data[i + 1] = val
def clear(self):
self._data[:] = 0
class Span(object):
def __init__(self, y, x0, x1=None):
self.y = y
self.x0 = x0
self.x1 = x1
class Footprint(object):
def __init__(self, fId):
self.id = fId
self.spans = []
self.centroid = None
self.npix = None
def findPeakInFootprint(image, foot):
s = foot.spans[0]
ypeak = s.y
xpeak = s.x0
max = image[s.y, s.x0]
npix = 0
for s in foot.spans:
for x in range(s.x0, s.x1):
npix += 1
val = image[s.y, x]
if val > max:
xpeak = x
ypeak = s.y
return xpeak, ypeak, npix
def findObjects(data, threshold, grow=0):
h, w = data.shape
nextId = 1
aliases = {}
spans = {}
idc, idp = [IdArray(w), IdArray(w)]
for y in range(h):
idc, idp = idp, idc
idc.clear()
drow = data[y]
for x in range(w):
if drow[x] > threshold:
if x == 0 or x > 0 and drow[x - 1] < threshold:
span = Span(y, x, w)
if idc[x - 1] > 0:
idc[x] = idc[x - 1]
elif idp[x - 1] > 0:
idc[x] = idp[x - 1]
elif idp[x] > 0:
idc[x] = idp[x]
elif idp[x + 1] > 0:
idc[x] = idp[x + 1]
else:
spans[nextId] = []
span = Span(y, x, w)
idc[x] = nextId
nextId += 1
if idp[x + 1] and idc[x] != idp[x + 1]:
aliases[idc[x]] = idp[x + 1]
else:
if idc[x - 1] > 0:
span.x1 = x
spans[idc[x - 1]].append(span)
#
# Remove cycles in the aliases -- you get this if you have objects with holes in the middle
#
for objId in spans:
resolved = set()
while objId in aliases:
nobjId = aliases[objId]
if nobjId in resolved:
del aliases[objId]
break
resolved.add(nobjId)
objId = nobjId
nextId = 1
footprints = {}
footObjIds = {} # mapping from IDs used for spans to footprint Ids
for objId, spans in spans.items():
while objId in aliases:
objId = aliases[objId]
if objId in footObjIds:
foot = footprints[footObjIds[objId]]
else:
fId = nextId; nextId += 1
footObjIds[objId] = fId
foot = Footprint(fId)
footprints[fId] = foot
foot.spans += spans
del spans
#
# Grow footprints by given number of pixels
if grow:
footprints = growFootprints(data.shape, footprints, grow)
if not grow:
for i, foot in footprints.items():
xc, yc, npix = findPeakInFootprint(data, foot)
foot.centroid = (xc, yc)
foot.npix = npix
return footprints
def growFootprints(shape, footprints, radius):
size = int(radius + 1)
x, y = np.mgrid[-size:size+1, -size:size+1]
disk = np.zeros([2*size + 1, 2*size + 1])
disk[np.hypot(x, y) <= radius] = 1
tmp = footprintToIdImage(shape, footprints)
tmp = scipy.signal.convolve(tmp, disk, mode='same')
return findObjects(tmp, 1, grow=0)
def setMaskFromFootprints(da, footprints, bitName):
bitMask = imageProc.maskPlanes[bitName]
for i, foot in footprints.items():
for span in foot.spans:
da.mask[span.y, span.x0:span.x1] |= bitMask
def footprintToIdImage(shape, footprints):
"""Return an image with the given shape, with pixels set to the Footprint IDs"""
data = np.zeros(shape)
for foot in footprints.values():
for span in foot.spans:
data[span.y, span.x0:span.x1] = foot.id
return data