分离重叠对象 MATLAB

Separate overlapping objects MATLAB

我有这张图片

我需要计算图像中硬币的数量,但我遇到了重叠硬币的问题,我正在使用此代码来执行此操作:

seg = imread('img.jpg');
lvl = graythresh(seg);
seg = imbinarize(seg,lvl);
seg = imfill(~seg,'holes');
[centres, radii, metric] = imfindcircles(seg, [30, 90]);
imshow(seg);
[l,c] = size(radii);

hold on;

plot(centres(:,1), centres(:,2), 'r*'); 
viscircles(centres, radii, 'EdgeColor', 'b'); 

这是我画圆后得到的输出 我该怎么做?

首先,您可以使用Canny边缘检测方法找到所有边缘。 其次,最好使用高灵敏度值来找到所有类似圆形的形状,然后限制您的条件以确定正确的圆圈数。例如,您可以利用圆心的距离(或圆的相关性)。这是我的代码,用于确定给定图像中硬币的数量。

clc;clear all;close all;
%% Preporocessing
input = imread('img.jpg');
input_edges = edge(input,'Canny');
figure;imshow(input_edges);
input_edges=medfilt2(input_edges,[2 2]);% with this filter size, the edges can be strengthened!
figure;imshow(input_edges);
input_edges=bwareaopen(input_edges,130);%remove small edges
%% Applying  circular Hough transform
[centres, radii, metric] = imfindcircles(input_edges, [35, 90],...
    'ObjectPolarity','bright','Sensitivity',0.9,'EdgeThreshold',0.1);
figure;imshow(input_edges);
[l,c] = size(radii);
hold on;
plot(centres(:,1), centres(:,2), 'r*');
viscircles(centres, radii, 'EdgeColor', 'b');
%% Distinguishing circlar shadow
x_centres=centres(:,1);
y_centres=centres(:,2);
num=size(centres,1);
centre_dists = sqrt( bsxfun(@minus,centres(:,1),centres(:,1)').^2 + ...
    bsxfun(@minus,centres(:,2),centres(:,2)').^2 );

% [x_idx,y_idx] =find(centre_dists<50 & centre_dists>0);
% sort(centre_dists(centre_dists<50 & centre_dists>0));
coin_nm=size(centres,1)-0.5*numel(find(centre_dists<50 & centre_dists>0));

得到输出如下: