clear all;
close all;
clc;
img = imread('scan0016.jpg');
figure;
imshow(img);
[img,rect]= imcrop(img);%imtool(img,[]);
%img=imrotate(img,-3);
img = medfilt2(img);
img = imadjust(img);
figure;
imshow(img)
I = im2bw(img);
figure
imshow(I)
I = ~I;
I=imfill(I,'holes');
se = strel('disk',1);
I = imerode(I,se);
figure;
imshow(I)
[B,L] = bwboundaries(I,'noholes');
imshow(label2rgb(L, @jet, [.5 .5 .5]));
hold on
p_size = 210/2480;
for k = 1:length(B)
boundary = B{k};
plot(boundary(:,2), boundary(:,1), 'w', 'LineWidth', 2)
end
stats = regionprops(L,'Area','Centroid','EquivDiameter','BoundingBox');
threshold = 0.85;
d = 1;
e = 1;
for k = 1:length(B)
% obtain (X,Y) boundary coordinates corresponding to label 'k'
boundary = B{k};
% compute a simple estimate of the object's perimeter
delta_sq = diff(boundary).^2;
perimeter = sum(sqrt(sum(delta_sq,2)));
% obtain the area calculation corresponding to label 'k'
area = stats(k).Area;
if area > 10
% compute the roundness metric
metric = 4*pi*area/perimeter^2
% display the results
metric_string = sprintf('%2.2f',metric);
if metric > threshold
centroid = stats(k).Centroid;
plot(centroid(1),centroid(2),'ko');
diameter(d) = stats(k).EquivDiameter*p_size;
formatSpec = 'The diameter of the object is %f.';
str = sprintf(formatSpec,diameter(d));
disp(str);
d = d + 1;
else
bbox = stats(k).BoundingBox*p_size;
corner_x(e) = bbox(1);
corner_y(e) = bbox(2);
length(e)= bbox(3);
width(e)= bbox(4);
formatSpec = 'The length and width of the object is %f & %f.';
str = sprintf(formatSpec,length(e),width(e));
disp(str)
e = e + 1;
end
end
end