Finding contours

Image Processing in Python

Rebeca Gonzalez

Data Engineer

Finding contours

  • Measure size
  • Classify shapes
  • Determine the number of objects

Total points in domino tokens: 29.

Image Processing in Python

Binary images

We can obtain a binary image applying thresholding or using edge detection

Image Processing in Python

Find contours using scikit-image

Preparing the image

Transform the image to 2D grayscale.

# Make the image grayscale
image = color.rgb2gray(image)

Image Processing in Python

Find contours using scikit-image

Preparing the image

Binarize the image

# Obtain the thresh value
thresh = threshold_otsu(image)

# Apply thresholding
thresholded_image = image > thresh

Image Processing in Python

Find contours using scikit-image

And then use find_contours().

# Import the measure module
from skimage import measure

# Find contours at a constant value of 0.8
contours = measure.find_contours(thresholded_image, 0.8)

Image Processing in Python

Constant level value

Image Processing in Python

The steps to spotting contours

from skimage import measure
from skimage.filters import threshold_otsu

# Make the image grayscale
image = color.rgb2gray(image)

# Obtain the optimal thresh value of the image thresh = threshold_otsu(image) # Apply thresholding and obtain binary image thresholded_image = image > thresh
# Find contours at a constant value of 0.8 contours = measure.find_contours(thresholded_image, 0.8)
Image Processing in Python

The steps to spotting contours

Resulting in

Image Processing in Python

A contour's shape

Contours: list of (n,2) - ndarrays.

for contour in contours:
    print(contour.shape)
(433, 2)
(433, 2)
(401, 2)
(401, 2) 
(123, 2)
(123, 2) 
(59, 2)
(59, 2)
(59, 2)
(57, 2)
(57, 2)
(59, 2)
(59, 2)

Image Processing in Python

A contour's shape

for contour in contours:
    print(contour.shape)
(433, 2)
(433, 2) --> Outer border
(401, 2)
(401, 2)
(123, 2)
(123, 2)
(59, 2)
(59, 2)
(59, 2)
(57, 2)
(57, 2)
(59, 2)
(59, 2) 

Image Processing in Python

A contour's shape

for contour in contours:
    print(contour.shape)
(433, 2)
(433, 2) --> Outer border
(401, 2)
(401, 2) --> Inner border
(123, 2)
(123, 2) 
(59, 2)
(59, 2)
(59, 2)
(57, 2)
(57, 2)
(59, 2)
(59, 2) 

Image Processing in Python

A contour's shape

for contour in contours:
    print(contour.shape)
(433, 2)
(433, 2) --> Outer border
(401, 2)
(401, 2) --> Inner border
(123, 2)
(123, 2) --> Divisory line of tokens
(59, 2)
(59, 2)
(59, 2)
(57, 2)
(57, 2)
(59, 2)
(59, 2) 

Image Processing in Python

A contour's shape

for contour in contours:
    print(contour.shape)
(433, 2)
(433, 2) --> Outer border
(401, 2)
(401, 2) --> Inner border
(123, 2)
(123, 2) --> Divisory line of tokens
(59, 2)
(59, 2)
(59, 2)
(57, 2)
(57, 2)
(59, 2)
(59, 2) --> Dots 

Number of dots: 7.

Image Processing in Python

Let's practice!

Image Processing in Python

Preparing Video For Download...