cv2 threshold method implementation in Python : In 3 Steps Only

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 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)

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.


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)

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.


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.

Source link

Faatiha Aayat Age Education Biography & More

Fatiha Aayat is a most talented Muslim kid, She is a 4th-grade student of the “New York Gifted and Talented program” Recently she founded a new organization named “CHIL& D”, By this organization she wants to help Arpan and poor people.

Faathiha ayat picture
Faathiha aayat picture

Today I will share with you the ultimate information about Bangladeshi-American citizen Fatiha Aayat. So Let’s start Fatiha Aayat Biography.

Quick Info

Full Name: Faatiha Aayat
NickName: Faatiha
Date of Birth: 2 Feb 2013
Birthplace: Not Known
Nationality: Bangladeshi and American
Occupation: Science Student
Religion: Muslim
Height Weight & Body Facts
Height: 4 feet 3 inches
Weight: 32 Kg
Body Info Not Known
Eye Color: Black
Hair Color: Black
Family info
Father: Name Not known
Faatiha Ayat with father
Mother: Not Public
Faatiha Ayat with mother photo

Faatiha Aayat career

Fateeha is just 7 years old, But she has long career info, She archived many awards, Many online magazines called him a magic girl.

In the Ramadan of 2020, She has hosted an Islamic show named “Ramadan with Fateeha”.

In the national child-math competition of America, She owns 3 options out of 5.

Faatiha Aayat on TBN24

Fateeha Ayat photos

Fateeha Ayat photo
Fateeha Ayat pic
Fateeha Ayat with friend photo
Fateeha Ayat with father photo

Official Social Media Links

These links and contact info were collected from Faatiha Aayat’s official Facebook page. (Updated by Sonia Sabnom)

Some lasserknown fact about Fatiha Ayat (Girl)

  • Fatiha Ayat age is now 8 years as 2021.
  • She love math from beginning.
  • Now Fatiha is living in USA.
  • Fatiha love Bangladeshi Islamic scholar Mizanur Rahman Azhari.
  • She hosted Facebook live with Ayman Sadiq, (10 min school founder) Chomok Hasan and many famous people.

If you want to edit this biography, Please let us know with a comment, Or please send an email to us with our contact page. Your edit and improvement will read thousand of people.

Source link