raster_geometry icon indicating copy to clipboard operation
raster_geometry copied to clipboard

raster_geometry.sphere is giving different volumes when only the position changes

Open dorkylever opened this issue 2 years ago • 2 comments

Hi All,

I have a set of midpoints (from labelled tumours within a set of scans [512x512x512 voxels]). I'm just trying to create a sphere with a radius of 10 voxels at each midpoint. To do this, I'm using raster_geometry.sphere and defining the position as the midpoint/512 (i.e. position = midpoint/ 512 , raster_geometry.sphere(512, 10, positon) )

When I'm doing this, however, the volume of the sphere changes. Is there something I don't understand about raster_geometry?

from logzero import logger as logging
from lama import common
import numpy as np
import SimpleITK as sitk
import raster_geometry as rg

for i, img_path in enumerate(scan_paths):
    logging.info(img_path)
    logging.info(tumour_paths[i])

    m_loader = common.LoadImage(tumour_paths[i])
    mask = m_loader.img

    m_array = sitk.GetArrayFromImage(mask)

    s = ndimage.find_objects(m_array)[-1]

    midpoint = [(np.mean([s[0].start, s[0].stop]))/512,
                (np.mean([s[1].start, s[1].stop]))/512,
                (np.mean([s[2].start, s[2].stop]))/512]

    print("Original Midpoint", [i*512 for i in midpoint])

    print("Modified midpoint", midpoint)

    arr = rg.sphere(512, 10, midpoint).astype(np.int_)

    print(np.count_nonzero(arr))
    print(np.sum(arr))

Example Output:

[I 220525 17:37:58 radiomics_normaliser:86] E:\220204_BQ_dataset\220521_BQ_norm\imgs\200721_MPTLVo3_GFSeeds_4T1R_4T1R_D7_C1_002.nrrd
[I 220525 17:37:58 radiomics_normaliser:87] E:\220204_BQ_dataset\220521_BQ_norm\tumour_respaced\200721_MPTLVo3_GFSeeds_4T1R_4T1R_D7_C1_002.nrrd
Original Midpoint [260.5, 252.5, 162.0]
Modified midpoint [0.5087890625, 0.4931640625, 0.31640625]
4160
4160
[I 220525 17:38:14 radiomics_normaliser:86] E:\220204_BQ_dataset\220521_BQ_norm\imgs\200721_MPTLVo3_GFseeds_4T1R_4T1R_D7_C1_003.nrrd
[I 220525 17:38:14 radiomics_normaliser:87] E:\220204_BQ_dataset\220521_BQ_norm\tumour_respaced\200721_MPTLVo3_GFseeds_4T1R_4T1R_D7_C1_003.nrrd
Original Midpoint [219.5, 234.5, 165.5]
Modified midpoint [0.4287109375, 0.4580078125, 0.3232421875]
4165
4165

dorkylever avatar May 25 '22 07:05 dorkylever

adding smoothing=True and rounding the midpoint before dividing by 512 seemed to improve the results.

dorkylever avatar May 25 '22 08:05 dorkylever

This is a limitation of the underlying mathematical model related to the rasterization of an arbitrarily precise object to a spatially quantized grid. This is especially true for binary masks. You should expect an error the size of the skin of the object. A way of estimating this error is to compute the volume of the object with a given size and the same object with size - 1. The smoothing parameter has been introduced specifically to mitigate this effect. The idea is to provide a different weighting to the pixels in the skin so that the sum will give an estimate of the area closer to that of the actual object and independent of the position within the grid. I reckon this is not perfect, but if you have suggestions, please feel free to propose them.

norok2 avatar May 29 '22 06:05 norok2