Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting the upper side of a dice

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

like image 331
tfv Avatar asked Apr 11 '16 18:04

tfv


2 Answers

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.

like image 155
Adib Avatar answered Sep 28 '22 05:09

Adib


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..

like image 42
Cyb3rFly3r Avatar answered Sep 28 '22 04:09

Cyb3rFly3r