【问题标题】:Thinning contour lines in a binary image细化二值图像中的轮廓线
【发布时间】:2016-08-11 22:48:26
【问题描述】:

我有一个带有轮廓线的二值图像,需要净化所有不必要像素的每个轮廓线,留下一条最小连接线。

谁能给我一个源代码、代码示例或此类问题的更多信息以及在哪里寻求帮助?

【问题讨论】:

  • 我在 Google 上搜索了“图形算法细线”,并获得了相当多的点击率。你试过了吗?

标签: python image-processing binary


【解决方案1】:

实际上有一种算法称为张素细化算法。你可以在这里找到它的代码:http://rosettacode.org/wiki/Zhang-Suen_thinning_algorithm

我还用 Python 编写了一个矢量化版本,比该代码快 10 倍左右。代码如下:

def neighbours_vec(image):
    return image[2:,1:-1], image[2:,2:], image[1:-1,2:], image[:-2,2:], image[:-2,1:-1],     image[:-2,:-2], image[1:-1,:-2], image[2:,:-2]

def transitions_vec(P2, P3, P4, P5, P6, P7, P8, P9):
    return ((P3-P2) > 0).astype(int) + ((P4-P3) > 0).astype(int) + \
    ((P5-P4) > 0).astype(int) + ((P6-P5) > 0).astype(int) + \
    ((P7-P6) > 0).astype(int) + ((P8-P7) > 0).astype(int) + \
    ((P9-P8) > 0).astype(int) + ((P2-P9) > 0).astype(int)

def zhangSuen_vec(image, iterations):
    for iter in range (1, iterations):
        print iter
        # step 1    
        P2,P3,P4,P5,P6,P7,P8,P9 = neighbours_vec(image)
        condition0 = image[1:-1,1:-1]
        condition4 = P4*P6*P8
        condition3 = P2*P4*P6
        condition2 = transitions_vec(P2, P3, P4, P5, P6, P7, P8, P9) == 1
        condition1 = (2 <= P2+P3+P4+P5+P6+P7+P8+P9) * (P2+P3+P4+P5+P6+P7+P8+P9 <= 6)
        cond = (condition0 == 1) * (condition4 == 0) * (condition3 == 0) * (condition2 == 1) * (condition1 == 1)
        changing1 = numpy.where(cond == 1)
        image[changing1[0]+1,changing1[1]+1] = 0
        # step 2
        P2,P3,P4,P5,P6,P7,P8,P9 = neighbours_vec(image)
        condition0 = image[1:-1,1:-1]
        condition4 = P2*P6*P8
        condition3 = P2*P4*P8
        condition2 = transitions_vec(P2, P3, P4, P5, P6, P7, P8, P9) == 1
        condition1 = (2 <= P2+P3+P4+P5+P6+P7+P8+P9) * (P2+P3+P4+P5+P6+P7+P8+P9 <= 6)
        cond = (condition0 == 1) * (condition4 == 0) * (condition3 == 0) * (condition2 == 1) * (condition1 == 1)
        changing2 = numpy.where(cond == 1)
        image[changing2[0]+1,changing2[1]+1] = 0
    return image

【讨论】:

    【解决方案2】:

    如果您正在寻找 python 实现,请查看scikit-image

    One of their examples 本质上是您的用例。

    或者,如果你想坚持“直接”scipy,你可以通过使用连续的erosions and dilations using scipy.ndimage来做到这一点。 (正如@AxezDNyde 提到的那样。)

    编辑:链接已修复。

    【讨论】:

      【解决方案3】:

      在二值图像上结合腐蚀和膨胀(反之亦然)有助于去除椒盐样噪声,使小线条完好无损。关键字是“排序过滤器”和“形态过滤器”。

      【讨论】:

        【解决方案4】:

        在 PyPi 上有一个名为 thinning 的软件包,您可以直接 pip 安装。它实现了郭和霍尔针对凹凸数组/opencv 灰度图像的细化算法。

        【讨论】:

          猜你喜欢
          • 2011-08-18
          • 2020-03-29
          • 2021-04-25
          • 1970-01-01
          • 2021-03-08
          • 1970-01-01
          • 2016-07-11
          • 1970-01-01
          • 2015-12-12
          相关资源
          最近更新 更多