Change language

Python | Threshold Methods Using OpenCV | Set-3 (Otsu Trasholding)

In Otsu Thresholding , the threshold value is not selected, but is determined automatically. A bimodal image (two different image values) is considered. The generated histogram contains two peaks. So a general condition would be to choose a threshold that is in the middle of both peaks of the histogram.

We use the traditional cv2.threshold function and use cv2.THRESH_OTSU as an optional flag.

Syntax: cv2.threshold (source, thresholdValue, maxVal , thresholdingTechnique)

Parameters:
-"  source : Input Image array (must be in Grayscale).
-"  thresholdValue : Value of Threshold below and above which pixel values ​​will change accordingly.
-"  maxVal : Maximum value that can be assigned to a pixel.
-"  thresholdingTechnique : The type of thresholding to be applied.

Below is the Python code explaining the father-threshold technique —

# Python program for illustration
# Type of otsu threshold in the image

 
# organization of import

import cv2 

import numpy as np 

 
# the path to the input image is specified and
# the image is loaded using the imread command

image1 = cv2.imread ( ’ input1.jpg’ )

 
# cv2.cvtColor is applied over
# image input with parameters applied
# convert image to grayscale

img = cv2.cvtColor (image1, cv2.COLOR_BGR2GRAY)

  
# application Otsu threshold
# as an optional flag in binary
# threshold

ret, thresh1 = cv2.threshold (img, 120 , 255 , cv2.THRESH_BINARY +  

< code class = "undefined spaces">  cv2.THRESH_OTSU) 

 
# window with the displayed image
# with an appropriate threshold
# methods applied to the input image

cv2.imshow ( ’Otsu Threshold’ , thresh1) 

 
# Cancel allocating any associated memory usage

if cv2.waitKey ( 0 ) & amp;  0xff = = 27 :

cv2.destroyAllWindows () 

Input data :

Exit:

The calculation assumes that the image contains two classes of pixels, following the pixels of the foreground and background, at which point an ideal limit is set, isolating the two classes in order to be consolidated the spread was negligible.

Shop

Gifts for programmers

Learn programming in R: courses

$FREE
Gifts for programmers

Best Python online courses for 2022

$FREE
Gifts for programmers

Best laptop for Fortnite

$399+
Gifts for programmers

Best laptop for Excel

$
Gifts for programmers

Best laptop for Solidworks

$399+
Gifts for programmers

Best laptop for Roblox

$399+
Gifts for programmers

Best computer for crypto mining

$499+
Gifts for programmers

Best laptop for Sims 4

$

Latest questions

PythonStackOverflow

Common xlabel/ylabel for matplotlib subplots

1947 answers

PythonStackOverflow

Check if one list is a subset of another in Python

1173 answers

PythonStackOverflow

How to specify multiple return types using type-hints

1002 answers

PythonStackOverflow

Printing words vertically in Python

909 answers

PythonStackOverflow

Python Extract words from a given string

798 answers

PythonStackOverflow

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

606 answers

PythonStackOverflow

Python os.path.join () method

384 answers

PythonStackOverflow

Flake8: Ignore specific warning for entire file

360 answers

News


Wiki

Python | How to copy data from one Excel sheet to another

Common xlabel/ylabel for matplotlib subplots

Check if one list is a subset of another in Python

How to specify multiple return types using type-hints

Printing words vertically in Python

Python Extract words from a given string

Cyclic redundancy check in Python

Finding mean, median, mode in Python without libraries

Python add suffix / add prefix to strings in a list

Why do I get "Pickle - EOFError: Ran out of input" reading an empty file?

Python - Move item to the end of the list

Python - Print list vertically