cv_connection (像halcon一样对区域进行打散)
主题:
分享一个自己用opencv写的小方法,
可以像halcon的connection算子一样,
对cv2.threshold得到的region进行打散,
并返回一个打散后的不相连的region的列表。
代码如下
def cv_connection(region):# he的拓扑信息为 后一个轮廓的索引,前一个轮廓的索引,第一个子轮廓的索引,父轮廓的索引contours0, he = cv2.findContours(region, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)father = he[..., 3][0]connection_region = []for index in range(len(contours0)):mask = np.zeros_like(region, dtype=np.uint8)if father[index] < 0: #就是-1mask0 = cv2.drawContours(mask, contours0, index, [255, 255, 255], -1)childs = np.where(father == index)[0]for child in childs:mask_child = np.zeros_like(region, dtype=np.uint8)mask_child0 = cv2.drawContours(mask_child, contours0, child, [255, 255, 255], -1)mask0 = mask0 - mask_child0connection_region.append(mask0)return connection_region