Extracts the score from blue and red dices from a given image, also it outputs the score for each dice separately.
Function 'extract_dice_score' returns 2 numbers that represent the sum of numbers on blue and red dices.
Function 'extract_dice_score_bonus' returns the score for each dice separately.
Extraction of the scores on the blue and red dices:
The images are converted from RGB to HSV color format, because the idea is to use the saturation channel to extract blue and red dots from the background and to use hue channel to distinguish between red and blue dots.
Setting the saturation threshold to 0.5 the image is binarized and the blue and red dots are extracted:
To remove the blue and red dots on the sides, morphological operation of openning is performed. Then morphological operation of erosion is performed to separte the segments:
By counting the connected objects on the resulting image the sum of the scores on the blue and red dices is extracted.
Setting the threshold for the hue channel to 0.93 red segments are extracted from the orignal image:
As before, same morphological operations of openning and erosion are applied:
By counting the number of connected objects, the score of the red dices is calculated. Subtracting that score from the sum of the scores on the blue and red dices we get the score of the blue dices.
Extraction of the scores on each dice separately:
Same segmentation steps are applied as before and now we have 2 binarized images. First image contains blue and red segments and the second contains only red segments. The following algorithm is applied on both images:
Using the function regionprops we extract the position of the centroids for each connected segment. Each centroid initially gets unique integer starting from 1. That number represents the number of the dice that centroid belongs to. We iterate thorugh all of the centroids and if the distance between 2 centroids is relatively small (threshold distance is hyperparameter), second centroid gets the integer number of the dice of the first centroid. Now resulting centroids that belong to the same dice have the same integer number. By summing the same integer numbers we get the score on each dice separately.
By applying this algorithm on the second image we get the scores of the red dices and by applying it to the first image we get scores of the red and blue dices. Excluding the scores which appear in the red dices from the joint scores we get the scores on the blue dices.
Run the main.py to test the functions on 12 images.
Hyperparameters are tuned for Matlab 2017.