Python-opencv通过距离变换提取图像骨骼

距离变换

如果把二值图像理解成地形,黑色表示海洋,白色表示陆地,那么陆地上任意一点,到海洋都有一个最近的距离,如下图所示,对于左侧二值图像来说,【dist-bg】为其白色区域的骨骼;【dist-fg】为黑色区域的骨骼。

在这里插入图片描述

实现代码如下

import numpy as np
import matplotlib.pyplot as plt
from scipy.misc import ascent
import cv2

img = ascent().astype(np.uint8)

bImgs = {}
th, bImg = cv2.threshold(img, 0, 255,
    cv2.THRESH_BINARY+cv2.THRESH_OTSU)
bImgs[f'otsu({th})'] = bImg

bImgs["dist-bg"] = cv2.distanceTransform(bImg, cv2.DIST_L2,5)
bImgs["dist-fg"] = cv2.distanceTransform(255-bImg, cv2.DIST_L2,5)

for i,key in enumerate(bImgs,1):
    plt.subplot(1,3,i)
    plt.imshow(bImgs[key], cmap='gray')
    plt.title(key)
    plt.axis('off')

plt.show()

distanceTransform函数

【distanceTransform】函数的功能是,计算当前像素点到零像素点的最短距离,其输入参数有三,分别是输入的二值图像;求解距离的类型,以及掩膜尺寸,一般可设为3或者5。

在一张图像中,两点之间的距离有多种计算方式,比如

  • a a a 水平和数竖直方向的变化量
  • b b b 对角方向的变化量
  • c c c 条约移动的变化量

距离变换函数综合了这三种距离,根据各种距离的权重不同,提供了下面几种不同的距离类别

distanceTypemaskSize参数
CV_DIST_C3 ( 3 × 3 ) (3\times3) (3×3) a = 1 , b = 1 a=1, b=1 a=1,b=1
CV_DIST_L13 ( 3 × 3 ) (3\times3) (3×3) a = 1 , b = 2 a=1, b=2 a=1,b=2
CV_DIST_L23 ( 3 × 3 ) (3\times3) (3×3) a = 0.955 , b = 1.3693 a=0.955, b=1.3693 a=0.955,b=1.3693
CV_DIST_L25 ( 5 × 5 ) (5\times5) (5×5) a = 1 , b = 1.4 , c = 2.1969 a=1, b=1.4, c=2.1969 a=1,b=1.4,c=2.1969
  • 11
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

微小冷

请我喝杯咖啡

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值