OpenCV Image Threshold
The basic concept of the threshold is that more simplify the visual data for analysis. When we convert the image into gray-scale, we have to remember that grayscale still has at least 255 values. The threshold is converted everything to white or black, based on the threshold value. Let's assume we want the threshold to be 125(out of 255), then everything that was under the 125 would be converted to 0 or black, and everything above the 125 would be converted to 255, or white. The syntax is as follows:
retval,threshold = cv2.threshold(src, thresh, maxValue, cv2.THRESH_BINARY_INV)
src: Source image, it should be a grayscale image.
thresh: It is used to classify the pixel value.
maxVal: It represents the value to be given if the pixel threshold value.
OpenCV provides different styles of threshold that is used as fourth parameter of the function. These are the following:
Let's take a sample input image
We have taken above image as an input. We describe how threshold actually works. The above image is slightly dim and little bit hard to read. Some parts are light enough to read, while other part is required more focus to read properly.
Let's consider the following example:
import cv2 img = cv2.imread(r'C:\Users\DEVANSH SHARMA\book1.jpg',1) retval, threshold = cv2.threshold(img, 62, 255, cv2.THRESH_BINARY) cv2.imshow("Original Image", img) cv2.imshow("Threshold",threshold) cv2.waitKey(0)
The above program highlighted the image. Now we can easily read the content of the image.