• python-opencv高级形态学处理—边缘—凸包



    前言

    图像的形态学处理有很多种,其中凸包处理是一种比较常见的高级方法,其主要原理是:生成一个凸多边形,这个凸多边形将图片中所有的白色像素点都包含在内的运算。
    Python中有相应的实现方法。
    形态学处理,除了最基本的膨胀、腐蚀、开/闭运算、黑/白帽处理外,还有一些更高级的运用,如凸包,连通区域标记,删除小块区域等。


    一、实现代码

    1.引入库

    代码如下(示例):

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.patches as mpatches
    from skimage import data,filters,segmentation,measure,morphology,color
    
    • 1
    • 2
    • 3
    • 4

    2.加载并裁剪硬币图片

    代码如下(示例):

    image = data.coins()[50:-50, 50:-50]
    
    • 1

    3.阈值分割

    thresh =filters.threshold_otsu(image)
    
    • 1

    4.闭运算

    bw =morphology.closing(image > thresh, morphology.square(3))
    
    • 1

    5.清除与边界相连的目标物

    cleared = bw.copy()  #复制
    segmentation.clear_border(cleared)
    
    • 1
    • 2

    6.连通区域标记

    label_image =measure.label(cleared)  
    borders = np.logical_xor(bw, cleared) #异或
    label_image[borders] = -1
    
    • 1
    • 2
    • 3

    7.不同标记用不同颜色显示

    image_label_overlay =color.label2rgb(label_image, image=image)
    
    • 1

    8.综合示例 阈值分割+闭运算+连通区域标记+删除小区块+分色显示

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.patches as mpatches
    from skimage import data,filter,segmentation,measure,morphology,color
    
    #加载并裁剪硬币图片
    image = data.coins()[50:-50, 50:-50]
    
    thresh =filter.threshold_otsu(image) #阈值分割
    bw =morphology.closing(image > thresh, morphology.square(3)) #闭运算
    
    cleared = bw.copy() #复制
    segmentation.clear_border(cleared) #清除与边界相连的目标物
    
    label_image =measure.label(cleared) #连通区域标记
    borders = np.logical_xor(bw, cleared) #异或
    label_image[borders] = -1
    image_label_overlay =color.label2rgb(label_image, image=image) #不同标记用不同颜色显示
    
    fig,(ax0,ax1)= plt.subplots(1,2, figsize=(8, 6))
    ax0.imshow(cleared,plt.cm.gray)
    ax1.imshow(image_label_overlay)
    
    for region in measure.regionprops(label_image): #循环得到每一个连通区域属性集
      
      #忽略小区域
      if region.area < 100:
        continue
    
      #绘制外包矩形
      minr, minc, maxr, maxc = region.bbox
      rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
                   fill=False, edgecolor='red', linewidth=2)
      ax1.add_patch(rect)
    fig.tight_layout()
    plt.show()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36

    完整代码

    import numpy as np
    import matplotlib.pyplot as plt
    import matplotlib.patches as mpatches
    from skimage import data,filters,segmentation,measure,morphology,color
    # Notes:author写错了,应该是filters而不是filter
    # https://www.cnblogs.com/denny402/p/5166258.html
    
    #加载并裁剪硬币图片
    image = data.coins()[50:-50, 50:-50]
    
    # thresh = skimage.filter.thresholding.threshold_otsu(image)
    thresh =filters.threshold_otsu(image) #阈值分割
    bw =morphology.closing(image > thresh, morphology.square(3)) #闭运算
    
    cleared = bw.copy()  #复制
    segmentation.clear_border(cleared)  #清除与边界相连的目标物
    
    label_image =measure.label(cleared)  #连通区域标记
    borders = np.logical_xor(bw, cleared) #异或
    label_image[borders] = -1
    
    
    image_label_overlay =color.label2rgb(label_image, image=image) #不同标记用不同颜色显示
    """bruce说,这是在skimage.color()模块下的结果,在color模块的颜色空间转换函数中,还有一个比较有用的函数是
    skimage.color.label2rgb(arr), 可以根据标签值对图片进行着色。以后的图片分类后着色就可以用这个函数。
    例:将lena图片分成三类,然后用默认颜色对三类进行着色	https://www.jianshu.com/p/f2e88197e81d
    """
    
    
    fig,(ax0,ax1)= plt.subplots(1,2, figsize=(8, 6))
    ax0.imshow(cleared,plt.cm.gray)
    ax1.imshow(image_label_overlay)
    
    for region in measure.regionprops(label_image): #循环得到每一个连通区域属性集
        
        #忽略小区域
        if region.area < 100:
            continue
    
        #绘制外包矩形
        minr, minc, maxr, maxc = region.bbox
        rect = mpatches.Rectangle((minc, minr), maxc - minc, maxr - minr,
                                  fill=False, edgecolor='red', linewidth=2)
        ax1.add_patch(rect)
        
    fig.tight_layout()
    plt.show()
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48

    该处使用的url网络请求的数据。


    总结

    形态学目的如下:
    图像预处理(去噪声,简化形状)
    增强物体结构(抽取骨骼,细化,粗化,凸包,物体标记)
    从背景中分隔物体
    物体量化描述(面积,周长,投影,Euler-Poincare特征)

  • 相关阅读:
    string类型常用命令与其底层数据结构
    什么是BFC,他有什么用?
    gitlab修改项目名称
    安科瑞能耗监测系统在新集卫生院综合楼、急诊楼的设计与应用
    如何用python生成动态随机验证码图片
    Oracle VM VirtualBox安装CentOS 7系统
    鉴源实验室丨汽车电子电气架构演进和域控制器发展方向
    什么是自动做市商?
    SpringBoot 27 Dubbo-admin、Zookeeper 安装
    arm服务器运行onlyoffice
  • 原文地址:https://blog.csdn.net/DeepLearning_/article/details/127875454