def loop_connected_components(mask):
"""
@type mask: np.ndarray
@rtype (np.ndarray, np.ndarray, np.ndarray)
"""
c = np.array([])
init = np.array([])
fin = np.array([])
if mask.sum() > 0:
labeled = sp_image.label(mask)[0]
components = sp_image.measurements.find_objects(labeled)
c_fin = [(s[0].stop - s[0].start, s[0].stop - 1) for s in components]
if len(c_fin) > 1 and mask[0] and mask[-1]:
c_fin[0] = c_fin[0][0] + c_fin[-1][0], c_fin[0][1]
c_fin = c_fin[0:-1]
c, fin = zip(*c_fin)
c = np.array(c, dtype=int)
fin = np.array(fin, dtype=int)
init = (fin - c + 1) % mask.shape[0]
return c, init, fin
评论列表
文章目录