【问题标题】:Save numpy array as image with high precision (16 bits) with scikit-image使用 scikit-image 将 numpy 数组保存为高精度(16 位)的图像
【发布时间】:2014-08-06 01:30:07
【问题描述】:

我正在使用 2D 浮点 numpy 数组,我想以高精度(例如 16 位)保存到灰度 .png 文件中。如果可能的话,我想使用 scikit-image skimage.io 包来做到这一点。

这是我尝试过的主要内容:

import numpy as np
from skimage import io, exposure, img_as_uint, img_as_float

im = np.array([[1., 2.], [3., 4.]], dtype='float64')
im = exposure.rescale_intensity(im, out_range='float')
im = img_as_uint(im)
im

产生:

array([[    0, 21845],
       [43690, 65535]], dtype=uint16)

首先我尝试将其保存为图像,然后使用 Python 图像库重新加载:

# try with pil:
io.use_plugin('pil')
io.imsave('test_16bit.png', im)
im2 = io.imread('test_16bit.png')
im2

产生:

array([[  0,  85],
       [170, 255]], dtype=uint8)

所以某处(在写入或读取中)我失去了精度。然后我尝试使用 matplotlib 插件:

# try with matplotlib:
io.use_plugin('matplotlib')
io.imsave('test_16bit.png', im)
im3 = io.imread('test_16bit.png')
im3

给我一​​个 32 位浮点数:

array([[ 0.        ,  0.33333334],
       [ 0.66666669,  1.        ]], dtype=float32)

但我怀疑这真的是 32 位,因为我将 16 位 uint 保存到文件中。如果有人能指出我哪里出错了,那就太好了。我也希望将其扩展到 3D 数组(即每个颜色通道节省 16 位,每个图像 48 位)。

更新:

问题在于 imsave。图像是每通道 8 位。如何使用 io.imsave 输出高位深度图像?

【问题讨论】:

标签: python image-processing numpy scipy scikit-image


【解决方案1】:

您想使用freeimage 库来执行此操作:

import numpy as np
from skimage import io, exposure, img_as_uint, img_as_float

io.use_plugin('freeimage')

im = np.array([[1., 2.], [3., 4.]], dtype='float64')
im = exposure.rescale_intensity(im, out_range='float')
im = img_as_uint(im)

io.imsave('test_16bit.png', im)
im2 = io.imread('test_16bit.png')

结果:

[[    0 21845]
 [43690 65535]]

对于 3D 数组,你需要正确地构造数组,然后它才能工作:

# im = np.array([[1, 2.], [3., 4.]], dtype='float64')
im = np.linspace(0, 1., 300).reshape(10, 10, 3)
im = exposure.rescale_intensity(im, out_range='float')
im = img_as_uint(im)

io.imsave('test_16bit.png', im)
im2 = io.imread('test_16bit.png')

请注意,读取的图像是翻转的,因此np.fliplr(np.flipud(im2)) 之类的内容会将其恢复为原始形状。

【讨论】:

  • 太好了,谢谢阿布迪斯。对于其他有此问题的用户:我在 OSX 上;我用自制软件 (brew install freeimage) 安装了 freeimage,然后上面的 (io.use_plugin('freeimage')) 工作得很好。
猜你喜欢
  • 2014-10-31
  • 1970-01-01
  • 2016-07-12
  • 2013-04-16
  • 2010-10-28
  • 2021-11-25
  • 2014-08-18
  • 2018-06-17
相关资源
最近更新 更多