【医学图像边界显示】


前言

医学图像分割的精确度往往就体现在图像边界的分割上,网上找了很多都没有重点显示图像边缘的分割,本文对此做一个简单分享。


一、读取图片

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())

<class ‘numpy.ndarray’> <class ‘numpy.ndarray’> (96, 128, 3) (96, 128)
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 <class ‘float’> (96, 128) <class ‘numpy.ndarray’>
255 0

三、轮廓检测

contours, hierarchy = cv2.findContours(image,mode,method)
  • image:输入图像
  • mode:轮廓的模式
  • method:轮廓的近似方法
  • contours:返回的轮廓
  • hierarchy:每条轮廓对应的属性
contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)  
print(type(contours),type(hierarchy),hierarchy.shape)

<class ‘tuple’> <class ‘numpy.ndarray’> (1, 1, 4)

四、绘制轮廓

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()

图片


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值