# identifying number on dice

조회 수: 1 (최근 30일)
Sameer 2014년 5월 25일
댓글: Image Analyst 2020년 10월 21일
% I want to crop the front face of dice and identify the number on it. I tried the following
figure,imshow(A); title('Original Image');
%Convert the Image to binary
B=im2bw(A);
%Fill the holes
C=imfill(B,'holes');
%Label the connected components
[Label,Total]=bwlabel(C,8);
figure,imshow(C); title('Labelled Image');
% then I did the following
D=imsubtract(B,C);
imshow(D);
%
where Background is black, dice is white with black dots. But WHEN BACKGROUND IS NOT PERFECTLY BLACK THEN IT CREATES PROBLEM. CAN YOU PLZ TELL ME HOW REMOVE BACKGROUND AND CROP ONLY DICE PART?
##### 댓글 수: 2없음 표시없음 숨기기
Aayush Maharjan 2020년 10월 21일
How can I do the same using python.??? I don't understand Matlab code.
Image Analyst 2020년 10월 21일
What do you understand? Python? If so, you should know the corresponding functions in Python. I don't know Python's library functions well enough to do the conversion for you. But presumably you know Python much better than MATLAB so you should be able to translate it yourself. If you don't, then try asking in a Python language community forum.

댓글을 달려면 로그인하십시오.

### 채택된 답변

Sven 2014년 5월 25일
편집: Sven 2014년 5월 25일
Hi Sameer, Here's what I would do. It's a little like IA's bwareaopen suggestion. It tries to make minimal assumptions other than:
1. Dice are white and bigger than 1000 pixels
2. (front facing) dots are surrounded by dice and bigger than 50 pixels
% Fetch the image
G = rgb2gray(I);
% Dice are white and big
diceBlobs = bwareaopen(G>180, 1000);
cc = bwconncomp(diceBlobs);
diceLabels = labelmatrix(cc);
for i = 1:cc.NumObjects
thisDice = diceLabels==i;
% Dots are surrounded by dice and +50 pixels
thisDots = bwareaopen(imfill(thisDice,'holes') & ~thisDice, 50);
dotsCC = bwconncomp(thisDots);
figure, imshow(thisDice*0.5 + thisDots)
title(sprintf('Dice #%d has %d dots',i,dotsCC.NumObjects))
end
Does this code help you out? You could certainly add some further criteria such as dots needing to be round (I would use regionprops and look at, say, the Solidity property), and maybe dice should be square, but this seems reasonably robust as long as your pictures are all quite similar.
Thanks, Sven.
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Sameer 2014년 5월 25일
Thank you very much

댓글을 달려면 로그인하십시오.

### 추가 답변 (1개)

Image Analyst 2014년 5월 25일
You forgot to attach your image(s). It's more difficult to give advice on image processing without an image. Maybe you can do a median filter or maybe call bwareaopen, or do some size filtering. If the black spots in the background are the same size as your spots, you will have to make a mask. Threshold for bright things, fill them in, then take the largest.
##### 댓글 수: 1이전 댓글 -1개 표시이전 댓글 -1개 숨기기
Sameer 2014년 5월 25일
ooops, I'm sorry. here is the image.

댓글을 달려면 로그인하십시오.

### 카테고리

Help CenterFile Exchange에서 Call Python from MATLAB에 대해 자세히 알아보기

### Community Treasure Hunt

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

Start Hunting!

Translated by