您好,登錄后才能下訂單哦!
這篇文章主要介紹“Python+OpenCV怎么實現(xiàn)閾值分割”,在日常操作中,相信很多人在Python+OpenCV怎么實現(xiàn)閾值分割問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”Python+OpenCV怎么實現(xiàn)閾值分割”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習吧!
原圖:
整幅圖采用一個閾值,與圖片的每一個像素灰度進行比較,重新賦值;
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()
代碼如下(示例):
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)建滑動條回調(diào)函數(shù),參數(shù)thresh為滑動條對應(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ù)組,達到同時顯示多幅圖的目的 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') #新建滑動條,初始位置為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()
代碼如下(示例):
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()
代碼如下(示例):
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()
1.cv2.threshold(src, thresh, maxval, type)
參數(shù):
src:輸入的圖像
thresh:圖像分割所用的閾值(threshold value)
maxval:當閾值類型(thresholding type)采用cv2.THRESH_BINARY和cv2.THRESH_BINARY_INV時像素點被賦予的新值
type:介紹6種類型:
cv2.THRESH_BINARY(當圖像某點像素值大于thresh(閾值)時賦予maxval,反之為0。注:最常用)
cv2.THRESH_BINARY_INV(當圖像某點像素值小于thresh時賦予maxval,反之為0)
cv2.THRESH_TRUNC(當圖像某點像素值大于thresh時賦予thresh,反之不變。注:雖然maxval沒用了,但是調(diào)用函數(shù)不能省略)
cv2.THRESH_TOZERO(當圖像某點像素值小于thresh時賦予0,反之不變。注:同上)
cv2.THRESH_TOZERO_INV(當圖像某點像素值大于thresh時賦予0,反之不變。注:同上)
cv2.THRESH_OTSU(該方法自動尋找最優(yōu)閾值,并返回給retval,見下文)
返回值:
retval:設(shè)定的thresh值,或者是通過cv2.THRESH_OTSU算出的最優(yōu)閾值
dst:閾值分割后的圖像
到此,關(guān)于“Python+OpenCV怎么實現(xiàn)閾值分割”的學(xué)習就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習,快去試試吧!若想繼續(xù)學(xué)習更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。