Javatpoint Logo
Javatpoint Logo

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:

Parameters-

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:

  • cv2.THRESH_BINARY
  • cv2.THRESH_BINARY_INV
  • cv2.THRESH_TRUNC
  • cv2.THRESH_TOZERO
  • cv2.THRESH_TOZERO_INV

Let's take a sample input image

OpenCV Image Threshold

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:

Output:

OpenCV Image Threshold

The above program highlighted the image. Now we can easily read the content of the image.


Next TopicOpenCV Contours





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA