cant detect valid coins and segement them fully, have a deadline in 4 days

8 views (last 30 days)
% Q)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.
testImageIdx = randi([1,3])
testCoinImage = imread("testCoinImage"+testImageIdx+".png");
[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 the entire coin region.
se = strel('disk', 60, 0);
faceEdgeMask = imdilate(faceEdgeMask, se);
% Combine the dilated faceEdgeMask with the original testcoinMask to get the validCoinMask.
validCoinMask = faceEdgeMask & testcoinMask;
% Display the validCoinMask.
figure, imshow(validCoinMask), title('Valid Coin Mask');
function [BW, maskedImage] = segmentCoin(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
  2 Comments
John D'Errico
John D'Errico on 24 Dec 2023
That you need a solution to this immediately is only a reason why you need to learn better time management in the future. If you get a solution soon, that is good for you, but not our problem.

Sign in to comment.

Accepted Answer

DGM
DGM on 25 Dec 2023
Edited: DGM on 25 Dec 2023
Hm. Std alone seems a bit marginal given that some of the valid coins are quite overexposed and some of the painted coins have some remaining texture. This is what I did. It works for the three test images given, but it's probably still not very robust.
The masking is determined by intensity, whereas blob rejection is determined by mean gradient magnitude.
% a test image
testCoinImage = imread('testCoinImage3.png');
% get the mask
testcoinMask = segmentCoin(testCoinImage);
% for visualization, combine the mask and image
outpict = imoverlay(testCoinImage,testcoinMask,[1 0 0.5]);
imshow(outpict,'border','tight')
function BW = segmentCoin(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);
% get edge information
G = imgradient(im2double(inputImage));
% Measure areas and perimeters.
labeledImage = bwlabel(BW);
coinProps = regionprops(BW, G, 'MeanIntensity');
edginess = vertcat(coinProps.MeanIntensity)
% Get a mask of only where the average gradient is high
BW = ismember(labeledImage, find(edginess >= 0.3));
end
  5 Comments
Ashutosh
Ashutosh on 25 Dec 2023
testImageIdx = randi([1,3])
testCoinImage = imread("testCoinImage"+testImageIdx+".png");
% get the mask
testcoinMask = segmentCoin(testCoinImage);
% for visualization, combine the mask and image
validCoinMask = imoverlay(testCoinImage,testcoinMask,[1 0 0.5]);
validCoinMask = logical(validCoinMask );
imshow(validCoinMask,'border','tight')
function BW = segmentCoin(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);
% get edge information
G = imgradient(im2double(inputImage));
% Measure areas and perimeters.
labeledImage = bwlabel(BW);
coinProps = regionprops(BW, G, 'MeanIntensity');
edginess = vertcat(coinProps.MeanIntensity)
% Get a mask of only where the average gradient is high
BW = ismember(labeledImage, find(edginess >= 0.3));
end
DGM
DGM on 25 Dec 2023
No. You shouldn't need to cast anything as logical.
In the code I gave, outpict is a composite RGB image showing where the mask covers the original image. That's the wrong variable, and imshow() doesn't know what to do with a multichannel logical array. It's just there for visualization.
Rename testcoinMask to validCoinMask. It's already a logical array.
% get the mask
validCoinMask = segmentCoin(testCoinImage);
% for visualization, combine the mask and image
outpict = imoverlay(testCoinImage,validCoinMask,[1 0 0.5]);
I forgot that your assignment was picky about the specific names.

Sign in to comment.

More Answers (1)

Image Analyst
Image Analyst on 24 Dec 2023
Edited: Image Analyst on 24 Dec 2023
Looks like it should work. What values are in allSD? Maybe you need to pick a different threshold than 15. How are they determining valid coins? You get 10 coins plus probably some noise, then with bwareaopen you end up with only the large blobs, which are coins. Aren't they all valid? Or are only some of them valid? How do you tell? From the diameter? From the variation in intensity?
  1 Comment
Ashutosh
Ashutosh on 24 Dec 2023
see the valid coins are the ones that have not been tamepered with here, which show a face of some person, others are invalid, i cannot segment the coins to it outermost boundaries, i need to segment the valid coins only here and fully segment them , i cannot segment them fully here

Sign in to comment.

Products


Release

R2023b

Community Treasure Hunt

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

Start Hunting!