首先,说明用opencv与skimage.io.imread读取和保存图片的区别:
读取和保存后的都是numpy格式,但cv2的读取和存储格式是BGR,而skimage的读取和存储格式是RGB。
1.读取图片:
#skimage读取图片
img=skimage.io.imread(file_dir, as_grey=False)
#imread读取图片,0:grey,1:color,默认:color
img=cv2.imread(file_dir,1)
image.io.imread用法:
image.io.
imread
(fname, as_gray=False, plugin=None, **plugin_args)[source]
Load an image from file.
Parameters
fnamestring
Image file name, e.g. test.jpg
or URL.
as_graybool, optional
If True, convert color images to gray-scale (64-bit floats). Images that are already in gray-scale format are not converted.
pluginstr, optional
Name of plugin to use. By default, the different plugins are tried (starting with imageio) until a suitable candidate is found. If not given and fname is a tiff file, the tifffile plugin will be used.
Returns
img_arrayndarray
The different color bands/channels are stored in the third dimension, such that a gray-image is MxN, an RGB-image MxNx3 and an RGBA-image MxNx4.
Other Parameters
plugin_argskeywords
Passed to the given plugin.
2.存储图片
#skimge
skimage.io.imsave(file_dir, img)
#存储的时候保存为unit8格式
img=np.array(img,dtype=np.uint8)
#cv2
cv2.imwrite(file_dir,img)
注意:如果float类型用imsave()保存,需要数值在-1到1之间,否则会报错.如果一开始就是uint8,保存的时候就无需除以255,如:
img=np.array(img,dtype=np.uint8)
3.将像素值转换为[0,1]之间的数值.也就是给每一个像素值除以255.
img_as_float32
Convert an image to single-precision (32-bit) floating point format, with values in [0, 1].
skimage.img_as_float(skimage.io.imread(filename, as_grey=not color))