How can i seperate connected objects in binary image?
Show older comments
code:
img = (imread('coins.jpg'));
img_gray = rgb2gray(img);
% Filter image for easier edge detection
m = 12;
n = 12;
img_filter = imfilter(img_gray, fspecial('log',[10 10],1));
%figure, imshow(img_filter), title('f')
% Edge detection
[~, threshold] = edge(img_filter, 'canny');
fudgeFactor = 2;
img_edge = edge(img_filter, 'canny', threshold * fudgeFactor );
figure, imshow(img_edge), title('edge detection')
% Dilate image to make the coin edges complete without holes
se_disk = strel('disk',4);
se_line1 = strel('line',3,100);
se_line2 = strel('line',3,100);
img_dilated = imdilate(img_edge, se_disk);
img_dilated = imdilate(img_dilated, [se_line1 se_line2]);
figure, imshow(img_dilated), title('dilate')
% Remove small objects (noise) and fill complete objects
img_clearborder = imclearborder(img_dilated, 4);
%figure, imshow(BWclear), title('cleared border image');
img_fill = imfill(img_clearborder, 'holes');
figure, imshow(img_fill), title('fill holes')
% Erode image to make a clear cut between objects
se_diamond = strel('diamond',2);
img_erode = imerode(img_fill,se_diamond);
for k=1:3
img_erode = imerode(img_erode,se_diamond);
end
img_nosmall = bwareaopen(img_erode,300);
figure, imshow(img_nosmall), title('erode')
[B, L] = bwboundaries(img_nosmall);
%ltemp = (L == 1);
se_diamond1 = strel('disk',15);
img_erode1 = imerode(L,se_diamond1);
figure, imshow(label2rgb(img_erode1, @jet, [.5 .5 .5])), title('boundaries')
hold on
stats = regionprops(img_erode1,img(:,:,1),...
'Area','Centroid','Orientation','EquivDiameter','MeanIntensity');
threshold = 0.8; % For differentiating coins from matches based on an objects circularity
coinCentroids = [];
coinIntensities = [];
matchCentroids = [];
matchAngles = [];
coinRatios = [];
for k = 1:length(B)
boundary = B{k};
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
area = stats(k).Area;
metric = 4*pi*area/perimeter^2;
metric_string = sprintf('%2.2f',metric);
angle_string = sprintf('%2.2f',stats(k).Orientation);
centroid = stats(k).Centroid;
if metric > threshold
% Object is round, therefore a coin
coinCentroids = [coinCentroids; centroid];
coinIntensities = [coinIntensities; stats(k).MeanIntensity];
coinRatios = [coinRatios; stats(k).EquivDiameter/area];
else
% Object is a match
angle = stats(k).Orientation;
matchCentroids = [matchCentroids; centroid];
matchAngles = [matchAngles; angle];
end
plot(centroid(1),centroid(2),'ko');
end
I attached the input image, the "result image" (it has to be look like this) and the image where i am. My question: How can I separate that 3 objects to 6?
Answers (1)
Image Analyst
on 10 Apr 2016
0 votes
Use imfindcircles(). See Brett's demo of it: http://www.mathworks.com/matlabcentral/fileexchange/34365-circle-finder
1 Comment
NemVagyokBot
on 11 Apr 2016
Categories
Find more on Object Analysis in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!