Statistics using SimpleITK#
We can use SimpleITK for extracting features from 3D label images. For convenience reasons we use the napari-simpleitk-image-processing library.
import numpy as np
import pandas as pd
from skimage.data import cells3d
import stackview
from skimage import measure
from skimage import filters
import napari_simpleitk_image_processing as nsitk
# load image
image = cells3d()[:,1]
stackview.insight(image)
|
# denoising
blurred_image = filters.gaussian(image, sigma=1)
# binarization
threshold = filters.threshold_otsu(blurred_image)
thresholded_image = blurred_image >= threshold
# labeling
label_image = measure.label(thresholded_image)
# visualization
stackview.insight(label_image)
|
Measurements/ region properties#
We are now using the very handy function label_statistics
which provides a table of features. Let us check first what we need to provide for this function:
nsitk.label_statistics?
Signature:
nsitk.label_statistics(
intensity_image: 'napari.types.ImageData',
label_image: 'napari.types.LabelsData',
size: bool = True,
intensity: bool = True,
perimeter: bool = False,
shape: bool = False,
position: bool = False,
moments: bool = False,
napari_viewer: 'napari.Viewer' = None,
) -> 'pandas.DataFrame'
Docstring:
Measure intensity/shape/... statistics per label
Parameters
----------
intensity_image: ndarray, optional
Can be None
label_image: ndarray
Must be subsequently labeled
size: bool, optional
intensity: bool, optional
perimeter: bool, optional
shape: bool, optional
position: bool, optional
moments: bool, optional
napari_viewer: napari.Viewer, optional
Returns
-------
pandas DataFrame, in case napari_viewr is None, otherwise the DataFrame will be added to
the passed label_image's layer.features
See Also
--------
..[0] https://simpleitk.org/doxygen/latest/html/classitk_1_1simple_1_1LabelShapeStatisticsImageFilter
..[1] http://insightsoftwareconsortium.github.io/SimpleITK-Notebooks/Python_html/35_Segmentation_Shape_Analysis.html
File: c:\users\haase\mambaforge\envs\tea2024\lib\site-packages\napari_simpleitk_image_processing\_simpleitk_image_processing.py
Type: function
Feature categories which have are set to True
are measured by default. In this case, the categories are size
and intensity
. But the rest might be also interesting to investigate. So we need them to True
as well:
df = pd.DataFrame(nsitk.label_statistics(image, label_image,
size=True,
shape=True,
perimeter=True,
position=True))
df
label | maximum | mean | median | minimum | sigma | sum | variance | bbox_0 | bbox_1 | ... | equivalent_ellipsoid_diameter_0 | equivalent_ellipsoid_diameter_1 | equivalent_ellipsoid_diameter_2 | equivalent_spherical_perimeter | equivalent_spherical_radius | number_of_pixels | number_of_pixels_on_border | perimeter | perimeter_on_border | perimeter_on_border_ratio | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
0 | 1 | 65535.0 | 22623.940316 | 19071.708984 | 4126.0 | 12083.017961 | 7.471330e+08 | 1.459993e+08 | 33 | 58 | ... | 24.610818 | 46.274120 | 55.381770 | 4977.797687 | 19.902778 | 33024 | 523 | 7186.004933 | 523.0 | 0.072780 |
1 | 2 | 47610.0 | 16500.850114 | 16255.751953 | 4837.0 | 4418.271669 | 6.720961e+08 | 1.952112e+07 | 87 | 133 | ... | 29.888411 | 49.597182 | 52.476713 | 5724.894744 | 21.344148 | 40731 | 0 | 6172.236982 | 0.0 | 0.000000 |
2 | 3 | 55766.0 | 15183.665955 | 14463.779297 | 4932.0 | 4399.126377 | 1.414799e+09 | 1.935231e+07 | 51 | 192 | ... | 28.534707 | 51.817058 | 120.357525 | 9939.460710 | 28.123960 | 93179 | 1049 | 15144.119180 | 1049.0 | 0.069268 |
3 | 4 | 65298.0 | 19238.187210 | 18559.716797 | 4458.0 | 6757.679557 | 4.229708e+08 | 4.566623e+07 | 228 | 195 | ... | 29.207059 | 30.203761 | 47.599101 | 3795.318904 | 17.378777 | 21986 | 1157 | 4286.533941 | 1157.0 | 0.269915 |
4 | 5 | 36134.0 | 13131.887959 | 12927.802734 | 5453.0 | 2797.962330 | 1.357010e+09 | 7.828593e+06 | 126 | 0 | ... | 27.940183 | 51.292200 | 137.713543 | 10649.307251 | 29.110908 | 103337 | 785 | 21514.657299 | 785.0 | 0.036487 |
5 | 6 | 34332.0 | 12687.766570 | 12415.810547 | 5833.0 | 2613.257575 | 4.404812e+08 | 6.829115e+06 | 86 | 48 | ... | 28.520444 | 40.164739 | 57.881841 | 5146.503243 | 20.237236 | 34717 | 0 | 8387.413862 | 0.0 | 0.000000 |
6 | 7 | 41113.0 | 15498.465402 | 15231.767578 | 5643.0 | 3879.807500 | 7.140453e+08 | 1.505291e+07 | 10 | 120 | ... | 30.029330 | 41.504116 | 70.599501 | 6215.014814 | 22.239046 | 46072 | 0 | 7250.531028 | 0.0 | 0.000000 |
7 | 8 | 42394.0 | 13376.216498 | 12927.802734 | 5880.0 | 3092.779705 | 4.901982e+08 | 9.565286e+06 | 27 | 158 | ... | 29.605400 | 44.627579 | 52.974339 | 5335.515795 | 20.605505 | 36647 | 0 | 7516.259868 | 0.0 | 0.000000 |
8 | 9 | 55624.0 | 15445.162122 | 14975.771484 | 3983.0 | 4369.205585 | 7.239456e+08 | 1.908996e+07 | 144 | 176 | ... | 27.890772 | 51.601386 | 62.200368 | 6286.753725 | 22.367029 | 46872 | 0 | 7703.261028 | 0.0 | 0.000000 |
9 | 10 | 45903.0 | 12296.049111 | 12159.814453 | 5169.0 | 2351.130239 | 3.923300e+08 | 5.527813e+06 | 0 | 0 | ... | 27.589543 | 42.667873 | 51.765670 | 4864.909579 | 19.675802 | 31907 | 910 | 8026.035532 | 926.0 | 0.115375 |
10 | 11 | 34617.0 | 12775.336218 | 12415.810547 | 6022.0 | 2542.662952 | 4.295962e+08 | 6.465135e+06 | 54 | 13 | ... | 29.709700 | 37.358972 | 57.862384 | 5038.209321 | 20.023186 | 33627 | 0 | 6971.419828 | 0.0 | 0.000000 |
11 | 12 | 39406.0 | 14132.811306 | 13695.791016 | 5596.0 | 3229.607077 | 5.340224e+08 | 1.043036e+07 | 137 | 73 | ... | 31.075148 | 46.908817 | 49.506772 | 5445.503874 | 20.816806 | 37786 | 0 | 6298.712627 | 0.0 | 0.000000 |
12 | 13 | 39738.0 | 14336.412193 | 13951.787109 | 4837.0 | 3421.796926 | 6.751303e+08 | 1.170869e+07 | 220 | 78 | ... | 26.939097 | 34.084241 | 97.951670 | 6306.410182 | 22.401968 | 47092 | 1782 | 9251.046757 | 1782.0 | 0.192627 |
13 | 14 | 32910.0 | 14267.065307 | 13951.787109 | 5027.0 | 3382.211567 | 6.359402e+08 | 1.143936e+07 | 160 | 115 | ... | 27.654190 | 49.888746 | 61.704873 | 6079.556006 | 21.995356 | 44574 | 0 | 7637.852741 | 0.0 | 0.000000 |
14 | 15 | 25560.0 | 12693.643645 | 12159.814453 | 5975.0 | 2712.361825 | 1.668833e+08 | 7.356907e+06 | 184 | 233 | ... | 22.385207 | 23.324345 | 48.090298 | 2693.819960 | 14.641290 | 13147 | 814 | 5034.421312 | 814.0 | 0.161687 |
15 | 16 | 23378.0 | 11729.898405 | 11647.822266 | 6971.0 | 1929.340114 | 2.794062e+07 | 3.722353e+06 | 0 | 165 | ... | 8.035226 | 15.888670 | 35.633420 | 862.538778 | 8.284845 | 2382 | 480 | 1533.882378 | 480.0 | 0.312931 |
16 | 17 | 21624.0 | 12051.440492 | 11903.818359 | 6829.0 | 1909.310342 | 4.506034e+07 | 3.645466e+06 | 61 | 0 | ... | 9.119146 | 21.321118 | 36.727611 | 1164.987059 | 9.628433 | 3739 | 646 | 1806.519762 | 646.0 | 0.357594 |
17 | 18 | 18067.0 | 12642.775056 | 12671.806641 | 7113.0 | 1887.721974 | 5.676606e+06 | 3.563494e+06 | 42 | 252 | ... | 3.421169 | 12.457051 | 20.121389 | 283.562210 | 4.750280 | 449 | 201 | 467.703703 | 201.0 | 0.429759 |
18 | 19 | 18494.0 | 13171.954545 | 13183.798828 | 7777.0 | 2080.440088 | 4.346745e+06 | 4.328231e+06 | 247 | 0 | ... | 4.063350 | 9.274316 | 16.724348 | 230.936946 | 4.286884 | 330 | 172 | 326.481063 | 189.0 | 0.578900 |
19 | 20 | 12993.0 | 11074.950000 | 10879.833984 | 9626.0 | 866.009327 | 2.214990e+05 | 7.499722e+05 | 0 | 41 | ... | 1.500034 | 3.803307 | 6.695281 | 35.631775 | 1.683890 | 20 | 8 | 41.625299 | 8.0 | 0.192191 |
20 | 21 | 15412.0 | 11762.400000 | 11647.822266 | 8536.0 | 1432.674746 | 5.293080e+05 | 2.052557e+06 | 0 | 54 | ... | 2.590166 | 4.244004 | 7.818268 | 61.182300 | 2.206521 | 45 | 26 | 68.982937 | 26.0 | 0.376905 |
21 | 22 | 25607.0 | 12046.942658 | 11903.818359 | 6212.0 | 1987.248259 | 5.882522e+07 | 3.949156e+06 | 191 | 0 | ... | 12.177307 | 22.230537 | 34.449813 | 1391.901835 | 10.524449 | 4883 | 531 | 2100.978140 | 531.0 | 0.252739 |
22 | 23 | 22667.0 | 14137.542265 | 13951.787109 | 8014.0 | 2564.794476 | 8.864239e+06 | 6.578171e+06 | 252 | 14 | ... | 3.931732 | 14.779420 | 20.607609 | 354.265737 | 5.309574 | 627 | 258 | 555.992667 | 258.0 | 0.464035 |
23 | 24 | 13278.0 | 12313.333333 | 12671.806641 | 11096.0 | 1112.726981 | 3.694000e+04 | 1.238161e+06 | 0 | 75 | ... | 0.000000 | 0.000000 | 0.000000 | 10.059235 | 0.894700 | 3 | 3 | 8.070601 | 3.0 | 0.371720 |
24 | 25 | 15933.0 | 11565.902778 | 11391.826172 | 8868.0 | 1472.300884 | 8.327450e+05 | 2.167670e+06 | 0 | 206 | ... | 3.295460 | 4.895938 | 8.522794 | 83.696209 | 2.580762 | 72 | 28 | 94.930218 | 28.0 | 0.294953 |
25 | 26 | 15933.0 | 11984.942029 | 11903.818359 | 8962.0 | 1716.507911 | 8.269610e+05 | 2.946399e+06 | 0 | 61 | ... | 2.810671 | 4.741784 | 9.887779 | 81.354863 | 2.544408 | 69 | 38 | 99.583883 | 38.0 | 0.381588 |
26 | 27 | 11333.0 | 11333.000000 | 11391.826172 | 11333.0 | 0.000000 | 1.133300e+04 | 0.000000e+00 | 6 | 51 | ... | 0.000000 | 0.000000 | 0.000000 | 4.835976 | 0.620350 | 1 | 0 | 3.004080 | 0.0 | 0.000000 |
27 | 28 | 22288.0 | 15056.166667 | 14975.771484 | 9247.0 | 3374.279528 | 1.806740e+05 | 1.138576e+07 | 163 | 72 | ... | 2.203916 | 2.521246 | 4.124510 | 25.347684 | 1.420248 | 12 | 0 | 24.542458 | 0.0 | 0.000000 |
28 | 29 | 16977.0 | 14581.750000 | 16255.751953 | 11144.0 | 2606.129234 | 5.832700e+04 | 6.791910e+06 | 164 | 179 | ... | 0.000000 | 0.000000 | 0.000000 | 12.185896 | 0.984745 | 4 | 0 | 10.499264 | 0.0 | 0.000000 |
29 | 30 | 37747.0 | 16443.567164 | 13951.787109 | 6639.0 | 7418.631290 | 1.101719e+06 | 5.503609e+07 | 75 | 163 | ... | 3.595463 | 4.844137 | 7.346914 | 79.775094 | 2.519583 | 67 | 0 | 82.973966 | 0.0 | 0.000000 |
30 | 31 | 16834.0 | 14415.500000 | 16767.744141 | 11997.0 | 3420.275501 | 2.883100e+04 | 1.169828e+07 | 184 | 186 | ... | 0.000000 | 0.000000 | 0.000000 | 7.676633 | 0.781593 | 2 | 0 | 5.641937 | 0.0 | 0.000000 |
31 | 32 | 22430.0 | 15296.428571 | 16511.748047 | 10764.0 | 3278.686644 | 2.141500e+05 | 1.074979e+07 | 69 | 159 | ... | 2.160799 | 3.186080 | 3.883813 | 28.091148 | 1.495133 | 14 | 0 | 26.197575 | 0.0 | 0.000000 |
32 | 33 | 26271.0 | 15447.663462 | 15487.763672 | 8346.0 | 3648.568557 | 1.606557e+06 | 1.331205e+07 | 199 | 90 | ... | 5.098551 | 5.807702 | 6.707854 | 106.948086 | 2.917303 | 104 | 0 | 105.057726 | 0.0 | 0.000000 |
33 | 34 | 18873.0 | 15003.800000 | 14719.775391 | 12661.0 | 2099.312522 | 1.500380e+05 | 4.407113e+06 | 128 | 72 | ... | 2.144512 | 2.397638 | 3.714405 | 22.446612 | 1.336505 | 10 | 0 | 20.224982 | 0.0 | 0.000000 |
34 | 35 | 22667.0 | 15211.444444 | 14719.775391 | 7540.0 | 4278.261961 | 4.107090e+05 | 1.830353e+07 | 42 | 38 | ... | 2.196427 | 3.738460 | 6.279941 | 43.523783 | 1.861051 | 27 | 0 | 48.509499 | 0.0 | 0.000000 |
35 | 36 | 16834.0 | 14494.666667 | 14975.771484 | 12282.0 | 1758.626130 | 8.696800e+04 | 3.092766e+06 | 0 | 132 | ... | 0.000000 | 0.000000 | 0.000000 | 15.968040 | 1.127252 | 6 | 6 | 14.624144 | 6.0 | 0.410280 |
36 | 37 | 19585.0 | 19300.500000 | 19583.701172 | 19016.0 | 402.343758 | 3.860100e+04 | 1.618805e+05 | 243 | 69 | ... | 0.000000 | 0.000000 | 0.000000 | 7.676633 | 0.781593 | 2 | 0 | 5.641937 | 0.0 | 0.000000 |
37 | 38 | 14321.0 | 12139.500000 | 14207.783203 | 9958.0 | 3085.106886 | 2.427900e+04 | 9.517884e+06 | 59 | 0 | ... | 0.000000 | 0.000000 | 0.000000 | 7.676633 | 0.781593 | 2 | 2 | 5.641937 | 2.0 | 0.354488 |
38 rows × 31 columns
These are all columns that are available:
print(df.keys())
Index(['label', 'maximum', 'mean', 'median', 'minimum', 'sigma', 'sum',
'variance', 'bbox_0', 'bbox_1', 'bbox_2', 'bbox_3', 'bbox_4', 'bbox_5',
'centroid_0', 'centroid_1', 'centroid_2', 'elongation',
'feret_diameter', 'flatness', 'roundness',
'equivalent_ellipsoid_diameter_0', 'equivalent_ellipsoid_diameter_1',
'equivalent_ellipsoid_diameter_2', 'equivalent_spherical_perimeter',
'equivalent_spherical_radius', 'number_of_pixels',
'number_of_pixels_on_border', 'perimeter', 'perimeter_on_border',
'perimeter_on_border_ratio'],
dtype='object')
df.describe()
label | maximum | mean | median | minimum | sigma | sum | variance | bbox_0 | bbox_1 | ... | equivalent_ellipsoid_diameter_0 | equivalent_ellipsoid_diameter_1 | equivalent_ellipsoid_diameter_2 | equivalent_spherical_perimeter | equivalent_spherical_radius | number_of_pixels | number_of_pixels_on_border | perimeter | perimeter_on_border | perimeter_on_border_ratio | |
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
count | 38.000000 | 38.000000 | 38.000000 | 38.000000 | 38.000000 | 38.000000 | 3.800000e+01 | 3.800000e+01 | 38.000000 | 38.000000 | ... | 38.000000 | 38.000000 | 38.000000 | 38.000000 | 38.000000 | 38.000000 | 38.000000 | 38.000000 | 38.000000 | 38.000000 |
mean | 19.500000 | 29249.631579 | 14141.934960 | 14012.417763 | 7860.578947 | 3062.636210 | 2.624646e+08 | 1.371663e+07 | 96.631579 | 96.605263 | ... | 12.892735 | 20.459120 | 32.981898 | 2458.453815 | 10.268039 | 17830.736842 | 247.868421 | 3574.679397 | 248.736842 | 0.149901 |
std | 11.113055 | 14853.985729 | 2419.956877 | 2179.065205 | 3112.768027 | 2110.473638 | 3.820512e+08 | 2.467465e+07 | 84.994569 | 74.168012 | ... | 12.762268 | 19.699178 | 34.788721 | 3095.863466 | 9.625117 | 26189.733760 | 426.475003 | 4893.955404 | 427.080579 | 0.178247 |
min | 1.000000 | 11333.000000 | 11074.950000 | 10879.833984 | 3983.000000 | 0.000000 | 1.133300e+04 | 0.000000e+00 | 0.000000 | 0.000000 | ... | 0.000000 | 0.000000 | 0.000000 | 4.835976 | 0.620350 | 1.000000 | 0.000000 | 3.004080 | 0.000000 | 0.000000 |
25% | 10.250000 | 17249.500000 | 12300.370167 | 12223.813477 | 5607.750000 | 1914.317785 | 2.159872e+05 | 3.664688e+06 | 14.250000 | 42.750000 | ... | 2.169706 | 3.324175 | 4.663368 | 29.976304 | 1.542322 | 15.500000 | 0.000000 | 30.054506 | 0.000000 | 0.000000 |
50% | 19.500000 | 23022.500000 | 14135.176785 | 13951.787109 | 7042.000000 | 2662.809700 | 7.270422e+06 | 7.093011e+06 | 72.000000 | 74.000000 | ... | 4.580950 | 13.618236 | 20.364499 | 318.913973 | 5.029927 | 538.000000 | 2.500000 | 511.848185 | 2.500000 | 0.052877 |
75% | 28.750000 | 38991.250000 | 15204.499822 | 14975.771484 | 9531.250000 | 3421.416570 | 4.777690e+08 | 1.170609e+07 | 163.750000 | 162.000000 | ... | 27.831627 | 41.169272 | 52.849932 | 5119.429763 | 20.183723 | 34444.500000 | 424.500000 | 7132.358657 | 424.500000 | 0.308437 |
max | 38.000000 | 65535.000000 | 22623.940316 | 19583.701172 | 19016.000000 | 12083.017961 | 1.414799e+09 | 1.459993e+08 | 252.000000 | 252.000000 | ... | 31.075148 | 51.817058 | 137.713543 | 10649.307251 | 29.110908 | 103337.000000 | 1782.000000 | 21514.657299 | 1782.000000 | 0.578900 |
8 rows × 31 columns
Exercise#
Load data/blobs.tif
, segment the objects and measure feret_diameter
, flatness
, roundness
and elongation
. Compare the measurements with scikit-image.