I followed the example in How to detect light spots in image?
I now want to know the number of objects in result['center']
. I know I can iterate through it, but it feels like there exists a better way.
Naively, I tried using len(), assuming it was some kind of dict. However, that did not work. I also looked through the documentation of the measurement feature but did not find a clear way to do it.
Link to the documentation: https://diplib.org/diplib-docs/dip-Feature.html
Given result
from the linked answer, as the output of the dip.MeasurementTool.Measure()
function:
result = dip.MeasurementTool.Measure(labels, features=['Size', 'Center'])
then result.NumberOfObjects()
is the number of objects measured.
If you think of result
as a table, then result['Center']
is a column from this table. This object also has a NumberOfObjects()
method.
You can also do result.Objects()
or result['Center'].Objects()
to get a list of object IDs measured. These are the values that you can use to index into either to get measurements for a specific object.
Here is the documentation for the C++ class dip::Measurement
, and dip::Measurement::IteratorFeature
(which is the type that result['Center']
is). The Python bindings behave similarly to the C++ classes, though there are a few things we tried to make more useful in a Python context.
help(result['Center'])
will show you which C++ methods are accessible from Python, and which operators are defined for the object. The next release of DIPlib will make the Python help more useful, I've recently managed to copy the first paragraph of each function's documentation to the Python bindings. Hopefully the documentation will continue to keep improving, it's currently a bit difficult to find your way as a new user.
An alternative is to convert the object to a NumPy array. You can do so without copying the data: np.asarray(result)
or np.asarray(result['Center'])
.
Note that you can apply NumPy functions to these objects without first converting to a NumPy array, as they expose a buffer that NumPy directly can use: np.mean(result['Center'])
.
You can also translate the Measurement
object to a Pandas DataFrame
: result.ToDataFrame()
.
I'll add an overload of len()
to these objects. Thanks!