zoukankan      html  css  js  c++  java
  • python库skimage 绘制二值图像的凸壳(convex hull)

    二值图像的凸壳指的是包围输入二值图像白色区域的最小的凸多边形的像素集合。

    skimage中的函数

    from skimage.morphology import convex_hull_image
    chull = convex_hull_image(image)
    

    完整代码:

    """
    ===========
    Convex Hull
    ===========
    
    The convex hull of a binary image is the set of pixels included in the
    smallest convex polygon that surround all white pixels in the input.
    
    A good overview of the algorithm is given on `Steve Eddin's blog
    <http://blogs.mathworks.com/steve/2011/10/04/binary-image-convex-hull-algorithm-notes/>`__.
    
    """
    
    import matplotlib.pyplot as plt
    
    from skimage.morphology import convex_hull_image
    from skimage import data, img_as_float
    from skimage.util import invert
    
    # The original image is inverted as the object must be white.
    image = invert(data.horse())
    
    chull = convex_hull_image(image)
    
    fig, axes = plt.subplots(1, 2, figsize=(8, 4))
    ax = axes.ravel()
    
    ax[0].set_title('Original picture')
    ax[0].imshow(image, cmap=plt.cm.gray)
    ax[0].set_axis_off()
    
    ax[1].set_title('Transformed picture')
    ax[1].imshow(chull, cmap=plt.cm.gray)
    ax[1].set_axis_off()
    
    plt.tight_layout()
    plt.show()
    
    ######################################################################
    # We prepare a second plot to show the difference.
    #
    
    chull_diff = img_as_float(chull.copy())
    chull_diff[image] = 2
    
    fig, ax = plt.subplots()
    ax.imshow(chull_diff, cmap=plt.cm.gray)
    ax.set_title('Difference')
    plt.show()
    

    实验输出

    左图:原图;右图:原图的凸壳
    一张图中显示原图及其凸壳

  • 相关阅读:
    nodejs 教程
    文摘
    TED字幕摘抄
    Gamma函数相关matlab代码
    js以excel为模板的打印
    ASP.NET动态生成GridView的使用
    IE报错:缺少标识符、字符串或数字
    ExtJS Ext.Ajax.request最好设为同步
    javascript onclick 函数不执行
    Ext.grid.GridPanel数据转json
  • 原文地址:https://www.cnblogs.com/wojianxin/p/12653564.html
Copyright © 2011-2022 走看看