Counting coins on a greyscale image - using morphological and/or f transforms
Show older comments

I am trying to convert the image into a binary image, using the function form: function C = coins2bw(A) where A is a 2D grayscale image variable and C is a 2D binary image variable. The output image C should show the coins as filled in round disks with no other arifacts or stray foreground pixels(background in black while the coins are white). Using only morphological or fourier transforms.
Accepted Answer
More Answers (1)
Image Analyst
on 19 Nov 2020
I know you've already accepted an answer and got it solved already, but here's how I'd start:
clc; % Clear the command window.
close all; % Close all figures (except those of imtool.)
imtool close all; % Close all imtool figures if you have the Image Processing Toolbox.
clear; % Erase all existing variables. Or clearvars if you want.
workspace; % Make sure the workspace panel is showing.
format long g;
format compact;
fontSize = 16;
%===============================================================================
% Read in gray scale image.
folder = pwd;
baseFileName = 'coins.png';
% Get the full filename, with path prepended.
fullFileName = fullfile(folder, baseFileName);
if ~exist(fullFileName, 'file')
% Didn't find it there. Check the search path for it.
fullFileName = baseFileName; % No path this time.
if ~exist(fullFileName, 'file')
% Still didn't find it. Alert user.
errorMessage = sprintf('Error: %s does not exist.', fullFileName);
uiwait(warndlg(errorMessage));
return;
end
end
grayImage = imread(fullFileName);
% Get the dimensions of the image. numberOfColorBands should be = 1.
[rows, columns, numberOfColorBands] = size(grayImage);
% If it's RGB instead of grayscale, convert it to gray scale.
if numberOfColorBands > 1
grayImage = rgb2gray(grayImage);
end
% Display the original image.
subplot(2, 2, 1);
imshow(grayImage);
axis on;
impixelinfo; % Let user mouse around and see gray level.
caption = sprintf('Original Image : %s', baseFileName);
title(caption, 'FontSize', fontSize);
impixelinfo;
% Enlarge figure to full screen.
set(gcf, 'Units', 'Normalized', 'Outerposition', [0, 0.1, 1, 0.9]);
% Display the histogram
subplot(2, 2, 2);
imhist(grayImage);
grid on;
% Specify a threshold.
threshold = 181
% Place line on histogram at the mean.
xline(threshold, 'Color', 'r', 'LineWidth', 2);
% Create a binary image
mask = grayImage < threshold;
mask = imfill(mask, 'holes');
subplot(2, 2, 3);
imshow(mask);
title('mask', 'FontSize', fontSize);
% Now call imerode to get them to not touch.
% Then call bwlabel to count them.
4 Comments
Stephan
on 19 Nov 2020
Is there a way to find circles and estimate the diameters of those circles by using only functions considering the given restrictions?
Image Analyst
on 20 Nov 2020
No. You need something other than only morphological functions like imclose(), imerode(), imdilate(), bwareaopen(), etc. None of those would give you the area (count of pixels) or the diameter. You'd need something like regionprops() or at a minimum the sum() function.
kandlev
on 20 Nov 2020
Image Analyst
on 20 Nov 2020
Once you have the binary image mask of just the coins, then you do
[labeledImage, numRegions] = bwlabel(mask);
% Then if you want to find all the areas and centroids:
props = regionprops(labeledImage, 'Area', 'Centroid');
allAreas = [props.Area]
xy = vertcat(props.Centroid); % List of centroids' (x, y) coordinates.
See my Image Segmentation Tutorial if you want a well commented tutorial:
Categories
Find more on Morphological Operations 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!