def hasBird(spec, threshold=16):
#working copy
img = spec.copy()
#STEP 1: Median blur
img = cv2.medianBlur(img,5)
#STEP 2: Median threshold
col_median = np.median(img, axis=0, keepdims=True)
row_median = np.median(img, axis=1, keepdims=True)
img[img < row_median * 3] = 0
img[img < col_median * 4] = 0
img[img > 0] = 1
#STEP 3: Remove singles
img = filter_isolated_cells(img, struct=np.ones((3,3)))
#STEP 4: Morph Closing
img = cv2.morphologyEx(img, cv2.MORPH_CLOSE, np.ones((5,5), np.float32))
#STEP 5: Frequency crop
img = img[128:-16, :]
#STEP 6: Count columns and rows with signal
#(Note: We only use rows with signal as threshold, but columns might come in handy in other scenarios)
#column has signal?
col_max = np.max(img, axis=0)
col_max = ndimage.morphology.binary_dilation(col_max, iterations=2).astype(col_max.dtype)
cthresh = col_max.sum()
#row has signal?
row_max = np.max(img, axis=1)
row_max = ndimage.morphology.binary_dilation(row_max, iterations=2).astype(row_max.dtype)
rthresh = row_max.sum()
#final threshold
thresh = rthresh
#DBUGB: show?
#print thresh
#cv2.imshow('BIRD?', img)
#cv2.waitKey(-1)
#STEP 7: Apply threshold (Default = 16)
bird = True
if thresh < threshold:
bird = False
return bird, thresh
######################################################
#elist all bird species
评论列表
文章目录