ValueError: Images of type float must be between -1 and 1.

1,324 views
Skip to first unread message

Christopher

unread,
Feb 8, 2016, 8:38:14 PM2/8/16
to scikit-image
Hi, I am having a problem trying to create an image object from an array. The problem is the array extends beyond -1 and 1. The pixel values are raw 32bit float values from a detector.


array.dtype
float32


summary(array)
Mean:    0.00656827

Min:    -6.1125
Median:    1.09516e-08
Max:    3.69815



image = skimage.img_as_float(array)



  File "/home/user/anaconda2/lib/python2.7/site-packages/skimage/util/dtype.py", line 291, in img_as_float
    return convert(image, np.float64, force_copy)
  File "/home/user/anaconda2/lib/python2.7/site-packages/skimage/util/dtype.py", line 195, 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


I am using version 0.11.3 via anaconda on fedora 23


Sometimes I can get img_as_float to work if I use astype on the array first, but then it doesn't seem to change the values, and then skimage.io.Image() will fail with a similar error

Thanks in advance for any help.

Christopher

unread,
Feb 8, 2016, 8:40:14 PM2/8/16
to scikit-image
An update, the values are not 32 bit from the detector, the detector is 16bit. But the images have been aligned by a previous program, so they are interpolated to 32 bit precision.

Juan Nunez-Iglesias

unread,
Feb 9, 2016, 1:32:43 AM2/9/16
to scikit...@googlegroups.com
Hi Christopher!

In some cases, skimage has to assume a certain input range for images, so that things like display brightness and type conversions are consistent between images. In cases like yours, this can get in the way.

A few questions:

- why are you using img_as_float at all, if your image is already float?
- if img_as_float is being called by a particular function that you need, what is that function?
- Does it have a `preserve_range` keyword argument? If so, try setting it to True.

If it turns out that you're using a valid function for input of arbitrary ranges and still getting an error, that's a bug and we should raise an issue on github.

Let us know!

Juan.

PS: If at all possible, you should think about switching to Python 3 for your main environment. =)

--
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...@googlegroups.com.
To post to this group, send email to scikit...@googlegroups.com.
To view this discussion on the web, visit https://groups.google.com/d/msgid/scikit-image/d8581def-a4fd-4b4c-bcab-71abfc8fabca%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Christopher

unread,
Feb 9, 2016, 2:23:41 PM2/9/16
to scikit-image
Hi! Thanks for the reply

The reason I wanted to use img_as_float is because I eventually plan to use many functions and I thought I should get the image into a format that scikit-image likes before proceeding. However, as you suggested, the function I want to use immediately is transform.rotate() which does have a preserve scale option, and that did fix my problem. Should I just use this on every function?

Stéfan van der Walt

unread,
Feb 9, 2016, 4:34:29 PM2/9/16
to scikit-image
Hi Christopher
If you know the range of your input data, then you could rescale the
image to be between -1 and 1:

exposure.rescale_intensity(data, in_range=(-10, 10))

From there on, you should have no problems. If, however, you need to
preserve your original scale, you can either use `preserve_range` or
rescale your image afterwards. The latter approach is dangerous,
though, since you don't know how the range of values get influenced by
the operations you performed on your image.

Regards
Stéfan

Christopher

unread,
Feb 10, 2016, 2:15:45 PM2/10/16
to scikit-image

Ok thanks, exposure.rescale_intensity(data, in_range=(-1, 1)) seems to do the trick. Was -10,10 a typo? Is there any concern for loss of information when doing a rescale?

Juan Nunez-Iglesias

unread,
Feb 10, 2016, 5:06:40 PM2/10/16
to scikit...@googlegroups.com
Um, yeah, I think you're probably clipping your data by doing `in_range=(-1, 1)`. That means that the function will consider any values under -1 as -1 and any values above 1 as 1. Probably not what you want! The input range should cover the entire range of possible values in your data.

On Thu, Feb 11, 2016 at 6:15 AM, Christopher <spa...@gmail.com> wrote:

Ok thanks, exposure.rescale_intensity(data, in_range=(-1, 1)) seems to do the trick. Was -10,10 a typo? Is there any concern for loss of information when doing a rescale?

--
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...@googlegroups.com.
To post to this group, send email to scikit...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages