img_as_float is confounding me

Tony Yu tsyu80 at gmail.com
Wed Dec 4 23:11:34 EST 2013


On Wed, Dec 4, 2013 at 2:38 PM, Josh Warner <silvertrumpet999 at gmail.com>wrote:

> Briefly, `img_as_float` assumes all inputs were properly scaled images of
> their reported dtype.
>
> If `img_as_float` is handed an image with the datatype `np.float64`, it is
> caught and the input image is returned without modification... no scaling
> is applied or attempted.
>
> Your input here appears to be a floating point array on the range [0.0,
> 777.0]. You will need to manually scale to the range [0, 1] - or, ideally,
> set the actual dtype (`np.int16` or `np.uint16`, in this case?) when you
> load your data. Then everything should work well.
>


You might find `rescale_intensity` helpful if you have data that should
automatically be linearly rescaled to the dtype limits. For example:


import numpy as np
from skimage.exposure import rescale_intensity

rescale_intensity(np.arange(1000, dtype=float))


Note however, that it's best practice to pass in an input range to rescale
intensity (by default, it uses the min and max of the input data). If
you're processing a series of images, you can't really tell signal from
noise if you're always stretching to the min/max of each image.

Best,
-Tony


>
>
> On Wednesday, December 4, 2013 2:16:10 PM UTC-6, Scott Classen wrote:
>>
>> I have four 2D numpy arrays. 100x100, 200x200, 300x300, 400x400
>>
>> I want to use pyramid_reduce on the 200x200, 300x300, and 400x400 arrays
>> to downsample them to 100x100 arrays, then add them all together.
>>
>> The arrays are in a list and I'm looping through the list (looping code
>> left out for clarity) calculating binned images for each one:
>>
>> g.binned = img_as_float(g.cropped)# no reducing here, just converting to
>> scikit compatable float…I hope.
>> g.binned = pyramid_reduce(img_as_float(g.cropped), downscale=2)
>> g.binned = pyramid_reduce(img_as_float(g.cropped), downscale=3)
>> g.binned = pyramid_reduce(img_as_float(g.cropped), downscale=4)
>>
>> The g.cropped input arrays originated from numpy arrays with dtype=float64
>>
>> I then print some useful information about my 4 arrays:
>>
>> array data type: float64 shape: (100, 100) nanmax: 777.0
>> array data type: float64 shape: (100, 100) nanmax: 1.0
>> array data type: float64 shape: (100, 100) nanmax: 1.0
>> array data type: float64 shape: (100, 100) nanmax: 1.0
>>
>>
>> I'm curious why the g.binned array from g.binned =
>> img_as_float(g.cropped)has not been scaled to 0,1? The others which have
>> been through the pyramid_reduce function have apparently been scaled, but
>> maybe not by the img_as_float routine, but by pyramid_reduce?
>>
>> I then add the arrays
>>
>> tmp_image = list[0].binned + list[1].binned + list[2].binned +
>> list[3].binned
>>
>> However, because the first array has not been properly scaled to 0,1 I
>> get an error when I run the tmp_image through img_as_uint so I can write
>> out my binary image file:
>>
>> Traceback (most recent call last):
>>   File "./test.py", line 318, in <module>
>>     image_to_write = img_as_uint(tmp_image)
>>   File "/sw/lib/python2.7/site-packages/skimage/util/dtype.py", line
>> 310, in img_as_uint
>>     return convert(image, np.uint16, force_copy)
>>   File "/sw/lib/python2.7/site-packages/skimage/util/dtype.py", line
>> 191, in convert
>>     raise ValueError("Images of type float must be between -1 and 1.")
>> ValueError: Images of type float must be between -1 and 1.
>>
>>
>> Any advice would be most appreciated.
>>
>>
>>
>>
>>
>>
>>
>  --
> You received this message because you are subscribed to the Google Groups
> "scikit-image" group.
> To unsubscribe from this group and stop receiving emails from it, send an
> email to scikit-image+unsubscribe at googlegroups.com.
> For more options, visit https://groups.google.com/groups/opt_out.
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-image/attachments/20131204/1e09b156/attachment.html>


More information about the scikit-image mailing list