> For the complete documentation index, see [llms.txt](https://docs.aic-eec.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.aic-eec.com/artificial-intelligence-ai/basic-image-processing/computer-vision-for-python/lab-2-basic-cv/simple-thresholding.md).

# Simple Thresholding

&#x20;    Threshold คือ หนึ่งในเทคนิคการคัดแยกวัตถุออกจากภาพพื้นหลัง โดยการรับภาพสีเทามาแปลงเป็นภาพไบนารีที่มีค่าพิกเซลเป็น 0 หรือ 255 ด้วยการกำหนดค่าคงที่ขั้นต่ำมาเทียบกับค่าพิกเซลในแต่ละพื้นที่ของภาพสีเทา เพื่อทำการเปลี่ยนค่าพิกเซลนั้นเป็น 0 หรือ 255&#x20;

```
$ (T, threshImage) = cv2.threshold(image, thresh, maxval, type)
```

**Parameters:**

* image => ภาพระดับสีเทาที่จะนำมาใช้ในการ Threshold
* thresh => ค่าความสว่างขั้นต่ำที่นำมาใช้ในการเทียบกับค่าพิเซลใภาพระดับสีเทา
* maxval => ค่าความสว่างสูงสุดที่ต้องการ
* type => ประเภท Threshold&#x20;

\
**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)
```

![](https://paper-attachments.dropbox.com/s_ED8F33D726DEEB274B91070CD54DD9822C8EEE25152706EDC8932020507EB3FC_1600709144212_Screenshot+from+2020-09-22+00-25-20.png)
