How to identify black dots on dice via image processing? (MATLAB)

3 views (last 30 days)
Hi, I have some pictures of two white dices with black dots, and I need to recognize the value on each visible face of dices. For example, from this image:
I need to get [5 6]. Any ideas?
It's really urgent, I need to submit my final project and this is the last piece. Thanks a lot!

Answers (1)

Image Analyst
Image Analyst on 11 Oct 2015
It's not too hard. Of course it would be easier if you could start out with a good image. Why are you zoomed out so much and tossing the dice on a varying background? Why don't you toss them onto a dark background, like black velvet? This would reduce the chance you need to do any noise cleanup. If you're luck, you can just threshold for dark spots and call bwlabel() to count them
[!, numSpots] = bwlabel(grayImage < 128); % or whatever value works.
You can also adapt my Image Segmentation Tutorial for a full demo. Just reverse the direction of the < sign so that you threshold for dark spots instead of light spots. Here is the code: http://www.mathworks.com/matlabcentral/fileexchange/?term=authorid%3A31862
If it fails for some images, then there are some things you can do, like first threshold for white (assuming that the only white will be on the dice), Then fill the holes with imfill() and then use this as a mask on the whole image. Then find the black spots.
  2 Comments
Sapir
Sapir on 11 Oct 2015
Hi, Thanks for your respond. I cannot neither use black background, nor change the zoom. Those are my constraints.
I'm using your algorithm to identify the sum on both dices, and it works fine. My problem is that I need to separate the numbers, so I will get [5 6] instead of 11 (in the attached image). Do you have any suggestions how to get those two numbers?
The algorithm:
rgb = snapshot(cam);
%work - [295.5 224.5 23 38]
X2 = imcrop(rgb, [83.5 189.5 453 87]);
rgbImage = X2;
% Get the dimensions of the image. numberOfColorBands should be = 3.
[rows ,columns, numberOfColorBands] = size(rgbImage);
% Display the original color image.
subplot(2, 2, 1);
imshow(rgbImage, []);
title('Original color Image');
% Enlarge figure to full screen.
set(gcf, 'Position', get(0,'Screensize'));
% Extract the individual red, green, and blue color channels.
redChannel = rgbImage(:, :, 1);
greenChannel = rgbImage(:, :, 2);
blueChannel = rgbImage(:, :, 3);
spots = rgbImage(:, :, :) < 58;
spots = spots(:, :, 1) == spots(:, :, 2) == spots(:, :, 3);
subplot(2, 2, 2);
imshow(spots, []);
title('Thresholded Red Channel');
spots = imclearborder(spots,8);
subplot(2, 2, 3);
imshow(spots, []);
title('Border Cleared');
% Fill holes
spots = imfill(spots, 'holes');
subplot(2, 2, 4);
imshow(spots, []);
title('Final Spots Image');
impixelinfo;
% Count them
L = bwlabel(spots);
[labeledImage ,numberOfSpots] = bwlabel(spots);
message = sprintf('Done!\nThe number of spots (total on both dice) is %d', numberOfSpots);
msgbox(message);
[IDX] = kmeans(L,2);
figure, imshow(rgbImage,[]);
Thanks a lot!
Image Analyst
Image Analyst on 11 Oct 2015
Then first you'll need to identify each dice one at a time. First threshold for white and look for blobs of the right size. Then, if there is none but only a blob about twice as large, then the dice are touching and you'll have to split it apart with imerode. Then use your algorithm on each die one at a time.

Sign in to comment.

Categories

Find more on Visual Exploration in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!