opencv
  1. opencv-image-threshold

Image Threshold - ( Drawing and Image Processing in OpenCV )

Heading h2

Syntax

cv2.threshold(src, thresh, maxval, type[, dst]) → retval, dst

Example

import cv2

img = cv2.imread('example.jpg', 0)
ret,thresh = cv2.threshold(img, 127, 255, cv2.THRESH_BINARY)

cv2.imshow('Original Image', img)
cv2.imshow('Thresholded Image', thresh)
cv2.waitKey(0)
cv2.destroyAllWindows()

Output

The output will be two images, one is the original image, and the other is the thresholded image. The thresholded image will have only two colors, black and white, depending on the threshold value.

Explanation

Thresholding is a technique used in image processing to separate objects from the background in an image. It is a simple technique where a threshold value is set, and all the pixel values above and below this threshold are set to a specific value, typically white or black.

In OpenCV, the cv2.threshold() function is used to apply thresholding to an image. It takes in the image array, threshold value, maximum value, and the type of thresholding as input. The function returns two outputs, the threshold value used, and the thresholded image.

In the above example, we load an image and apply thresholding to it using the cv2.threshold() function. We set a threshold value of 127, and the maximum value as 255. We use the cv2.THRESH_BINARY flag to indicate the type of thresholding to be applied. This flag sets all pixel values greater than the threshold value as white and all other values as black.

Use

Thresholding is used in various image processing applications such as edge detection, object identification, and feature extraction. It is also used in computer vision for applications like face recognition, object tracking, and image segmentation.

Important Points

  • Thresholding is a technique used in image processing to separate objects from the background in an image
  • The cv2.threshold() function is used in OpenCV to apply thresholding to an image
  • The function takes in the image array, threshold value, maximum value, and the type of thresholding as input
  • The function returns two outputs, the threshold value used, and the thresholded image

Summary

In conclusion, thresholding is a simple yet powerful image processing technique used to separate objects from the background in an image. The cv2.threshold() function in OpenCV can be used to apply thresholding to an image. The function takes in the image array, threshold value, maximum value, and the type of thresholding as input, and returns the thresholded image and the threshold value used. Thresholding is used in various image processing and computer vision applications like edge detection, object identification, and feature extraction.

Published on: