How to reduce valid coin mask true pixels outside valid coins?

Hi, I need help for the valid coins segementation. I struggle to resolve the error: Your valid coin mask has one or more true pixels outside valid coins. I have try more than 30 attempts but still don't get to resolve the error. I don' t know where is my code went wrong. I even try many times to adjust the dilate and erode values and find circles values, but still not resolve the error.
[testcoinMask,MaskedtestCoin] = segmentCoinFace(testCoinImage);
se = strel("disk", 25, );
testcoinMask = imfill(testcoinMask, "holes");
testcoinMask = imerode(testcoinMask, se);
imgFilt = imgaussfilt(MaskedtestCoin, 1, "Padding", "circular","FiltterDomain,"Frequency","FilterSize",3);
faceEdgeMask = edge(imgFilt, "sobel", 0.04, "both");
faceEdgeMask(~testcoinMask) = false;
seFE = strel("disk",60,0);
fEdgeMask = imfill(faceEdgeMask, "holes");
BW2 = imdilate(fEdgeMask,seFE);
validCoinMask = BW2 & testcoinMask;
se2 = strel("disk",65,0);
validCoinMask = imdilate(validCoinMask, se2);
imshow(validCoinMask)
[centers,radii] = imfindcircles(validCoinMask,[105,150]);
imshow(validCoinMask);
viscircles(centers,radii,"Edgecolor','b');
coinProps = regionprops("table",validCoinMask,"Area","Perimeter","Centroid");
disp(coinProps)

 Accepted Answer

Don't use imfindcircles. Use bwareafilt to select only a certain number of blobs, or blobs in a certain size range.

7 Comments

Hi, thank you for your reply. I have follow your suggestion to use bwareafilt, but still get the same error. Your valid coin mask has one or more true pixels outside valid coins.
Here is the code:
... continue
validCoinMask = BW2 & testcoinMask;
se2 = strel('disk',60,0);
validcoinMask = imdilate(validCoinMask, se2);
coinProps = regionprops("table", validCoinMask, 'Area', 'Perimeter');
areas = table2array(coinProps(:, 'Area'));
range = [min(areas ) max(areas)];
validCoinMask = bwareafilt(validCoinMask, range);
imshow(validCoinMask)
Hi, would like to share with you I have passed the test. Because I make the adjustment again on the BW2 and validCoinMask dilate values. Thank you for your help to provide me the hints that use bwareafilt. Thank you so much.
Glad it's working now for you. I never was sure what youi considered "valid" coins so I just assumed it was based on size.
If this Answer solves your original question, then could you please click the "Accept this answer" link to award the answerer with "reputation points" for their efforts in helping you? They'd appreciate it. Thanks in advance. 🙂 Note: you can only accept one answer (so pick the best one) but you can click the "Vote" icon for as many Answers as you want. Voting for an answer will also award reputation points.
@Ahsan please start your own new dicsussion thread and attach your "testCoinImage3.png" image and m-file with the paperclip icon. By the way, the call to imadjust is not needed and just slows it down.
@Natalie can you please share your code you used to pass the course?
@Natalie can you share the code if possible as i am also stuck at the same question
@Joel and @Ariba, size filtering is gone over in my Image Segmentation Tutorial in my File Exchange:
It's a generic, general purpose demo (a complete, running script) of how to threshold an image to find blobs, and then measure things about the blobs, and extract certain blobs based on their areas or diameters.
If you still have questions, start a new discussion thread with any code you might have and attach your image.

Sign in to comment.

More Answers (0)

Products

Release

R2023a

Community Treasure Hunt

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

Start Hunting!