我试图在二值图像中计数细丝。它们是细胞filopodia的分割掩模,并且它们经常重叠。这种重叠使得很难准确计数。
我正在尝试使用以下代码,它可以使用细化/ backbone 化和分支/端点计数,但没有运气:
def find_endpoints(img):
(rows,cols) = np.nonzero(img)
skel_coords = []
for (r,c) in zip(rows,cols):
# Extract an 8-connected neighbourhood
(col_neigh,row_neigh) = np.meshgrid(np.array([c-1,c,c+1]), np.array([r-1,r,r+1]))
# Cast to int to index into image
col_neigh = col_neigh.astype('int')
row_neigh = row_neigh.astype('int')
# Convert into a single 1D array and check for non-zero locations
pix_neighbourhood = img[row_neigh,col_neigh].ravel() != 0
# If the number of non-zero locations equals 2, add this to our list of co-ordinates
if np.sum(pix_neighbourhood) == 2:
skel_coords.append((r,c))
return len(skel_coords)
def num_filopodia_demerged(mask):
thinned = thin(mask)
img_thin_labeled = skimage.measure.label(thinned.astype(np.uint8), connectivity=2)
stats_bbox = skimage.measure.regionprops(img_thin_labeled.astype(np.uint8))
filopodia_count = 0
for i in range(0, len(stats_bbox)):
bbox = stats_bbox[i].bbox
bbox_region = img_thin_labeled[bbox[0]:bbox[2], bbox[1]:bbox[3]]
value_counts = Counter(bbox_region.flatten()).most_common()
most_frequent_value = value_counts[1][0] if len(value_counts) > 1 else value_counts[0][0]
bbox_region = (bbox_region == most_frequent_value) * 1
bbox_region_padded = np.pad(bbox_region, pad_width=4, mode='constant', constant_values=0)
n_endpoints = find_endpoints(bbox_region_padded)
filopodia_count += (n_endpoints - 1)
return max(filopodia_count, 0)
例如,以这些模式为例:
我该如何处理这个?考虑到在实际图像中这些细丝不是这样直,可能会重叠很多也在“结”模式(形状的“6”).一个例子“真实的的”面具:
1条答案
按热度按时间cczfrluj1#
HoughLines在您的情况下工作得很好。