8

Is it possible to detect the upper side of a dice? While this will be an easy task if you look from the top, from many perspectives multiple sides are visible.

Here is an example of a dice, feel free to take your own pictures: enter image description here

You usually want to know the score you have achieved. It is easy for me to extract ALL dots, but how to only extract those on the top? In this special case, the top side is the largest, but this might not always be true. I am looking for someting which evaluates the distortion of the top square (or circle in this case, which I can extract) in relation to the perspective given by the grid in the bottom.

Example program with some results is given below.

import numpy as np
import cv2

img = cv2.imread('dice.jpg')


# Colour range to be extracted
lower_blue = np.array([0,0,0])
upper_blue = np.array([24,24,24])

# Threshold the BGR image 
dots = cv2.inRange(img, lower_blue, upper_blue)

# Colour range to be extracted
lower_blue = np.array([0,0,0])
upper_blue = np.array([226,122,154])

# Threshold the BGR image 
upper_side_shape = cv2.inRange(img, lower_blue, upper_blue)

cv2.imshow('Upper side shape',upper_side_shape)
cv2.imshow('Dots',dots)

cv2.waitKey(0)
cv2.destroyAllWindows()

Some resulting images: enter image description here

tfv
  • 6,016
  • 4
  • 36
  • 67
  • 2
    The simplest way is by defining the qualified area of each white dot. In essence, the white dot should be of at least 40-50px worth or something. That's one approach. – Adib Apr 11 '16 at 19:07
  • 1
    I think I see what you mean: Your point is that, independent of the perspective, within one side, the size of dots would always be almost uniform. If I then look for the dot which is at the highest position, I'd only have to count all dots which are of "equal" size" equality being defined by the size distance to other dots, and belong to a distorted dot pattern which makes sense for a dice. – tfv Apr 11 '16 at 19:12
  • That's correct. You can base things on tolerance against the largest dot. For example, if the largest dot is 45px, you can set that as max, and then you can set the tolerance of -2. So your dots should be within the range of 44-45 (since 45 is max) – Adib Apr 11 '16 at 19:15

3 Answers3

2

The best solution is dot size, which I mentioned in the comment. You find the largest dot, consider it as max, and then create a tolerance level.

But what if all dots are nearly equal (viewing it from the edge at angle that makes things equidistant), or even too small? The best solution for that is creating a boundary to capture the dots. This requires the analysis of the dice's edge (edge detection basically), but once you define the boundary, you're solid.

All you need is to capture the edges of the dice from the perspective you're seeing.

Here's a visual example:

enter image description here

Since you have a virtual boundary set, you'll simply measure dots above a specific point on the y-axis.

Adib
  • 1,282
  • 1
  • 16
  • 32
2

The dot size is a good heuristic, but I would also add the dot roundness: if you compute the second order image moments of the binarized dots, the more the x and y moment are similar, the more round the figure. This will of course fail, like the size, for a side view, but then what does "top-side" really means if you can't sense gravity..

Cyb3rFly3r
  • 1,321
  • 7
  • 12
0

why try to chop up the image at all? based on what numbers you see on the side you can infer what number is on the top. your side numbers can be used as a check to validate your guess.

note that you'll have to be careful about handedness (see: http://mathworld.wolfram.com/Dice.html)

yjhuoh
  • 56
  • 2