close

Threshold函式:

ret, out = cv2.threshold(image, thresh, max, method)

主要參數設置如下:

● image: 輸入影像

● thresh: 參數為最小門檻值設置

● max: 參數為最大門檻值設置

method: 參數為使用Threshold的演算類型, 二值化有5種模式如下: 

   cv2.THRESH_BINARY

   cv2.THRESH_BINARY_INV

   cv2.THRESH_TRUNC

   cv2.THRESH_TOZERO

   cv2.THRESH_TOZERO_INV

※注意※

Threshold只能處理灰階影像

 

範例1:

import cv2

img = cv2.imread('test.jpg', 0) #讀取灰階影像
ret, out1 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)
ret, out2 = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)
ret, out3 = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)
ret, out4 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)
ret, out5 = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO_INV)
name = ['BINARY', 'BINARY_INV', 'TRUNC', 'TOZERO', 'TOZERO_INV']
images = [out1, out2, out3, out4, out5]

for i in range(len(images)):
    cv2.imwrite(name[i]+'.jpg', images[i])

 

原影像:

BINARY_結果1:

BINARY_INV_結果2:

TRUNC_結果3:

TOZERO_結果4:

TOZERO_INV_結果5:

 

※查詢其他OpenCV用法如下※

OpenCV應用總整理: 點擊我

arrow
arrow
    創作者介紹
    創作者 楓綺 的頭像
    楓綺

    K_程式人

    楓綺 發表在 痞客邦 留言(0) 人氣()