目的
语义分割中,标注或由其产生的mask与原图可以正确匹配,是训练模型的前提。接下来介绍1种可视化方法,可以直观且方便的check匹配的正确性。
实现
原图(我把gt打印出来了,没影响):
mask:
代码:
imgfile = 'image.jpg' #原图路径
pngfile = 'mask.png' #mask路径
img = cv2.imread(imgfile, 1)
mask = cv2.imread(pngfile, 0)
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE) #findContours函数用于找出边界点
cv2.drawContours(img, contours, -1, (0, 0, 255), 1) ##drawContours函数用于根据边界点画出图形
img = img[:, :, ::-1]
img[..., 2] = np.where(mask == 1, 255, img[..., 2])
plt.imshow(img)
plt.show()
#save_path = 'merge.jpg'
#cv2.imwrite(save_path, img)
结果图: