You can download this code by clicking the button below.
This code is now available for download.
This function reads an image from the specified path, converts it to grayscale, and then applies a binary thresholding operation to the image, setting pixels below lower_threshold and above upper_threshold to 0, and the rest to 255.
Technology Stack : OpenCV, NumPy
Code Type : Image processing
Code Difficulty : Intermediate
import cv2
import numpy as np
def threshold_image(image_path, lower_threshold, upper_threshold):
# Read the image from the given path
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# Apply thresholding to the image
_, thresholded_image = cv2.threshold(image, lower_threshold, upper_threshold, cv2.THRESH_BINARY)
return thresholded_image