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

40 次查看(过去 30 天)
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.

采纳的回答

Stephan
Stephan 2020-11-10
编辑:Stephan 2020-11-10
I = imread('image.png');
I = imadjust(I);
[centers,radii] = imfindcircles(I,[15 75],'ObjectPolarity','dark','Sensitivity',0.85);
BW = false(size(I,1),size(I,2));
[Xgrid,Ygrid] = meshgrid(1:size(BW,2),1:size(BW,1));
for n = 1:size(centers,1)
BW = BW | (hypot(Xgrid-centers(n,1),Ygrid-centers(n,2)) <= radii(n));
end
maskedImage = I;
maskedImage(~BW) = 0;
imshow(BW)
fprintf('Numbers of coins: %d\n',size(centers,1))
  8 个评论
kandlev
kandlev 2020-11-19
编辑:kandlev 2020-11-19
I don't think so, we're exclusively being asked to use grayscale morphology :(
We were given these dimensions also, but no where in our current study section does it mention 'imfindcircles'.
Could you use a 'disk' structuring element?

请先登录,再进行评论。

更多回答(1 个)

Image Analyst
Image Analyst 2020-11-19
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 个评论
kandlev
kandlev 2020-11-20
how do I call bwlabel? I havent used that before. Does it have something to do with one of my requirements? --------->
%required:
A is a coin image and where coinV is a row vector with the following form:
coinV = [Npennies Nnickels Ndimes Nquarters];
Image Analyst
Image Analyst 2020-11-20
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:

请先登录,再进行评论。

标签

Community Treasure Hunt

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

Start Hunting!

Translated by