最近一直在研究图像分割。我正在使用“cv2.findcontour”函数来包含掩码图像的注释信息。蒙版的像素值有0、1、2。但是,如下图所示,存在轮廓与重叠部分分离的问题。有没有办法组合相同标签或其他东西的图像?
我使用了以下代码:
for i in tqdm(range(16,17)):
mask = train_y[i]
height = image_data[i]['height']
width = image_data[i]['width']
mask_resize = cv2.resize(mask, dsize=(width, height), interpolation=cv2.INTER_AREA)
labels = np.unique(mask_resize[mask_resize > 0])
image_id = image_data[i]['id']
plt.imshow(mask_resize)
plt.show()
for label in labels:
y = mask_resize == label
y = y.astype(np.uint8)
contours, _ = cv2.findContours(y, cv2.RETR_TREE, cv2.CHAIN_APPROX_NONE)
for i, contour in enumerate(contours):
mask_zero = np.zeros(shape=(height, width), dtype="uint8")
ctr = np.array(contour).reshape((-1,1,2)).astype(np.int32)
M = cv2.moments(ctr)
if int(M['m00']) > 1000 :
centroid_x = int(M['m10']/M['m00'])
centroid_y = int(M['m01']/M['m00'])
img = cv2.drawContours(mask_zero, [ctr], -1, (255,0,0), 5)
segmentation = np.abs(contour).flatten().tolist()
plt.imshow(img)
plt.show()