Python efficiently filter blobs
I'm segmenting an image and have a good threshold performed, however I need to remove areas which are too large or too small. My current implementation works, but I feel there has to be a quicker way.
def consecutive(data, stepsize=1):
return np.split(data, np.where(np.diff(data) != stepsize)[0]+1)
# Remove blobs which aren't right size
# Go through rows
for row in thresh:
ret = consecutive(np.where(row))
for line in ret:
if line.size > 200 or line.size < 30:
row[line] = 0
# Go through columns
for row in thresh.T:
ret = consecutive(np.where(row))
for line in ret:
if line.size > 200 or line.size < 30:
row[line] = 0
The general idea of this is shown below (trying to isolate the LED - the middle image is the end result). Are there any built in ways to achieve this more efficiently?