python數(shù)字圖像處理之圖像自動(dòng)閾值分割示例
引言
圖像閾值分割是一種廣泛應(yīng)用的分割技術(shù),利用圖像中要提取的目標(biāo)區(qū)域與其背景在灰度特性上的差異,把圖像看作具有不同灰度級(jí)的兩類區(qū)域(目標(biāo)區(qū)域和背景區(qū)域)的組合,選取一個(gè)比較合理的閾值,以確定圖像中每個(gè)像素點(diǎn)應(yīng)該屬于目標(biāo)區(qū)域還是背景區(qū)域,從而產(chǎn)生相應(yīng)的二值圖像。
在skimage庫(kù)中,閾值分割的功能是放在filters模塊中。
我們可以手動(dòng)指定一個(gè)閾值,從而來(lái)實(shí)現(xiàn)分割。也可以讓系統(tǒng)自動(dòng)生成一個(gè)閾值,下面幾種方法就是用來(lái)自動(dòng)生成閾值。
1、threshold_otsu
基于Otsu的閾值分割方法,函數(shù)調(diào)用格式:
skimage.filters.threshold_otsu(image, nbins=256)
參數(shù)image是指灰度圖像,返回一個(gè)閾值。
from skimage import data,filters
import matplotlib.pyplot as plt
image = data.camera()
thresh = filters.threshold_otsu(image) #返回一個(gè)閾值
dst =(image <= thresh)*1.0 #根據(jù)閾值進(jìn)行分割
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,根據(jù)87進(jìn)行分割得下圖:

2、threshold_yen
使用方法同上:
thresh = filters.threshold_yen(image)
返回閾值為198,分割如下圖:

3、threshold_li
使用方法同上:
thresh = filters.threshold_li(image)
返回閾值64.5,分割如下圖:

4、threshold_isodata
閾值計(jì)算方法:
threshold = (image[image <= threshold].mean() +image[image > threshold].mean()) / 2.0
使用方法同上:
thresh = filters.threshold_isodata(image)
返回閾值為87,因此分割效果和threshold_otsu一樣。
5、threshold_adaptive
調(diào)用函數(shù)為:
skimage.filters.threshold_adaptive(image, block_size, method='gaussian')
block_size: 塊大小,指當(dāng)前像素的相鄰區(qū)域大小,一般是奇數(shù)(如3,5,7。。。)
method: 用來(lái)確定自適應(yīng)閾值的方法,有'mean', 'generic', 'gaussian' 和 'median'。
省略時(shí)默認(rèn)為gaussian
該函數(shù)直接訪問(wèn)一個(gè)閾值后的圖像,而不是閾值。
from skimage import data,filters
import matplotlib.pyplot as plt
image = data.camera()
dst =filters.threshold_adaptive(image, 15) #返回一個(gè)閾值圖像
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值來(lái)查看更多的效果。如:
dst1 =filters.threshold_adaptive(image,31,'mean') dst2 =filters.threshold_adaptive(image,5,'median')
兩種效果如下:

以上就是python數(shù)字圖像處理之圖像自動(dòng)閾值分割示例的詳細(xì)內(nèi)容,更多關(guān)于python數(shù)字圖像自動(dòng)閾值分割的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
基于scrapy實(shí)現(xiàn)的簡(jiǎn)單蜘蛛采集程序
這篇文章主要介紹了基于scrapy實(shí)現(xiàn)的簡(jiǎn)單蜘蛛采集程序,實(shí)例分析了scrapy實(shí)現(xiàn)采集程序的技巧,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-04-04
Python3轉(zhuǎn)換html到pdf的不同解決方案
今天小編就為大家分享一篇關(guān)于Python3轉(zhuǎn)換html到pdf的不同解決方案,小編覺(jué)得內(nèi)容挺不錯(cuò)的,現(xiàn)在分享給大家,具有很好的參考價(jià)值,需要的朋友一起跟隨小編來(lái)看看吧2019-03-03
Python網(wǎng)絡(luò)編程之TCP套接字簡(jiǎn)單用法示例
這篇文章主要介紹了Python網(wǎng)絡(luò)編程之TCP套接字簡(jiǎn)單用法,結(jié)合實(shí)例形式分析了TCP套接字的功能及客戶端、服務(wù)器端具體實(shí)現(xiàn)方法,需要的朋友可以參考下2018-04-04
Python?ConfigParser庫(kù)輕松讀寫(xiě)INI文件實(shí)例探究
這篇文章主要為大家介紹了Python?ConfigParser庫(kù)輕松讀寫(xiě)INI文件實(shí)例探究,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2024-01-01
Pandas中Series和DataFrame的索引實(shí)現(xiàn)
這篇文章主要介紹了Pandas中Series和DataFrame的索引實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-06-06

