cv2 threshold method implementation in Python : In 3 Steps Only

cv2 threshold method implementation in Python

OpenCV has many methods that can easily allow you to manipulate images and videos. The cv2.threshold() method is one of them. In this entire tutorial, you will know to implement the cv2 threshold method in python with steps.

What is the threshold in OpenCV?

Many new beginners are unable to understand what the is threshold. I have come with a simple explanation. It is a technique in OpenCV that allows you to change the pixel values with the provided threshold. The pixel value will be set to 0 If any value is below the threshold. And if the pixel value is above the threshold then it is set to the maximum pixel value that is 255.

Steps to Implement cv2 threshold in python

Step 1: Import the necessary library

The first and most basic step is to import the required library. Import OpenCV using the import statement.

import cv2

Step 2: Read the image

For properly implementing the cv2 threshold on the image,  you have to convert the color image into the grey image. You can also directly read the color image to a grey image by passing 0 inside the cv2.imread() method.  Let’s read the image. I am reading the following image.

heart
heart image

 

img = cv2.imread("heart.png",0)

Step 3: Implement the cv2 threshold method

Before applying the method, let’s learn the syntax of the threshold() method.

cv2.threshold(input_image, threshold_value, max_value, thresholding_technique)

The method accepts four parameters. The first (input_image)  is the path of your input image. The second parameter is the threshold value. The third parameter is max_value, it is the maximum value to be assigned to the pixel. The last parameter is the threshold technique and it is of four types.

cv2.THRESH_BINARY: If the pixel value is greater than the threshold, then it set the value to 255, otherwise 0.

cv2.THRESH_BINARY_INV: It is just the opposite of the above.

cv2.THRESH_TRUNC: If the pixel value is greater than the threshold then the pixel value is set to the threshold value. Other values will be the same.

cv2.THRESH_TOZERO: The pixel value is set to zero if the value is less than the threshold value. 

cv2.THRESH_TOZERO_INV: It is the inversion of the above cv2.THRESH_TOZERO:

In my example, I am using the cv2.THRESH_BINARY thresholding technique. In the above image, I will do the two works. One is showing text only. And the second work is to showing heart shape only and hiding text. Run the following lines of code to display the output. You can display the image using the cv2.imshow() method.

Showing text only

import cv2
img = cv2.imread("heart.png",0)
ret,thresh = cv2.threshold(img,210,255,cv2.THRESH_BINARY)

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

Here you can see I am setting the value of threshold to 210 and the maximum pixel value is 255. If any pixel value in the picture is greater than 210 then its value will be 255. And below its value will be 0.

Output

Showing text part only using cv2 threshold method
Showing text part only using cv2 threshold method

Showing Heart Shape Only

import cv2
img = cv2.imread("heart.png",0)
ret,thresh = cv2.threshold(img,10,255,cv2.THRESH_BINARY)

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

Here I am using 10 and 255 as threshold and maximum value. The pixel value will be 255 if any pixel intensity is greater than 10, otherwise, it is set to 0.

Output

Showing heart shape part only using cv2 threshold method
Showing heart shape part only using cv2 threshold method

 

You can see in the above pictures how the threshold has changed the pixel values according to the threshold value set.

That’s all for now. Hope this entire post helped in understanding the concept of threshold and how to use it. If you have any doubt about it then you can contact us for more help. You can also reach our Data Science Learner Offical Facebook Page.

 

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.

Meet Sukesh ( Chief Editor ), a passionate and skilled Python programmer with a deep fascination for data science, NumPy, and Pandas. His journey in the world of coding began as a curious explorer and has evolved into a seasoned data enthusiast.
 
Thank you For sharing.We appreciate your support. Don't Forget to LIKE and FOLLOW our SITE to keep UPDATED with Data Science Learner