scikits-image/scikits-image — Issue #92: util.dtype._convert is inefficient and not always correct
Author: cgohlke
Created: 2011-12-06 13:17:46+00:00
State: open

Consider lines 78 to 82 in the util.dtype._convert function:

image = image.astype(np.float64)
out = image - imin
out *= scale
out += shift
out = round_fn(out).astype(dtype)

This is potentially very memory and computationally intensive. For example, in the case of uint8 to uint16 conversion, two intermediate arrays of 16 times the size of the input array will be created and the conversion could be done by simply shifting bits instead of using double precision floating point operations.

Second, the scale and round functions are not always correct. For example, double to uint8 should be converted as

np.uint8(image * np.nextafter(image.dtype.type(256), image.dtype.type(0)))

instead of

np.round(image * 255).astype(np.uint8)

Try with image = np.array([1.0/257.0])