尋找等高線

Python 影像處理

Rebeca Gonzalez

Data Engineer

尋找等高線

  • 測量大小
  • 分類形狀
  • 判斷物件數量

多米諾上的總點數:29。

Python 影像處理

二值影像

可以用 thresholdingedge detection 取得二值影像

Python 影像處理

使用 scikit-image 尋找等高線

準備影像

將影像轉成 2D 灰階。

# 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 影像處理

等高線的 shape

等高線:由多個 (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 影像處理

等高線的 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) 

Python 影像處理

等高線的 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) 

Python 影像處理

等高線的 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) 

Python 影像處理

等高線的 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 

點的數量:7。

Python 影像處理

一起來練習吧!

Python 影像處理

Preparing Video For Download...