Getting started with thresholding

Image Processing in Python

Rebeca Gonzalez

Data Engineer

Thresholding

Partitioning an image into a foreground and background

By making it black and white

We do so by setting each pixel to:

  • 255 (white) if pixel $\gt$ thresh value
  • 0 (black) if pixel $\lt$ thresh value

Thresholded image of a man with camera

Image Processing in Python

Thresholding

Simplest method of image segmentation

  • Isolate objects
    • Object detection
    • Face detection
    • Etc.

Thresholded image of domino tokens

Image Processing in Python

Thresholding

Only from grayscale images

Steps to threshold images, showed with domino tokens. First being original RGB-3 colored, then grasycale and then thresholded

Image Processing in Python

Apply it

# Obtain the optimal threshold value
thresh = 127

# Apply thresholding to the image binary = image > thresh
# Show the original and thresholded show_image(image, 'Original') show_image(binary, 'Thresholded')

Thresholded image of a man with camera

Image Processing in Python

Inverted thresholding

# Obtain the optimal threshold value
thresh = 127

# Apply thresholding to the image inverted_binary = image <= thresh
# Show the original and thresholded show_image(image, 'Original') show_image(inverted_binary, 'Inverted thresholded')

Inverted thresholded image of a man with camera

Image Processing in Python

Categories

  • Global or histogram based: good for uniform backgrounds

  • Local or adaptive: for uneven background illumination

Comparation of global and local thresholding

Image Processing in Python

Try more thresholding algorithms

from skimage.filters import try_all_threshold

# Obtain all the resulting images fig, ax = try_all_threshold(image, verbose=False)
# Showing resulting plots show_plot(fig, ax)
Image Processing in Python

Try more thresholding algorithms

All global thresholding mdethods tested in a grascale image of a text page

Image Processing in Python

Optimal thresh value

Global

Uniform background
# Import the otsu threshold function
from skimage.filters import threshold_otsu

# Obtain the optimal threshold value thresh = threshold_otsu(image)
# Apply thresholding to the image binary_global = image > thresh
Image Processing in Python

Optimal thresh value

Global

# Show the original and binarized image
show_image(image, 'Original')
show_image(binary_global, 'Global thresholding')

Original image of a man with camera, this image thresholded and its histogram with a red line spotting the optimal thresh value

Image Processing in Python

Optimal thresh value

Local

Uneven background
# Import the local threshold function
from skimage.filters import threshold_local

# Set the block size to 35 block_size = 35
# Obtain the optimal local thresholding local_thresh = threshold_local(text_image, block_size, offset=10)
# Apply local thresholding and obtain the binary image binary_local = text_image > local_thresh
Image Processing in Python

Optimal thresh value

Local

# Show the original and binarized image
show_image(text_image, 'Original')
show_image(binary_local, 'Local thresholding')

Locally thresholded image of a text page

Image Processing in Python

Let's practice!

Image Processing in Python

Preparing Video For Download...