图像的自动阈值分割

图像阈值分割是一种广泛应用的分割技术,利用图像中要提取的目标区域与其背景在灰度特性上的差异,把图像看作具有不同灰度级的两类区域(目标区域和背景区域)的组合,选取一个比较合理的阈值,以确定图像中每个像素点应该属于目标区域还是背景区域,从而产生相应的二值图像。在skimage库中,阈值分割的功能是放在filters模块中。我们可以手动指定一个阈值,从而来实现分割。也可以让系统自动生成一个阈值,下面几种方法就是用来自动生成阈值。

一 threshold_otsu

基于Otsu的阈值分割方法,函数调用格式:

skimage.filters.threshold_otsu(imagenbins=256)

参数image是指灰度图像,返回一个阈值。

from skimage import data,filters
import matplotlib.pyplot as plt
image = data.camera()
thresh = filters.threshold_otsu(image)   #返回一个阈值
dst =(image <= thresh)*1.0   #根据阈值进行分割
plt.figure('thresh',figsize=(8,8))

plt.subplot(121)
plt.title('original image')
plt.imshow(image,plt.cm.gray)

plt.subplot(122)
plt.title('binary image')
plt.imshow(dst,plt.cm.gray)

plt.show()

结果如下图所示:

二 threshold_yen

使用方法同上:

from skimage import data,filters
import matplotlib.pyplot as plt
image = data.camera()
thresh = filters.threshold_yen(image)   #返回一个阈值
dst =(image <= thresh)*1.0   #根据阈值进行分割
plt.figure('thresh',figsize=(8,8))

plt.subplot(121)
plt.title('original image')
plt.imshow(image,plt.cm.gray)

plt.subplot(122)
plt.title('binary image')
plt.imshow(dst,plt.cm.gray)

plt.show()

结果如下图所示:

三 threshold_li

使用方法同上:

from skimage import data,filters
import matplotlib.pyplot as plt
image = data.camera()
thresh = filters.threshold_li(image)  #返回一个阈值
dst =(image <= thresh)*1.0   #根据阈值进行分割
plt.figure('thresh',figsize=(8,8))

plt.subplot(121)
plt.title('original image')
plt.imshow(image,plt.cm.gray)

plt.subplot(122)
plt.title('binary image')
plt.imshow(dst,plt.cm.gray)

plt.show()

结果如下图所示:

 

四 threshold_isodata

阈值计算方法:threshold = (image[image <= threshold].mean() +image[image > threshold].mean()) / 2.0

使用方法同上:

from skimage import data,filters
import matplotlib.pyplot as plt
image = data.camera()
thresh = filters.threshold_isodata(image) #返回一个阈值
dst =(image <= thresh)*1.0   #根据阈值进行分割
plt.figure('thresh',figsize=(8,8))

plt.subplot(121)
plt.title('original image')
plt.imshow(image,plt.cm.gray)

plt.subplot(122)
plt.title('binary image')
plt.imshow(dst,plt.cm.gray)

plt.show()

结果如下图所示:

五 threshold_adaptive

调用函数为:

skimage.filters.threshold_adaptive(imageblock_sizemethod='gaussian')

block_size: 块大小,指当前像素的相邻区域大小,一般是奇数(如3,5,7。。。)

method: 用来确定自适应阈值的方法,有'mean', 'generic', 'gaussian' 和 'median'。省略时默认为gaussian

该函数直接访问一个阈值后的图像,而不是阈值。

from skimage import data,filters
import matplotlib.pyplot as plt
image = data.camera()
dst = filters.threshold_adaptive(image, 15) #返回一个阈值图像
dst1 = filters.threshold_adaptive(image, 31,'mean') #返回一个阈值图像
dst2 = filters.threshold_adaptive(image,5,'median') #返回一个阈值图像

plt.figure('thresh',figsize=(8,8))

plt.subplot(221)
plt.title('original image')
plt.imshow(image,plt.cm.gray)

plt.subplot(222)
plt.title('binary image 15')
plt.imshow(dst,plt.cm.gray)

plt.subplot(223)
plt.title('binary image 31')
plt.imshow(dst1,plt.cm.gray)

plt.subplot(224)
plt.title('binary image 5')
plt.imshow(dst,plt.cm.gray)

plt.show()

结果如下图所示:

 

转载于:https://www.cnblogs.com/Terrypython/p/9958250.html

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值