開始使用免費開始

計算骰子影像上的點數

現在我們已經找出了輪廓,就可以從中擷取資訊了。

在前一個練習中,我們準備了一張紫色骰子的影像來尋找其輪廓:

3 images showing the steps to find contours

這次要透過計算影像中的點數,判斷骰子擲出的點數。

前一個練習所找到的輪廓已預先載入為 contours

建立一個包含所有輪廓形狀的清單 shape_contours。建立後,你可以在主控台呼叫 shape_contours 來查看所有輪廓的形狀。

檢查大多數輪廓的大小不超過 50。當你數一數,它們就剛好是影像中的點數。

show_image_contour(image, contours) 是預先載入的函式,會使用 Matplotlib 顯示含有所有已找到輪廓的影像。

本練習屬於課程

Python 影像處理

檢視課程

練習說明

  • shape_contours 設為由 contours 的所有輪廓形狀所組成的清單。
  • max_dots_shape 設為 50。
  • 設定輪廓的形狀條件,使其小於點的最大形狀大小 max_dots_shape
  • 列印出骰子的點數。

動手互動練習

試著完成這個範例程式碼,體驗一下這個練習。

# Create list with the shape of each contour
shape_contours = [cnt.shape[0] for cnt in ____]

# Set 50 as the maximum size of the dots shape
max_dots_shape = ____

# Count dots in contours excluding bigger than dots size
dots_contours = [cnt for cnt in contours if np.shape(cnt)[0] < ____]

# Shows all contours found 
show_image_contour(binary, contours)

# Print the dice's number
print("Dice's dots number: {}. ".format(len(____)))
編輯並執行程式碼