Konturen finden

Bildverarbeitung mit Python

Rebeca Gonzalez

Data Engineer

Konturen finden

  • Größe messen
  • Formen klassifizieren
  • Anzahl der Objekte bestimmen

Gesamtpunkte auf Dominosteinen: 29.

Bildverarbeitung mit Python

Binärbilder

Wir erhalten ein Binärbild durch Thresholding oder Kantenerkennung

Bildverarbeitung mit Python

Konturen mit scikit-image finden

Bild vorbereiten

Wandle das Bild in 2D‑Graustufen um.

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

Bildverarbeitung mit Python

Konturen mit scikit-image finden

Bild vorbereiten

Binarisiere das Bild

# Obtain the thresh value
thresh = threshold_otsu(image)

# Apply thresholding
thresholded_image = image > thresh

Bildverarbeitung mit Python

Konturen mit scikit-image finden

Und dann find_contours() verwenden.

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

Bildverarbeitung mit Python

Konstantes Level

Bildverarbeitung mit Python

Schritte zum Erkennen von Konturen

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)
Bildverarbeitung mit Python

Schritte zum Erkennen von Konturen

Ergebnis

Bildverarbeitung mit Python

Die Form einer Kontur

Konturen: Liste von (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)

Bildverarbeitung mit Python

Die Form einer Kontur

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) 

Bildverarbeitung mit Python

Die Form einer Kontur

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) 

Bildverarbeitung mit Python

Die Form einer Kontur

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

Bildverarbeitung mit Python

Die Form einer Kontur

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

Anzahl der Punkte: 7.

Bildverarbeitung mit Python

Lass uns üben!

Bildverarbeitung mit Python

Preparing Video For Download...