Count the dots in a dice's image

Now we have found the contours, we can extract information from it.

In the previous exercise, we prepared a purple dices image to find its contours:

3 images showing the steps to find contours

This time we'll determine what number was rolled for the dice, by counting the dots in the image.

The contours found in the previous exercise are preloaded as contours.

Create a list with all contour's shapes as shape_contours. You can see all the contours shapes by calling shape_contours in the console, once you have created it.

Check that most of the contours aren't bigger in size than 50. If you count them, they are the exact number of dots in the image.

show_image_contour(image, contours) is a preloaded function that displays the image with all contours found using Matplotlib.

Diese Übung ist Teil des Kurses

Image Processing in Python

Kurs anzeigen

Anleitung zur Übung

  • Make shape_contours be a list with all contour shapes of contours.
  • Set max_dots_shape to 50.
  • Set the shape condition of the contours to be the maximum shape size of the dots max_dots_shape.
  • Print the dice's number.

Interaktive Übung zum Anfassen

Probieren Sie diese Übung aus, indem Sie diesen Beispielcode ausführen.

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