Use best fit ellipse to calculate the crystal dimensions using the scikit-image toolbox. This is the default for
many/most computer vision tasks and is also used in my slicing model. It is crucial to use this in conjunction with the slicing model
otherwise additional errors will have to be taken into account.
| Parameters: |
-
img
(ndarray)
–
labelled object image - result of the segmentation algorithm.
-
sqrt_area
(bool, default:
True
)
–
Set to True if size measure is area^0.5, if False then length will be used. Defaults to True.
|
| Returns: |
-
size( ndarray
) –
Array with all segmented crystal sizes
-
–
aspect ratio (ndarray): Array with all segmented crystal aspect ratios
|
Source code in MinDet/Process/measure_funcs.py
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77 | def measure_ellipse(img, sqrt_area=True):
""" Use best fit ellipse to calculate the crystal dimensions using the scikit-image toolbox. This is the default for
many/most computer vision tasks and is also used in my slicing model. It is crucial to use this in conjunction with the slicing model
otherwise additional errors will have to be taken into account.
Args:
img (ndarray): labelled object image - result of the segmentation algorithm.
sqrt_area (bool, optional): Set to True if size measure is area^0.5, if False then length will be used. Defaults to True.
Returns:
size (ndarray): Array with all segmented crystal sizes
aspect ratio (ndarray): Array with all segmented crystal aspect ratios
"""
#generate properties objects
props = measure.regionprops(img)
area = []
major_axis = []
minor_axis = []
#cycle through all objects to get data below
for item in props:
if item.minor_axis_length > 0:
area.append(item.area)
major_axis.append(item.major_axis_length)
minor_axis.append(item.minor_axis_length)
else:
pass
#calculate aspect ratio
aspect_ratio = np.divide(major_axis,minor_axis)
#get the chosen size measure right
if sqrt_area == True:
size = np.sqrt(np.asarray(area))
else:
size = np.asarray(major_axis)
return size, aspect_ratio
|