Header Ads Widget

⚡ Premium Tools Hub • EXE Apps + Full Python Source Code
Lite • Pro • Bundle Packs • Instant Download

OpenCV Python Image Thresholding Tutorial: Simple, Adaptive & Binary Threshold Explained

OpenCV Python – Image Threshold

Image thresholding is one of the most important techniques in OpenCV used for image segmentation. It helps separate objects from the background by converting a grayscale image into a binary image (black and white).

In this tutorial, you will learn how to apply different types of thresholding using OpenCV Python.


1. What is Image Thresholding?

Thresholding is a process where pixel values are converted based on a threshold value.

  • Pixels above threshold → White (255)
  • Pixels below threshold → Black (0)

This helps in:

  • Object detection
  • Image segmentation
  • Background removal
  • Document scanning

2. Import OpenCV

import cv2

3. Read Image in Grayscale

Thresholding works best on grayscale images.

img = cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)

cv2.imshow("Original Image", img)
cv2.waitKey(0)
cv2.destroyAllWindows()

4. Simple Binary Threshold

Syntax:

cv2.threshold(source, threshold_value, max_value, type)

Example:

_, thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)

cv2.imshow("Binary Threshold", thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

5. Inverse Binary Threshold

_, thresh_inv = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY_INV)

cv2.imshow("Inverse Binary", thresh_inv)
cv2.waitKey(0)
cv2.destroyAllWindows()

6. Truncate Threshold

_, thresh_trunc = cv2.threshold(img, 127, 255, cv2.THRESH_TRUNC)

cv2.imshow("Truncate Threshold", thresh_trunc)
cv2.waitKey(0)
cv2.destroyAllWindows()

7. To Zero Threshold

_, thresh_zero = cv2.threshold(img, 127, 255, cv2.THRESH_TOZERO)

cv2.imshow("To Zero", thresh_zero)
cv2.waitKey(0)
cv2.destroyAllWindows()

8. Adaptive Thresholding

Adaptive thresholding is used when lighting conditions are not uniform.

Syntax:

cv2.adaptiveThreshold(src, maxValue, adaptiveMethod, thresholdType, blockSize, C)

Example:

adaptive = cv2.adaptiveThreshold(
img, 255,
cv2.ADAPTIVE_THRESH_MEAN_C,
cv2.THRESH_BINARY,
11, 2
)

cv2.imshow("Adaptive Threshold", adaptive)
cv2.waitKey(0)
cv2.destroyAllWindows()

9. Gaussian Adaptive Threshold

adaptive_gauss = cv2.adaptiveThreshold(
img, 255,
cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY,
11, 2
)

cv2.imshow("Gaussian Adaptive", adaptive_gauss)
cv2.waitKey(0)
cv2.destroyAllWindows()

10. Why Thresholding is Important

Thresholding is widely used in:

  • Document scanning apps
  • OCR (text recognition)
  • Medical image analysis
  • Object segmentation
  • Background removal systems

11. Common Mistakes

❌ Using color image

✔ Solution:

cv2.imread("image.jpg", cv2.IMREAD_GRAYSCALE)

❌ Wrong threshold value

✔ Solution:

  • Try values between 100–150 for best results

12. Conclusion

Image thresholding is a powerful technique in OpenCV Python for separating objects from backgrounds. It is widely used in image processing, AI, and computer vision applications.

Once you master thresholding, you can move to advanced segmentation techniques like contours and edge detection.




Post a Comment

0 Comments