@Image AnalystHello there sir, if you would be kind enough to check the abive code and tell me whats wrong, becaus eive used this same code in the next/last step of the project to the count the value of the coins and ive passed it.
Info
This question is locked. 편집하거나 답변을 올리려면 질문을 다시 여십시오.
Valid Coin Mask True Pixels Error Outside or Beyond the Actual Mask Error
조회 수: 4 (최근 30일)
이전 댓글 표시
imread("testCoinImage3.png");
[testcoinMask,MaskedtestCoin] = segmentCoinFace(testCoinImage);
se = strel("disk", 4, 0);
testcoinMask = imfill(testcoinMask, "holes");
testcoinMask = imerode(testcoinMask, se);
imgFilt = imgaussfilt(MaskedtestCoin, 1, 'Padding', 'circular', 'FilterDomain', 'frequency', 'FilterSize', 3);
faceEdgeMask = edge(imgFilt, "sobel", 0.09, "both");
faceEdgeMask(~testcoinMask) = false;
seFE = strel("disk",75,0);
fEdgeMask = imfill(faceEdgeMask, "holes");
BW2 = imdilate(fEdgeMask,seFE);
validCoinMask = BW2 & testcoinMask;
se2 = strel('disk',75,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)
function [BW,maskedImage] = segmentCoinFace(X)
%segmentImage Segment image using auto-generated code from Image Segmenter app
% [BW,MASKEDIMAGE] = segmentImage(X) segments image X using auto-generated
% code from the Image Segmenter app. The final segmentation is returned in
% BW, and a masked image is returned in MASKEDIMAGE.
% Auto-generated by imageSegmenter app on 08-Jul-2023
%----------------------------------------------------
% Adjust data to span data range.
X = imadjust(X);
% Threshold image with global threshold
BW = imbinarize(im2gray(X));
% Open mask with default
radius = 2;
decomposition = 0;
se = strel('disk', radius, decomposition);
BW = imopen(BW, se);testCoinImage = imread("testCoinImage3.png");
[testcoinMask,MaskedtestCoin] = segmentCoin(testCoinImage);
% Shrink the coin mask.
se = strel('disk', 3, 0);
testcoinMask = imfill(testcoinMask, 'holes'); % Fill any holes in it.
testcoinMask = imerode(testcoinMask, se); % Shrink by 3 layers of pixels.
% Find edges using original poster's code.
imgFilt = imgaussfilt(MaskedtestCoin,0.7,...
Padding="circular",FilterDomain="frequency",FilterSize=3);
faceEdgeMask = edge(imgFilt,"sobel",0.1,"both");
% Erase outside the shrunken coin mask to get rid of outer boundary.
faceEdgeMask(~testcoinMask) = false;
imshow(faceEdgeMask);
function [BW,maskedImage] = segmentCoin(X)
%segmentImage Segment image using auto-generated code from Image Segmenter app
% [BW,MASKEDIMAGE] = segmentImage(X) segments image X using auto-generated
% code from the Image Segmenter app. The final segmentation is returned in
% BW, and a masked image is returned in MASKEDIMAGE.
% Auto-generated by imageSegmenter app on 08-Jul-2023
%----------------------------------------------------
% Adjust data to span data range.
X = imadjust(X);
% Threshold image with global threshold
BW = imbinarize(im2gray(X));
% Open mask with default
radius = 2;
decomposition = 0;
se = strel('disk', radius, decomposition);
BW = imopen(BW, se);
% Close mask with default
radius = 2;
decomposition = 0;
se = strel('disk', radius, decomposition);
BW = imclose(BW, se);
% Fill holes
BW = imfill(BW, 'holes');
% Invert mask
BW = imcomplement(BW);
% Invert mask
BW = imcomplement(BW);
% Create masked image.
maskedImage = X;
maskedImage(~BW) = 0;
end
% Close mask with default
radius = 2;
decomposition = 0;
se = strel('disk', radius, decomposition);
BW = imclose(BW, se);
% Fill holes
BW = imfill(BW, 'holes');
% Invert mask
BW = imcomplement(BW);
% Invert mask
BW = imcomplement(BW);
% Create masked image.
maskedImage = X;
maskedImage(~BW) = 0;
end
in the se variable for structuring element , when I change the value to 4 it shows "Your valid coin mask has one or more true pixels outside valid coins," and when I change the se value to another value later on, it would show that it isn't covering all true valid coins . Please if you can help, I've been stuck on this since the past 3 days and I'm getting quite frustrated grrr. Thanks :)
댓글 수: 2
Cris LaPierre
2023년 7월 29일
There is a forum in the course, and I have it on good authority that the course authors are very responsive. Have you tried asking your question there?
답변 (4개)
Image Analyst
2023년 7월 20일
I don't think most of that is needed. I would just threshold, call imfill, and then call bwareaopen. The double imcomplement and imopen stuff are most likely not needed, however I can't run your code until tonight because I'm traveling and on a computer without MATLAB.
댓글 수: 13
Image Analyst
2023년 7월 29일
@Ahsan I threw out most of the unnecessary code and simplified it greatly. Here is what I got:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
testCoinImage = imread("testCoinImage3.png");
subplot(2, 2, 1);
imshow(testCoinImage)
title('Original Image', 'FontSize', fontSize)
% Segment the image.
[coinMask, MaskedtestCoin] = segmentCoinFace(testCoinImage);
subplot(2, 2, 2);
imshow(coinMask)
title('Mask', 'FontSize', fontSize)
subplot(2, 2, 3);
imshow(MaskedtestCoin)
title('Masked Image', 'FontSize', fontSize)
% Measure areas and perimeters.
coinProps = regionprops(coinMask, 'Area', 'Perimeter');
allAreas = [coinProps.Area]
allPerimeters = [coinProps.Perimeter]
range = [min(allAreas) max(allAreas)]
subplot(2, 2, 4);
bar(allAreas);
grid on;
title('Coin Areas', 'FontSize', fontSize)
%===========================================================================
function [BW, maskedImage] = segmentCoinFace(inputImage)
%segmentImage Segment image using auto-generated code from Image Segmenter app
% [BW, MASKEDIMAGE] = segmentImage(X) segments image X using auto-generated
% code from the Image Segmenter app. The final segmentation is returned in
% BW, and a masked image is returned in MASKEDIMAGE.
% Auto-generated by imageSegmenter app on 08-Jul-2023
%----------------------------------------------------
% Threshold image with global threshold
BW = imbinarize(im2gray(inputImage));
% Fill holes
BW = imfill(BW, 'holes');
% Find areas so we know what size blobs to exclude..
% props = regionprops(BW, 'Area');
% allAreas = sort([props.Area])
% Throw out blobs less than 2000 in area.
BW = bwareaopen(BW, 2000);
% Create masked image.
maskedImage = inputImage;
maskedImage(~BW) = 0;
end

댓글 수: 5
Image Analyst
2023년 8월 1일
So I guess the answer is "No, I didn't try what you said" about standard deviations.
OK, so here is how you could do it:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 18;
testCoinImage = imread("testCoinImage3.png");
subplot(2, 2, 1);
imshow(testCoinImage)
title('Original Image', 'FontSize', fontSize)
[coinMask, MaskedtestCoin] = segmentCoinFace(testCoinImage);
subplot(2, 2, 2);
imshow(coinMask)
title('Mask', 'FontSize', fontSize)
subplot(2, 2, 3);
imshow(MaskedtestCoin)
title('Masked Image', 'FontSize', fontSize)
% Measure areas and perimeters.
coinProps = regionprops(coinMask, testCoinImage, 'Area', 'Perimeter');
allAreas = [coinProps.Area]
allPerimeters = [coinProps.Perimeter]
range = [min(allAreas) max(allAreas)]
subplot(2, 2, 4);
bar(allAreas);
grid on;
title('Coin Areas', 'FontSize', fontSize)
%===========================================================================
function [BW, maskedImage] = segmentCoinFace(inputImage)
%segmentImage Segment image using auto-generated code from Image Segmenter app
% [BW, MASKEDIMAGE] = segmentImage(X) segments image X using auto-generated
% code from the Image Segmenter app. The final segmentation is returned in
% BW, and a masked image is returned in MASKEDIMAGE.
% Auto-generated by imageSegmenter app on 08-Jul-2023
%----------------------------------------------------
% Threshold image with global threshold
BW = imbinarize(im2gray(inputImage));
% Fill holes
BW = imfill(BW, 'holes');
% Find areas so we know what size blobs to exclude..
% props = regionprops(BW, 'Area');
% allAreas = sort([props.Area])
% Throw out blobs less than 2000 in area.
BW = bwareaopen(BW, 2000);
% Measure areas and perimeters.
labeledImage = bwlabel(BW);
coinProps = regionprops(BW, inputImage, 'PixelValues');
% Get the standard deviation of each coin
for k = 1 : numel(coinProps)
allSDs(k) = std(double(coinProps(k).PixelValues));
end
allSDs
% Get a mask of only where the StdDev is more than 15.
BW = ismember(labeledImage, find(allSDs >= 15));
% Create masked image.
maskedImage = inputImage;
maskedImage(~BW) = 0;
end

Ashutosh
2023년 12월 24일
편집: Walter Roberson
2023년 12월 25일
Valid Coin Segmentation
For this problem your code will need to create a mask which completely segments only the valid coins. Use the variable name validCoinMask.
The solutions to the previous assignments include a foreground mask with true pixel regions where any circular object is present (testCoinMask), and another coin face edge mask with true pixels only within the regions of valid coins (faceEdgeMask). Therefore, one approach to this task is to dilate the true pixel regions in faceEdgeMask to be greater than or equal to the size of the corresponding foreground regions in testCoinMask, and logically combine the result with testCoinMask to extract the valid coin regions. NOTE: Be careful not to dilate so far you overlap regions for blanks in the foreground mask. (See the project reading for more details.)
As before, you will be assessed using a randomly chosen selection from the three test images, so it is a good idea to test your algorithm on each of them in MATLAB.
% Read the test coin image.
testCoinImage = imread("testCoinImage3.png");
imshow(testCoinImage);
% Segment the coin image to get the testcoinMask and MaskedtestCoin.
[testcoinMask, MaskedtestCoin] = segmentCoin(testCoinImage);
% Shrink the coin mask to focus on the coin regions.
se = strel('disk', 20, 0);
testcoinMask = imfill(testcoinMask, 'holes');
testcoinMask = imerode(testcoinMask, se);
% Apply Gaussian filter to the masked coin image.
imgFilt = imgaussfilt(MaskedtestCoin, 0.5, 'Padding', 'circular', 'FilterDomain', 'frequency', 'FilterSize', 3);
% Detect edges using the Sobel operator.
faceEdgeMask = edge(imgFilt, 'sobel', 0.05, 'both');
% Eliminate edges outside the shrunken coin mask.
faceEdgeMask(~testcoinMask) = false;
% Dilate the faceEdgeMask to cover valid coin regions.
se_dilate = strel('disk', 20, 0);
dilated_faceEdgeMask = imdilate(faceEdgeMask, se_dilate);
% Combine the dilated faceEdgeMask with testcoinMask to get validCoinMask.
validCoinMask = testcoinMask & dilated_faceEdgeMask;
% Display the validCoinMask.
figure;
imshow(validCoinMask);
title('Valid Coin Mask');
% Segment Coin Function
function [testcoinMask, MaskedtestCoin] = segmentCoin(X)
X = im2gray(X);
testcoinMask = im2gray(X) > 200;
radius = 12;
decomposition = 4;
se = strel('disk', radius, decomposition);
testcoinMask = imclose(testcoinMask, se);
MaskedtestCoin = X;
MaskedtestCoin(~testcoinMask) = 0;
end
im ghetting the same error where im not able to mast all of the valid coins. please help me out here i have a deadline.
댓글 수: 5
Cris LaPierre
2023년 12월 25일
I'd also suggest asking your question in the course forum. You'll find the instructors are quite responsive, and will be more familiat with the techniques you are expected to use at this point in the course.
NABIL
2024년 9월 13일

Can anyone help solve the ‘Valid Coin Segmentation’ problem for the MathWorks certification?
댓글 수: 1
Cris LaPierre
2024년 9월 13일
Please ask your questions in the course forum. It is activiely monitored by the course instructors.
This question is locked.
참고 항목
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!