使用 skimage python 库调整图像大小后信息丢失

Loss of information after resizing the image using skimage python library

我正在使用以下函数调整具有形状 (samples, 1, image_row, image_column) 的图像集的大小。我正在使用 skimage 库。

from skimage import io
from skimage.transform import resize   
def preprocess(imgs):
imgs_p = np.ndarray((imgs.shape[0], imgs.shape[1], img_rows, img_cols), dtype=np.uint8)
for i in range(imgs.shape[0]):
    imgs_p[i, 0]  = resize(imgs[i, 0], (img_rows, img_cols))
return imgs_p

但是,我注意到调整大小的图像有点变成 0-1 数组。下面是一些测试结果。我们可以看到调整大小的图像仅包含 0-1 值。我不确定我的调整大小功能有什么问题。

   print(image[0,0].shape)

   (420, 580)
   print(image[0,0])
[[  0 155 152 ...,  87  91  90]
  [  0 255 255 ..., 140 141 141]
  [  0 255 255 ..., 157 156 158]
    ...,
  [  0  77  63 ..., 137 133 122]
  [  0  77  63 ..., 139 136 127]
  [  0  77  64 ..., 149 144 137]]

  print(resized_image[0,0].shape)
  (96, 128)
  print(resized_image[0,0])
  [[1 1 0 ..., 0 0 0]
   [0 0 0 ..., 0 0 0]
   [0 0 0 ..., 0 0 0]
     ...,
   [0 0 0 ..., 0 0 0]
   [0 0 0 ..., 0 0 0]
   [0 0 0 ..., 0 0 0]]

resize 的输出有一个 float 数据类型,因此它在 0-1 范围内。您可以将图像转换回 uint8 范围:

from skimage import img_as_ubyte
image = img_as_ubyte(image)

有关数据类型及其范围的完整说明,请参阅 user guide

调整大小时,您的图像将转换为浮点数。 transform.resize() 有一个可选的布尔标志:preserve_range。来自 source code

preserve_range: bool, optional. Whether to keep the original range of values. Otherwise, the input image is converted according to the conventions of img_as_float.

将其设置为 True 它应该可以解决您的问题。