医学图像分割的精确度往往就体现在图像边界的分割上,网上找了很多都没有重点显示图像边缘的分割,本文对此做一个简单分享。
input_path = './dataset/output/ISIC_0009928.png'
target_path = './dataset/output/ISIC_0009928_mask.png'
input_img = cv2.imread(input_path)
img = cv2.imread(target_path)
gray = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
print(type(img),type(gray),img.shape,gray.shape)
print(img.max(),img.min(),gray.max(),gray.min())
255 0 255 0
选取全局阈值,将整幅图像分成二值图像。如果像素值大于阈值,则为其分配之歌值(可以是白色),否则为其分配另一个值(可以是黑色)。使用的函数是cv2.threshold::
ret, binary = cv2.threshold(gray,100,255,cv2.THRESH_BINARY)
print(ret, type(ret),binary.shape,type(binary))
print(binary.max(), binary.min())
100.0
255 0
contours, hierarchy = cv2.findContours(image,mode,method)
contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
print(type(contours),type(hierarchy),hierarchy.shape)
end_img = cv2.drawContours(input_img,contours,-1,(255,36,0),3)
b,g,r = cv2.split(end_img)
end_img = cv2.merge([r,g,b])
plt.figure()
plt.imshow(end_img)
# img_pil.show()
plt.show()