Reputation: 511
I am try to determine and count the number of WBCs inside an image, I can determine it using below code. But appears when I am try to labeling the detected WBCs it is labeling the numbers outside the circles. The result is attached below.
Can you please tell me what I am missing here ?
clc;
clear all;
close all;
rgb = imread('test.jpg');
figure;
imshow(rgb);
binaryImage=im2bw(rgb);
bw = im2bw(rgb, graythresh(rgb));
imshow(bw)
L = bwlabel(bw);
imshow(rgb) hold on
[centers, radii, metric] = imfindcircles(rgb,[9 24],'ObjectPolarity','dark','Sensitivity',0.86,'Method','twostage');
m = viscircles(centers,radii);
[a,b]=size(centers);
disp(a);
disp(b);
s = regionprops(L, 'Centroid');
for k = 1:numel(s)
c = s(k).Centroid;
text(c(1), c(2), sprintf('%d', k), ...
'Color', 'w', ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'middle');
end
hold off
Upvotes: 2
Views: 166
Reputation: 35525
You are a bit confused.
Your approach is:
1) compute the circles, and plot them
2) compute the circles with a completely different approach, and put labels there
Wouldn't it make sense you use the result from the first approach to write the labels? You have a line of code that says:
[centers, radii, metric] = imfindcircles(rgb,[9 24],'ObjectPolarity','dark','Sensitivity',0.86,'Method','twostage');
Note, how the first thing written there, is centers
, which hints that that is a fantastic thing to use to well, plot centers!
Just remove all your binary image stuff and write:
clc;
clear all;
close all;
rgb = imread('https://i.sstatic.net/WXpjH.jpg');
figure;
imshow(rgb); hold on
[centers, radii, metric] = imfindcircles(rgb,[9 24],'ObjectPolarity','dark','Sensitivity',0.86,'Method','twostage');
m = viscircles(centers,radii);
[a,b]=size(centers);
disp(a);
disp(b);
% s = regionprops(L, 'Centroid');
for k = 1:size(centers,1)
c = centers(k,:);
text(c(1), c(2), sprintf('%d', k), ...
'Color', 'w', ...
'HorizontalAlignment', 'center', ...
'VerticalAlignment', 'middle');
end
hold off
Upvotes: 3