Simple Thresholding
Threshold คือ หนึ่งในเทคนิคการคัดแยกวัตถุออกจากภาพพื้นหลัง โดยการรับภาพสีเทามาแปลงเป็นภาพไบนารีที่มีค่าพิกเซลเป็น 0 หรือ 255 ด้วยการกำหนดค่าคงที่ขั้นต่ำมาเทียบกับค่าพิกเซลในแต่ละพื้นที่ของภาพสีเทา เพื่อทำการเปลี่ยนค่าพิกเซลนั้นเป็น 0 หรือ 255
$ (T, threshImage) = cv2.threshold(image, thresh, maxval, type)
Parameters:
image => ภาพระดับสีเทาที่จะนำมาใช้ในการ Threshold
thresh => ค่าความสว่างขั้นต่ำที่นำมาใช้ในการเทียบกับค่าพิเซลใภาพระดับสีเทา
maxval => ค่าความสว่างสูงสุดที่ต้องการ
type => ประเภท Threshold
Example:
import cv2
image = cv2.imread("/path/your/image.jpg")
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
blurred = cv2.GaussianBlur(gray, (7, 7), 0)
cv2.imshow("Image", image)
(T, threshInv) = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY_INV)
cv2.imshow("Threshold Binary Inverse", threshInv)
(T, thresh) = cv2.threshold(blurred, 200, 255, cv2.THRESH_BINARY)
cv2.imshow("Threshold Binary", thresh)
cv2.imshow("Output", cv2.bitwise_and(image, image, mask=threshInv))
cv2.waitKey(0)

Last updated
Was this helpful?