การหาคอนทัวร์

การประมวลผลภาพด้วย Python

Rebeca Gonzalez

Data Engineer

การหาคอนทัวร์

  • วัดขนาด
  • จำแนกรูปร่าง
  • นับจำนวนวัตถุ

จำนวนจุดทั้งหมดในโดมิโน: 29

การประมวลผลภาพด้วย Python

ภาพไบนารี

สร้างภาพไบนารีได้โดยใช้ thresholding หรือ edge detection

การประมวลผลภาพด้วย Python

การหาคอนทัวร์ด้วย scikit-image

เตรียมภาพ

แปลงภาพเป็นระดับสีเทา 2 มิติ

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

การประมวลผลภาพด้วย Python

การหาคอนทัวร์ด้วย scikit-image

เตรียมภาพ

แปลงภาพเป็นไบนารี

# Obtain the thresh value
thresh = threshold_otsu(image)

# Apply thresholding
thresholded_image = image > thresh

การประมวลผลภาพด้วย Python

การหาคอนทัวร์ด้วย scikit-image

จากนั้นใช้ 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)

การประมวลผลภาพด้วย Python

ค่าระดับคงที่

การประมวลผลภาพด้วย Python

ขั้นตอนการหาคอนทัวร์

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)
การประมวลผลภาพด้วย Python

ขั้นตอนการหาคอนทัวร์

ผลลัพธ์ที่ได้

การประมวลผลภาพด้วย Python

รูปร่างของคอนทัวร์

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)

การประมวลผลภาพด้วย Python

รูปร่างของคอนทัวร์

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) 

การประมวลผลภาพด้วย Python

รูปร่างของคอนทัวร์

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) 

การประมวลผลภาพด้วย Python

รูปร่างของคอนทัวร์

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) 

การประมวลผลภาพด้วย Python

รูปร่างของคอนทัวร์

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 

จำนวนจุด: 7

การประมวลผลภาพด้วย Python

มาฝึกกันเถอะ!

การประมวลผลภาพด้วย Python

Preparing Video For Download...