Download this file

59 lines (50 with data), 2.1 kB

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
function perimeter = ConvexPerim( bwImage )
STATS = regionprops(bwImage,'ConvexHull');
vertex = STATS.ConvexHull;
perimeter = 0;
if size(vertex,1) > 2
for i=2:size(vertex,1)
% if it is a vertical segment
if vertex(i,1)== vertex (i-1,1)
if vertex(i,2) ~= vertex(i-1,2)
perimeter = perimeter + abs( vertex(i,2) - vertex(i-1,2) );
end
end
% if it is a horizontal segment
if vertex(i,1)~= vertex (i-1,1)
if vertex(i,2) == vertex(i-1,2)
perimeter = perimeter + abs( vertex(i,1) - vertex(i-1,1) );
end
end
% if it is any other segment
if vertex(i,1)~= vertex (i-1,1)
if vertex(i,2) ~= vertex(i-1,2)
perimeter = perimeter + sqrt( (vertex(i,1) - vertex(i-1,1) )^2 + (vertex(i,2) - vertex(i-1,2) )^2 );
end
end
end
end
%Consider special cases, because very small objects have large
%errors, so call the facet midpoint method for these, and the exceptional
%cases will be handled correctly.
Icontour = bwboundaries(bwImage, 'noholes');
pixel = Icontour{1};
% If the shape is a single pixel, make the perimeter equal to the perimeter of
% a circle with area = 1
if size(pixel,1)== 2
perimeter = FacetMidpointPerim(bwImage);
end
%If the shape is two pixels aligned, make the perimeter equal to the perimeter
% of an eliipse with area = 2 and minor axis equal to 0.5.
if size(pixel,1) == 3 && ( pixel(1,1) == pixel(2,1) || pixel(1,2) == pixel(2,2) )
perimeter = 6.0774;
end
%If it is four pixels arranged as a square, make the perimeter equal to
%the periemter of a circle with area = 4
if(size(pixel, 1) == 5)
x = FacetMidpointPerim(bwImage);
if x == 7.089815;
perimeter = 7.089815;
end
end
end