Contours vinden

Beeldverwerking in Python

Rebeca Gonzalez

Data Engineer

Contours vinden

  • Grootte meten
  • Vormen classificeren
  • Aantal objecten bepalen

Totaal aantal punten in dominostenen: 29.

Beeldverwerking in Python

Binaire afbeeldingen

We kunnen een binaire afbeelding krijgen door een drempel toe te passen of randdetectie te gebruiken

Beeldverwerking in Python

Contours vinden met scikit-image

De afbeelding voorbereiden

Zet de afbeelding om naar 2D-grijswaarden.

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

Beeldverwerking in Python

Contours vinden met scikit-image

De afbeelding voorbereiden

Binariseer de afbeelding

# Obtain the thresh value
thresh = threshold_otsu(image)

# Apply thresholding
thresholded_image = image > thresh

Beeldverwerking in Python

Contours vinden met scikit-image

En gebruik daarna 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)

Beeldverwerking in Python

Constante niveaunwaarde

Beeldverwerking in Python

Stappen om contours te vinden

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)
Beeldverwerking in Python

Stappen om contours te vinden

Resulterend in

Beeldverwerking in Python

De vorm van een contour

Contours: lijst van (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)

Beeldverwerking in Python

De vorm van een contour

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

Beeldverwerking in Python

De vorm van een contour

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

Beeldverwerking in Python

De vorm van een contour

for contour in contours:
    print(contour.shape)
(433, 2)
(433, 2) --> Buitenrand
(401, 2)
(401, 2) --> Binnenrand
(123, 2)
(123, 2) --> Scheidingslijn van stenen
(59, 2)
(59, 2)
(59, 2)
(57, 2)
(57, 2)
(59, 2)
(59, 2) 

Beeldverwerking in Python

De vorm van een contour

for contour in contours:
    print(contour.shape)
(433, 2)
(433, 2) --> Buitenrand
(401, 2)
(401, 2) --> Binnenrand
(123, 2)
(123, 2) --> Scheidingslijn van stenen
(59, 2)
(59, 2)
(59, 2)
(57, 2)
(57, 2)
(59, 2)
(59, 2) --> Stippen 

Aantal stippen: 7.

Beeldverwerking in Python

Laten we oefenen!

Beeldverwerking in Python

Preparing Video For Download...