Python+OpenCV實(shí)現(xiàn)閾值分割的方法詳解
一、全局閾值
原圖:

整幅圖采用一個(gè)閾值,與圖片的每一個(gè)像素灰度進(jìn)行比較,重新賦值;
1.效果圖

2.源碼
import cv2
import matplotlib.pyplot as plt
#設(shè)定閾值
thresh=130
#載入原圖,并轉(zhuǎn)化為灰度圖像
img_original=cv2.imread(r'E:\py\python3.7\test2\test14yuzhi\cell.png',0)
img_original=cv2.resize(img_original,(0,0),fx=0.3,fy=0.3)
#采用5種閾值類型(thresholding type)分割圖像
retval1,img_binary=cv2.threshold(img_original,thresh,255,cv2.THRESH_BINARY)
retval2,img_binary_invertion=cv2.threshold(img_original,thresh,255,cv2.THRESH_BINARY_INV)
retval3,img_trunc=cv2.threshold(img_original,thresh,255,cv2.THRESH_TRUNC)
retval4,img_tozero=cv2.threshold(img_original,thresh,255,cv2.THRESH_TOZERO)
retval5,img_tozero_inversion=cv2.threshold(img_original,thresh,255,cv2.THRESH_TOZERO_INV)
#采用plt.imshow()顯示圖像
imgs=[img_original,img_binary,img_binary_invertion,img_trunc,img_tozero,img_tozero_inversion]
titles=['original','binary','binary_inv','trunc','tozero','tozero_inv']
for i in range(6):
plt.subplot(2,3,i+1)
plt.imshow(imgs[i],'gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()
二、滑動(dòng)改變閾值(滑動(dòng)條)
1.效果圖

2.源碼
代碼如下(示例):
import cv2
import numpy as np
import matplotlib.pyplot as plt
#載入原圖,轉(zhuǎn)化為灰度圖像,并通過cv2.resize()等比調(diào)整圖像大小
img_original=cv2.imread(r'E:\py\python3.7\test2\test14yuzhi\cell.png',0)
img_original=cv2.resize(img_original,(0,0),fx=0.3,fy=0.3)
#初始化閾值,定義全局變量imgs
thresh=130
imgs=0
#創(chuàng)建滑動(dòng)條回調(diào)函數(shù),參數(shù)thresh為滑動(dòng)條對應(yīng)位置的數(shù)值
def threshold_segmentation(thresh):
#采用5種閾值類型(thresholding type)分割圖像
retval1,img_binary=cv2.threshold(img_original,thresh,255,cv2.THRESH_BINARY)
retval2,img_binary_invertion=cv2.threshold(img_original,thresh,255,cv2.THRESH_BINARY_INV)
retval3,img_trunc=cv2.threshold(img_original,thresh,255,cv2.THRESH_TRUNC)
retval4,img_tozero=cv2.threshold(img_original,thresh,255,cv2.THRESH_TOZERO)
retval5,img_tozero_inversion=cv2.threshold(img_original,thresh,255,cv2.THRESH_TOZERO_INV)
#由于cv2.imshow()顯示的是多維數(shù)組(ndarray),因此我們通過np.hstack(數(shù)組水平拼接)
#和np.vstack(豎直拼接)拼接數(shù)組,達(dá)到同時(shí)顯示多幅圖的目的
img1=np.hstack([img_original,img_binary,img_binary_invertion])
img2=np.hstack([img_trunc,img_tozero,img_tozero_inversion])
global imgs
imgs=np.vstack([img1,img2])
#新建窗口
cv2.namedWindow('Images')
#新建滑動(dòng)條,初始位置為130
cv2.createTrackbar('threshold value','Images',130,255,threshold_segmentation)
#第一次調(diào)用函數(shù)
threshold_segmentation(thresh)
#顯示圖像
while(1):
cv2.imshow('Images',imgs)
if cv2.waitKey(1)==ord('q'):
break
cv2.destroyAllWindows()
三、自適應(yīng)閾值分割
1.效果圖

2.源碼
代碼如下(示例):
import cv2
import matplotlib.pyplot as plt
#載入原圖
img_original=cv2.imread(r'E:\py\python3.7\test2\test14yuzhi\cell.png',0)
#全局閾值分割
retval,img_global=cv2.threshold(img_original,130,255,cv2.THRESH_BINARY)
#自適應(yīng)閾值分割
img_ada_mean=cv2.adaptiveThreshold(img_original,255,cv2.ADAPTIVE_THRESH_MEAN_C,cv2.THRESH_BINARY,15,3)
img_ada_gaussian=cv2.adaptiveThreshold(img_original,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,15,3)
imgs=[img_original,img_global,img_ada_mean,img_ada_gaussian]
titles=['Original Image','Global Thresholding(130)','Adaptive Mean','Adaptive Guassian',]
#顯示圖片
for i in range(4):
plt.subplot(2,2,i+1)
plt.imshow(imgs[i],'gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()
3.GaussianBlur()函數(shù)去噪

代碼如下(示例):
import cv2
import matplotlib.pyplot as plt
#載入原圖
img_original=cv2.imread(r'E:\py\python3.7\test2\test14yuzhi\cell.png',0)
#高斯濾波
img_blur=cv2.GaussianBlur(img_original,(13,13),13) #根據(jù)情況修改參數(shù)
#自適應(yīng)閾值分割
img_thresh=cv2.adaptiveThreshold(img_original,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,15,3)
img_thresh_blur=cv2.adaptiveThreshold(img_blur,255,cv2.ADAPTIVE_THRESH_GAUSSIAN_C,cv2.THRESH_BINARY,15,3)
#顯示圖像
imgs=[img_thresh,img_thresh_blur]
titles=['img_thresh','img_thresh_blur']
for i in range(2):
plt.subplot(1,2,i+1)
plt.imshow(imgs[i],'gray')
plt.title(titles[i])
plt.xticks([])
plt.yticks([])
plt.show()
四、參數(shù)解釋
1.cv2.threshold(src, thresh, maxval, type)
參數(shù):
src:輸入的圖像
thresh:圖像分割所用的閾值(threshold value)
maxval:當(dāng)閾值類型(thresholding type)采用cv2.THRESH_BINARY和cv2.THRESH_BINARY_INV時(shí)像素點(diǎn)被賦予的新值
type:介紹6種類型:
cv2.THRESH_BINARY(當(dāng)圖像某點(diǎn)像素值大于thresh(閾值)時(shí)賦予maxval,反之為0。注:最常用)
cv2.THRESH_BINARY_INV(當(dāng)圖像某點(diǎn)像素值小于thresh時(shí)賦予maxval,反之為0)
cv2.THRESH_TRUNC(當(dāng)圖像某點(diǎn)像素值大于thresh時(shí)賦予thresh,反之不變。注:雖然maxval沒用了,但是調(diào)用函數(shù)不能省略)
cv2.THRESH_TOZERO(當(dāng)圖像某點(diǎn)像素值小于thresh時(shí)賦予0,反之不變。注:同上)
cv2.THRESH_TOZERO_INV(當(dāng)圖像某點(diǎn)像素值大于thresh時(shí)賦予0,反之不變。注:同上)
cv2.THRESH_OTSU(該方法自動(dòng)尋找最優(yōu)閾值,并返回給retval,見下文)
返回值:
retval:設(shè)定的thresh值,或者是通過cv2.THRESH_OTSU算出的最優(yōu)閾值
dst:閾值分割后的圖像
以上就是Python+OpenCV實(shí)現(xiàn)閾值分割的方法詳解的詳細(xì)內(nèi)容,更多關(guān)于Python OpenCV閾值分割的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
python 基于PYMYSQL使用MYSQL數(shù)據(jù)庫
這篇文章主要介紹了python 基于PYMYSQL使用MYSQL數(shù)據(jù)庫的方法,幫助大家更好的理解和使用python,感興趣的朋友可以了解下2020-12-12
Python基于動(dòng)態(tài)規(guī)劃算法解決01背包問題實(shí)例
這篇文章主要介紹了Python基于動(dòng)態(tài)規(guī)劃算法解決01背包問題,結(jié)合實(shí)例形式分析了Python動(dòng)態(tài)規(guī)劃算法解決01背包問題的原理與具體實(shí)現(xiàn)技巧,需要的朋友可以參考下2017-12-12
詳解Java中一維、二維數(shù)組在內(nèi)存中的結(jié)構(gòu)
這篇文章主要介紹了Java中一維、二維數(shù)組在內(nèi)存中的結(jié)構(gòu),本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-02-02
Django實(shí)現(xiàn)內(nèi)容緩存實(shí)例方法
在本篇文章里小編給大家整理了關(guān)于Django實(shí)現(xiàn)內(nèi)容緩存實(shí)例方法,有需要的朋友們可以跟著學(xué)習(xí)下。2020-06-06
python統(tǒng)計(jì)文章中單詞出現(xiàn)次數(shù)實(shí)例
在本篇文章里小編給大家整理的是關(guān)于python統(tǒng)計(jì)文章中單詞出現(xiàn)次數(shù)實(shí)例,需要的朋友們參考學(xué)習(xí)下。2020-02-02
使用Python快速生成chrome插件相關(guān)文件結(jié)構(gòu)
本文主要介紹了如何使用Python編寫一個(gè)程序,它允許用戶創(chuàng)建一些特定文件并將它們保存在指定的文件夾中,同時(shí)也能夠啟動(dòng)?Google?Chrome?瀏覽器并打開擴(kuò)展頁面,感興趣的可以了解一下2024-11-11

