Python学习11_图像自动阈值分割
2017-07-25 13:54
393 查看
转自:http://www.cnblogs.com/denny402/p/5131004.html
图像阈值分割是一种广泛应用的分割技术,利用图像中要提取的目标区域与其背景在灰度特性上的差异,把图像看作具有不同灰度级的两类区域(目标区域和背景区域)的组合,选取一个比较合理的阈值,以确定图像中每个像素点应该属于目标区域还是背景区域,从而产生相应的二值图像。
在skimage库中,阈值分割的功能是放在filters模块中。
我们可以手动指定一个阈值,从而来实现分割。也可以让系统自动生成一个阈值,下面几种方法就是用来自动生成阈值。
1、threshold_otsu
基于Otsu的阈值分割方法,函数调用格式:
skimage.filters.threshold_otsu(image, nbins=256)
参数image是指灰度图像,返回一个阈值。
返回阈值为87,根据87进行分割得下图:
![](http://images2015.cnblogs.com/blog/140867/201601/140867-20160114171601053-1017024447.png)
2、threshold_yen
使用方法同上:
返回阈值为198,分割如下图:
![](http://images2015.cnblogs.com/blog/140867/201601/140867-20160114171909460-229103995.png)
3、threshold_li
使用方法同上:
返回阈值64.5,分割如下图:
![](http://images2015.cnblogs.com/blog/140867/201601/140867-20160114172121038-976915406.png)
4、threshold_isodata
阈值计算方法:
threshold = (image[image <= threshold].mean() +image[image > threshold].mean()) / 2.0
使用方法同上:
返回阈值为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
该函数直接访问一个阈值后的图像,而不是阈值。
![](http://images2015.cnblogs.com/blog/140867/201601/140867-20160114174026069-1947238729.png)
大家可以修改block_size的大小和method值来查看更多的效果。如:
两种效果如下:
图像阈值分割是一种广泛应用的分割技术,利用图像中要提取的目标区域与其背景在灰度特性上的差异,把图像看作具有不同灰度级的两类区域(目标区域和背景区域)的组合,选取一个比较合理的阈值,以确定图像中每个像素点应该属于目标区域还是背景区域,从而产生相应的二值图像。
在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进行分割得下图:
![](http://images2015.cnblogs.com/blog/140867/201601/140867-20160114171601053-1017024447.png)
2、threshold_yen
使用方法同上:
thresh = filters.threshold_yen(image)
返回阈值为198,分割如下图:
![](http://images2015.cnblogs.com/blog/140867/201601/140867-20160114171909460-229103995.png)
3、threshold_li
使用方法同上:
thresh = filters.threshold_li(image)
返回阈值64.5,分割如下图:
![](http://images2015.cnblogs.com/blog/140867/201601/140867-20160114172121038-976915406.png)
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()
![](http://images2015.cnblogs.com/blog/140867/201601/140867-20160114174026069-1947238729.png)
大家可以修改block_size的大小和method值来查看更多的效果。如:
dst1 =filters.threshold_adaptive(image,31,'mean') dst2 =filters.threshold_adaptive(image,5,'median')
两种效果如下:
![](http://images2015.cnblogs.com/blog/140867/201601/140867-20160114174317553-483158220.png)
相关文章推荐
- python数字图像处理(11):图像自动阈值分割
- Python OpenCV学习笔记之:使用Grabcut算法进行图像背景和前景分割
- Python OpenCV学习笔记之:分水岭算法分割图像
- python OpenCV学习笔记(二十九):图像流域(分水岭)分割算法
- Google地图发布基于python的神经网络学习模型,自动高效高准确度的从带有地理定位信息的图像文件中提取信息
- 图像分割中阈值的自动选取的研究及其算法实现
- Python OpenCV学习笔记之:图像阈值操作
- Python机器学习应用 | 基于聚类的“图像分割”
- halcon学习之自动全局阈值与动态阈值分割方法
- opencv for python (11) 图像阈值 及自适应阈值
- python数字图像处理(11):图像自动阈值分割
- 基于梯度调整的矩不变自动阈值图像分割算法
- python学习笔记之11:图像用户界面
- 图像分割中阈值的自动选取的研究及其算法实现
- Halcon视频学习2.2---图像分割之阈值
- 图像分割—基于图像数据的自动选择阈值(基本全局阈值处理方法)
- OpenCV 2 学习笔记(25): 使用分水岭分割图像
- 莫烦 python Pandas 学习笔记 2017.12.7.11
- python学习之python中使用tab自动补齐
- 基础学习笔记之opencv(11):图像混合