图像阈值分割是一种广泛应用的分割技术,是利用图像中要提取的目标区域与其背景在灰度特性上的差异, 把图像看作具有不同灰度级的两类区域(目标区域和背景区域)的组合, 选取一个比较合理的阈值,以确定图像中每个像素点应该属于目标区域还是背景区域,从而产生相应的二值图像。
在 skimage
库中,阈值分割的功能是放在 filters
模块中。
可以手动指定一个阈值,从而来实现分割。也可以让系统自动生成一个阈值,下面几种方法就是用来自动生成阈值的。
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))
<Figure size 800x800 with 0 Axes>
<Figure size 800x800 with 0 Axes>
plt.subplot(121)
plt.title('original image')
plt.imshow(image,plt.cm.gray)
<matplotlib.image.AxesImage at 0x7efc9783d6d0>
plt.subplot(122)
plt.title('binary image')
plt.imshow(dst,plt.cm.gray)
plt.show()
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))
<Figure size 800x800 with 0 Axes>
<Figure size 800x800 with 0 Axes>
plt.subplot(121)
plt.title('original image')
plt.imshow(image,plt.cm.gray)
<matplotlib.image.AxesImage at 0x7efc8d674140>
plt.subplot(122)
plt.title('binary image')
plt.imshow(dst,plt.cm.gray)
plt.show()
thresh = filters.threshold_li(image)
dst =(image <= thresh)*1.0
plt.figure('thresh',figsize=(8,8))
<Figure size 800x800 with 0 Axes>
<Figure size 800x800 with 0 Axes>
plt.subplot(121)
plt.title('original image')
plt.imshow(image,plt.cm.gray)
<matplotlib.image.AxesImage at 0x7efc8d64a000>
plt.subplot(122)
plt.title('binary image')
plt.imshow(dst,plt.cm.gray)
plt.show()
thresh = filters.threshold_isodata(image)
dst =(image <= thresh)*1.0
plt.figure('thresh',figsize=(8,8))
<Figure size 800x800 with 0 Axes>
<Figure size 800x800 with 0 Axes>
plt.subplot(121)
plt.title('original image')
plt.imshow(image,plt.cm.gray)
<matplotlib.image.AxesImage at 0x7efc8d5b7ef0>
plt.subplot(122)
plt.title('binary image')
plt.imshow(dst,plt.cm.gray)
plt.show()
返回阈值为 87
,因此分割效果和 threshold_otsu
一样。
threshold_local
threshold_adaptive
在 0.13 版本中被弃用,并在 0.15 版本中移除,改用 threshold_local
。
用法稍有不同,需要注意。
调用函数为:
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_local(image, 15)
plt.figure('thresh',figsize=(8,8))
<Figure size 800x800 with 0 Axes>
<Figure size 800x800 with 0 Axes>
plt.subplot(121)
plt.title('original image')
plt.imshow(image,plt.cm.gray)
<matplotlib.image.AxesImage at 0x7efc8d47f3e0>
plt.subplot(122)
plt.title('gaussian usged image')
plt.imshow(dst,plt.cm.gray)
plt.show()
可以修改 block_size
的大小和 method
值来查看更多的效果。如:
dst1 =filters.threshold_local(image,31,'mean')
plt.imshow(dst1,plt.cm.gray)
plt.show()
dst2 =filters.threshold_local(image,5,'median')
plt.imshow(dst2,plt.cm.gray)
plt.show()