python怎么实现图像自动阈值分割

发布时间:2022-06-29 09:58:02 作者:iii
来源:亿速云 阅读:319

本文小编为大家详细介绍“python怎么实现图像自动阈值分割”,内容详细,步骤清晰,细节处理妥当,希望这篇“python怎么实现图像自动阈值分割”文章能帮助大家解决疑惑,下面跟着小编的思路慢慢深入,一起来学习新知识吧。

引言

图像阈值分割是一种广泛应用的分割技术,利用图像中要提取的目标区域与其背景在灰度特性上的差异,把图像看作具有不同灰度级的两类区域(目标区域和背景区域)的组合,选取一个比较合理的阈值,以确定图像中每个像素点应该属于目标区域还是背景区域,从而产生相应的二值图像。

在skimage库中,阈值分割的功能是放在filters模块中。

我们可以手动指定一个阈值,从而来实现分割。也可以让系统自动生成一个阈值,下面几种方法就是用来自动生成阈值。

1、threshold_otsu

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

skimage.filters.threshold_otsu(image, nbins=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()

返回阈值为87,根据87进行分割得下图:

python怎么实现图像自动阈值分割

2、threshold_yen

使用方法同上:

thresh = filters.threshold_yen(image)

返回阈值为198,分割如下图:

python怎么实现图像自动阈值分割

3、threshold_li

使用方法同上:

thresh = filters.threshold_li(image)

返回阈值64.5,分割如下图:

python怎么实现图像自动阈值分割

4、threshold_isodata

阈值计算方法:

threshold = (image[image <= threshold].mean() +image[image > threshold].mean()) / 2.0

使用方法同上:

thresh = filters.threshold_isodata(image)

返回阈值为87,因此分割效果和threshold_otsu一样。

5、threshold_adaptive

调用函数为:

skimage.filters.threshold_adaptive(image, block_size, method='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) #返回一个阈值图像
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()

python怎么实现图像自动阈值分割

大家可以修改block_size的大小和method值来查看更多的效果。如:

dst1 =filters.threshold_adaptive(image,31,'mean') 
dst2 =filters.threshold_adaptive(image,5,'median')

两种效果如下:

python怎么实现图像自动阈值分割

读到这里,这篇“python怎么实现图像自动阈值分割”文章已经介绍完毕,想要掌握这篇文章的知识点还需要大家自己动手实践使用过才能领会,如果想了解更多相关内容的文章,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. python如何实现图像拼接
  2. python如何实现图像处理

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

python

上一篇:怎么用Python读取千万级数据自动写入MySQL数据库

下一篇:linux文件的大小能不能限制

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》