[Insight-users] Truncating floating point in SimpleITK

David Welch david.m.welch at gmail.com
Thu Jun 27 13:27:25 EDT 2013


Hey all,

I have some images that represent probability (values between 0.0 and 1.0)
and I'd like to drop the unneeded precision from the Float32 values.
Here's my naive approach:

def truncate(image, ndigits=0):
    """ Truncate to the given number of digits """
    factor = 10 ** ndigits
    scaled = image * factor
    casted = sitk.Cast(scaled, sitk.sitkUInt32)
    truncated = sitk.Cast(casted, image.GetPixelIDValue()) / float(factor)
    return truncated

## Test
test = truncate(img, 4)
print img[128,188,196]
print test[128,188,196]

0.700903475285
0.700900018215

What am I doing wrong?  Any ideas?


Cheers,

Dave W.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20130627/9d73755a/attachment.htm>


More information about the Insight-users mailing list