资讯专栏INFORMATION COLUMN

python数字图像处理之图像自动阈值分割示例

89542767 / 281人阅读

  小编写这篇文章的一个主要目的,主要是利用其python数字图像技术,用来对python数字图像进行处理,主要是用来进行处理相关的阈值等一些情况,包括对其进行分割。具体的一些内容,下面就给大家详细解答下。


  序言


  什么是图像控制管理及时呢?主要是利用其图像之间的灰度进行一些相关的测试工作,根据图像灰度之间的不同程度,去进行测算一些相关情况。做这个的目的,主要是用来对其进行相关的灰度值的一些测算,从而产生一些相关的图像处理过程。


  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进行分割得下图:

01.png

  2、threshold_yen


  使用方法同上:

  thresh=filters.threshold_yen(image)


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

02.png

  3、threshold_li


  使用方法同上:

  thresh=filters.threshold_li(image)


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

03.png

  4、threshold_isodata


  阈值计算方法:


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

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

04.png

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

  

05.png

       综上所述,这篇文章就给大家介绍到这里了,希望可以给大家带来帮助。

文章版权归作者所有,未经允许请勿转载,若此文章存在违规行为,您可以联系管理员删除。

转载请注明本文地址:https://www.ucloud.cn/yun/128823.html

相关文章

发表评论

0条评论

最新活动
阅读需要支付1元查看
<