[scikit-image] image type issue and failed conversion

Egor Panfilov egor.v.panfilov at gmail.com
Thu Dec 29 05:16:21 EST 2016


Hi Yuanyuan,

In your example the image data range is not being rescaled as it already
has dtype float. `img_as_float` will rescale from [0:255] to [0:1] only if
the dtype of input ndarray is of integer family (and, in your case, uint8).

Take a look:
In [3]: nd_int = np.random.randint(0, 255, (3, 3))

In [4]: nd_int
Out[4]:
array([[ 85,  15,  60],
       [225, 252,  32],
       [162, 173,  34]])

In [5]: nd_int = nd_int.astype(np.uint8)

In [6]: skimage.img_as_float(nd_int)
Out[6]:
array([[ 0.33333333,  0.05882353,  0.23529412],
       [ 0.88235294,  0.98823529,  0.1254902 ],
       [ 0.63529412,  0.67843137,  0.13333333]])

Please, notice that if your data lies in a range [0:255], but the ndarray
dtype is not uint8 (e.g. uint16, int8, etc), you will get different results.

Regards,
Egor

2016-12-27 19:12 GMT+03:00 wine lover <winecoding at gmail.com>:

> Hi Egor,
>
> Thank you for the suggestion. This is how I modify the code
>
> imgs_equalized = np.random.rand(imgs.shape[0],imgs.shape[1],imgs.shape[2],
> imgs.shape[3])
>     for i in range(imgs.shape[0]):
>          print('imgs[i,0] ',imgs[i,0].shape)
>          print('imgs[i,0] ',imgs[i,0].dtype)
>          print('imgs[i,0] ',imgs[i,0].max())
>          print('imgs[i,0] ',imgs[i,0].min())
>          imgs[i,0]=img_as_float(imgs[i,0])
>          print('afte applying astype')
>          print('imgs[i,0] ',imgs[i,0].shape)
>          print('imgs[i,0] ',imgs[i,0].dtype)
>          print('imgs[i,0] ',imgs[i,0].max())
>          print('imgs[i,0] ',imgs[i,0].min())
>
> the output is
>
> imgs[i,0]  (584, 565)
> imgs[i,0]  float64
> imgs[i,0]  255.0
> imgs[i,0]  0.0
> afte applying astype
> imgs[i,0]  (584, 565)
> imgs[i,0]  float64
> imgs[i,0]  255.0
> imgs[i,0]  0.0
>
>
> Looks like it does not convert the image type as I expected, in specific,
> the maximum value.
>
> Thanks,
> Yuanyuan
>
>
>
>
>
>
> On Tue, Dec 27, 2016 at 1:39 AM, Egor Panfilov <egor.v.panfilov at gmail.com>
> wrote:
>
>> Dear Yuanyuan,
>>
>> First of all, it is not a good idea to initialize the array with values
>> using `np.empty`. I'd recommend to use either `np.random.rand` or
>> `np.random.randint`.
>>
>> As for main point of your question, I believe you might need
>> http://scikit-image.org/docs/dev/api/skimage.html#img-as-float (see also
>> http://scikit-image.org/docs/dev/user_guide/data_types.html ).
>> So, you can either create an array of floats [0:1) via  `np.random.rand`,
>> or create an array of uints via `np.random.randint`, and call
>> `img_as_float`. Then `equalize_adapthist` should work flawlessly.
>>
>> Regards,
>> Egor
>>
>> 2016-12-27 1:27 GMT+03:00 wine lover <winecoding at gmail.com>:
>>
>>> Dear All,
>>>
>>> I was trying to use the above code segment for performing Contrast
>>> Limited Adaptive Histogram Equalization (CLAHE).
>>> def clahe_equalized(imgs):
>>>     imgs_equalized = np.empty(imgs.shape)
>>>     for i in range(imgs.shape[0]):
>>>
>>>          print('imgs[i,0] ',imgs[i,0].dtype)
>>>          print('imgs[i,0] ',imgs[i,0].max())
>>>          print('imgs[i,0] ',imgs[i,0].min())
>>>          imgs_equalized[i,0] = exposure.equalize_adapthist(im
>>> gs[i,0],clip_limit=0.03)
>>>     return imgs_equalized
>>>
>>> The dtype is float64, maximum value is 255.0 and minimum value is 0.0
>>>
>>> Running the program generates the following error message ( I only
>>> keep the related ones)
>>>
>>> imgs_equalized[i,0] = exposure.equalize_adapthist(im
>>> gs[i,0],clip_limit=0.03)
>>>    raise ValueError("Images of type float must be between -1 and 1.")
>>> ValueError: Images of type float must be between -1 and 1.
>>>
>>> In accordance with the above error message and image characteristics,
>>> what are the best way to handle this scenario.
>>>
>>> I have been thinking of two approaches
>>>
>>>
>>>    1. add imgs[i,0] = imgs[i,0]/255.   which scale it to 0 and 1
>>>    2.  convert imgs[i,0] from float64 to unit8
>>>
>>> but imgs[i,0] = imgs[i,0].astype(np.unit8) gives the error message such
>>> as
>>>  imgs[i,0]=imgs[i,0].astype(np.unit8)
>>>
>>> AttributeError: 'module' object has no attribute 'unit8'
>>>
>>> Would you like to give any advice on this problem? Thank you very much!
>>>
>>>
>>>
>>> _______________________________________________
>>> scikit-image mailing list
>>> scikit-image at python.org
>>> https://mail.python.org/mailman/listinfo/scikit-image
>>>
>>>
>>
>> _______________________________________________
>> scikit-image mailing list
>> scikit-image at python.org
>> https://mail.python.org/mailman/listinfo/scikit-image
>>
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-image/attachments/20161229/d33c3876/attachment.html>


More information about the scikit-image mailing list