Displaying image uint8 (0-255) and float (0-1)
Displaying uint8 image
from skimage import dataimg = data.moon()
img
Out[3]:
array([[116, 116, 122, ..., 93, 96, 96],
[116, 116, 122, ..., 93, 96, 96],
[116, 116, 122, ..., 93, 96, 96],
...,
[109, 109, 112, ..., 117, 116, 116],
[114, 114, 113, ..., 118, 118, 118],
[114, 114, 113, ..., 118, 118, 118]], dtype=uint8)
import matplotlib.pyplot as plt
fig = plt.figure(figsize=(8, 5))
sub1 = fig.add_subplot(2, 2, 1)
sub1.imshow(img, cmap=plt.cm.gray)
<matplotlib.image.AxesImage at 0x7facb3887410>
Text(0.5,1,u'Displaying image in utin8(0-255)')
Displaying float image
img_float = img/255.0
img_float
array([[0.45490196, 0.45490196, 0.47843137, ..., 0.36470588, 0.37647059,
0.37647059],
[0.45490196, 0.45490196, 0.47843137, ..., 0.36470588, 0.37647059,
0.37647059],
[0.45490196, 0.45490196, 0.47843137, ..., 0.36470588, 0.37647059,
0.37647059],
...,
[0.42745098, 0.42745098, 0.43921569, ..., 0.45882353, 0.45490196,
0.45490196],
[0.44705882, 0.44705882, 0.44313725, ..., 0.4627451 , 0.4627451 ,
0.4627451 ],
[0.44705882, 0.44705882, 0.44313725, ..., 0.4627451 , 0.4627451 ,
0.4627451 ]])
sub2 = fig.add_subplot(2, 2, 2)
sub2.imshow(img_float, cmap=plt.cm.gray)
Out[12]: <matplotlib.image.AxesImage at 0x7facb90d5fd0>
sub2.set_title('Displaying image in float(0-1)')
Out[13]: Text(0.5,1,u'Displaying image in float(0-1)')
You may also use img_as_float function from skimage as follows to convert uint8 to float image
from skimage import img_as_float
image_as_float = img_as_float(img)
img_float == image_as_float
array([[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
...,
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True],
[ True, True, True, ..., True, True, True]])





Comments
Post a Comment