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

2.加载并裁剪硬币图片

代码如下(示例):

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

3.阈值分割

thresh =filters.threshold_otsu(image)

4.闭运算

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

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

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

6.连通区域标记

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

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

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

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

完整代码

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

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


总结

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

mDeepLearning4205

你的鼓励将是我创作的最大动力

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

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

打赏作者

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

抵扣说明:

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

余额充值