After learning some opencv, I decided to take a more serious look at the skimage pacakge, simply because of the unsatisfying interface of opencv.
from skimage import io
import matplotlib.pyplot as plt
import numpy as np
from skimage import img_as_ubyte, img_as_float
%matplotlib inline
Images in skimage are represnted as numpy ndarrays. However, skimage functions are claimed to support multiple dtypes for images (uint8, uint 16, uint 32, float, int8, int16, int32). Except dtype=float only supports range -1. to 1., all other dtypes support the full ranges. Some key points to take.
skimage.img_as_xx
to do the conversion, e.g., img_as_float
(to 64bit float), img_as_ubyte
(to uint8), img_as_uint
(to uint16), img_as_int
(to int16)img_as_ubyte
necessaryfrom skimage import img_as_float
beach = io.imread("data/images/beach.png")
plt.imshow(beach)
print beach.shape, beach.dtype
float_beach = img_as_float(beach)
plt.figure()
plt.imshow(float_beach)
print float_beach.shape, float_beach.dtype
(233, 350, 3) uint8 (233, 350, 3) float64
*Conversion of Negative Values: negative values are clipped to 0 when converting from signed to unsigned dtypes. Negative values are preserved when converting between signed dtypes.*
## most of time the important conversion will be from float to uint8 (e.g., by opencv)
print img_as_float(np.array([0, 125, 255], np.uint8))
print img_as_ubyte(np.array([-1., 0., 0.25, 1.], np.float64))
[ 0. 0.49019608 1. ] [ 0 0 64 255]