轮廓检测

Python 图像处理

Rebeca Gonzalez

Data Engineer

轮廓检测

  • 测量尺寸
  • 分类形状
  • 计算对象数量

多米诺子上的点数:29。

Python 图像处理

二值图像

可通过应用阈值分割或使用边缘检测获得二值图像。

Python 图像处理

用 scikit-image 查找轮廓

准备图像

将图像转为二维灰度。

# 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 图像处理

轮廓的形状

轮廓:由形状为 (n,2) 的 ndarray 构成的列表。

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 图像处理

Passons à la pratique !

Python 图像处理

Preparing Video For Download...