def threshold_and_label_blobs(intensity, blob_list, threshold):
# we cheat a little here because we take the list of
# known blob locations to fill the thresholded areas
# surrounding the blob centers. This way false blob detections
# are avoided.
assert(intensity.dtype == np.uint8)
W, H = intensity.shape
_, blobs_mask = cv2.threshold(intensity, threshold, 1, cv2.THRESH_BINARY)
labels = 255 - blobs_mask
for num, pk in enumerate(blob_list):
iy, ix = int(pk.y), int(pk.x)
if labels[ix, iy] < 254: # was the area under the blob location already filled by another blob?
return None # overlapping non-separable blobs
if labels[ix, iy] == 255:
return None # blob center is not in thresholded area
cv2.floodFill(labels.reshape((W, H, 1)), None, (iy, ix), (num,), (0,), (0,))
labels[labels == 254] = 255 # everything not a proper blob is assigned label 255
return labels
评论列表
文章目录