標籤:color block trunc val 部分 container target set truncate
cv2.threshold介紹,如所示,每個數字代表起本身的灰階值大小
cv2.threshod接收5種類參數形式,分別是cv2.THRESH_BINARY,cv2.THRESH_BINARY_INV,cv2.THRESH_TRUNC ,cv2.THRESH_TOZERO,cv2.THRESH_TOZERO_INV
(1)THRESH_BINARY(Binary Thresholding)
# Binary Threshold,原映像素>閾值,該像素點的值保持不變,否則為0.即大於閾值的部分變成像素最大值,小於閾值的部分變為0if src(x,y) > thresh dst(x,y) = maxValueelse dst(x,y) = 0
(2)THRESH_BINARY_INV(Inverse Binary Thresholding)
# Inverse Binary Threshold,原映像素<閾值,該像素點的值為0,否則保持不變.與(1)相反,大於閾值的部分變為0,小於閾值的部分變成像素最大值if src(x,y) > thresh dst(x,y) = 0else dst(x,y) = maxValue
(3)THRESH_TRUNC(Truncate Thresholding)
# Truncate Threshold,原映像素>閾值,該像素點的值等於閾值,否則保持不變.即,將所有像素變成閾值以下水平if src(x,y) > thresh dst(x,y) = threshelse dst(x,y) = src(x,y)
(4)THRESH_TOZERO(Threshold to Zero)
# Threshold to Zeroif src(x,y) > thresh dst(x,y) = src(x,y)else dst(x,y) = 0
(5)THRESH_TOZERO_INV(Inverted Threshold to Zero)
# Inverted Threshold to Zeroif src(x,y) > thresh dst(x,y) = 0else dst(x,y) = src(x,y)
以為例,python代碼為:
‘‘‘ OpenCV Threshold Example Copyright 2015 by Satya Mallick <[email protected]>‘‘‘# import opencv import cv2 # Read image src = cv2.imread("threshold.png", cv2.IMREAD_GRAYSCALE); # Basic threhold example th, dst = cv2.threshold(src, 0, 255, cv2.THRESH_BINARY); cv2.imwrite("opencv-threshold-example.jpg", dst); # Thresholding with maxValue set to 128th, dst = cv2.threshold(src, 0, 128, cv2.THRESH_BINARY); cv2.imwrite("opencv-thresh-binary-maxval.jpg", dst); # Thresholding with threshold value set 127 th, dst = cv2.threshold(src,127,255, cv2.THRESH_BINARY); cv2.imwrite("opencv-thresh-binary.jpg", dst); # Thresholding using THRESH_BINARY_INV th, dst = cv2.threshold(src,127,255, cv2.THRESH_BINARY_INV); cv2.imwrite("opencv-thresh-binary-inv.jpg", dst); # Thresholding using THRESH_TRUNC th, dst = cv2.threshold(src,127,255, cv2.THRESH_TRUNC); cv2.imwrite("opencv-thresh-trunc.jpg", dst); # Thresholding using THRESH_TOZERO th, dst = cv2.threshold(src,127,255, cv2.THRESH_TOZERO); cv2.imwrite("opencv-thresh-tozero.jpg", dst); # Thresholding using THRESH_TOZERO_INV th, dst = cv2.threshold(src,127,255, cv2.THRESH_TOZERO_INV); cv2.imwrite("opencv-thresh-to-zero-inv.jpg", dst);
結果圖:
(1) (2) (3)
(4) (5)
參考串連:
http://www.learnopencv.com/opencv-threshold-python-cpp/
http://docs.opencv.org/trunk/d7/d4d/tutorial_py_thresholding.html
python 影像處理