1. 程式人生 > >二值化的cv2 threshold函數

二值化的cv2 threshold函數

for inb binary 標準 titles ans tps word align

像素高於閾值時,給像素賦予新值,否則,賦予另外一種顏色。函數是cv2.threshold()
cv2.threshold(src,thresh,maxval,type[,dst])->retval,dst
作用:用於獲取二元值的灰度圖像
thresh:閾值,maxval:在二元閾值THRESH_BINARY和逆二元閾值THRESH_BINARY_INV中使用的最大值
返回值retval其實就是閾值

type:使用的閾值類型

例子:

#python 3.5.3  蔡軍生    
#http://edu.csdn.net/course/detail/2592    
#

import cv2
import
matplotlib.pyplot as plt img = cv2.imread(‘test1.jpg‘,0) #直接讀為灰度圖像 #二值化 ret,thresh1 = cv2.threshold(img,1,255,cv2.THRESH_BINARY) ret,thresh2 = cv2.threshold(img,127,255,cv2.THRESH_BINARY_INV) ret,thresh3 = cv2.threshold(img,127,255,cv2.THRESH_TRUNC) ret,thresh4 = cv2.threshold(img,127,255,cv2.THRESH_TOZERO) ret,thresh5 = cv2.threshold(img,127
,255,cv2.THRESH_TOZERO_INV) titles = [‘img‘,‘BINARY‘,‘BINARY_INV‘,‘TRUNC‘,‘TOZERO‘,‘TOZERO_INV‘] images = [img,thresh1,thresh2,thresh3,thresh4,thresh5] for i in range(6): plt.subplot(2,3,i+1),plt.imshow(images[i],‘gray‘) plt.title(titles[i]) plt.xticks([]),plt.yticks([]) plt.show()

輸出結果:

技術分享圖片

技術分享圖片

可見使用這個函數可以獲取輪廓圖案。

1. TensorFlow入門基本教程

http://edu.csdn.net/course/detail/4369

2. C++標準模板庫從入門到精通

http://edu.csdn.net/course/detail/3324

3.跟老菜鳥學C++

http://edu.csdn.net/course/detail/2901

4. 跟老菜鳥學python

http://edu.csdn.net/course/detail/2592

5. 在VC2015裏學會使用tinyxml庫

http://edu.csdn.net/course/detail/2590

6. 在Windows下SVN的版本管理與實戰

http://edu.csdn.net/course/detail/2579

7.Visual Studio 2015開發C++程序的基本使用

http://edu.csdn.net/course/detail/2570

8.在VC2015裏使用protobuf協議

http://edu.csdn.net/course/detail/2582

9.在VC2015裏學會使用MySQL數據庫

http://edu.csdn.net/course/detail/2672

再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://www.cnblogs.com/captainbed

二值化的cv2 threshold函數