【发布时间】:2017-06-22 14:04:54
【问题描述】:
首先我得到的错误是:
import numpy as np
dtype_range = {np.bool_: (False, True),
np.bool8: (False, True),
np.uint8: (0, 255),
np.uint16: (0, 65535),
np.int8: (-128, 127),
np.int16: (-32768, 32767),
np.int64: (-2**63, 2**63 - 1),
np.uint64: (0, 2**64 - 1),
np.int32: (-2**31, 2**31 - 1),
np.uint32: (0, 2**32 - 1),
np.float32: (-1, 1),
np.float64: (-1, 1)}
dtype_range[image.dtype.type]
>>>KeyError: <type 'numpy.uint32'>
疯狂的是,当我这样做时
print image.dtype.type
>>><type 'numpy.uint32'>
现在的主要问题是,这发生在 skimage.io 的标准库中,确切地说是 dtype.py,我无法更改该源代码。所以我想知道在传递我的论点image 以使其发挥作用时,我该如何改变或改变什么?我的猜测是命名空间有问题,因为它是np 和numpy?但是我如何影响我的数据以将其保存为np.uint32 而不是numpy.uint32??
我正在使用 Python 2.7 顺便说一句。
我们将不胜感激。
编辑:
我将图像投射到uint8 by
image = image.astype(uint8)
现在这并没有给我一个错误,即使
print image.astype(uint8).dtype.type
>>><type 'numpy.uint8'>
当然是你所期望的。
这解决了我的问题。但是,如果有人知道这里发生了什么,我会很高兴回答只是为了理解这个问题。无论如何谢谢:)
编辑:
现在这是从 IPython 控制台的输出中复制的:
image = np.array([35, 37, 39, 36, 34, 31, 33, 32, 32, 33, 31, 33, 30, 34, 36, 37, 36,
32, 33, 30, 28, 30, 28, 28, 29, 30, 29, 31, 30, 31, 36, 33, 34, 31,
34, 35, 34, 32, 29, 26, 25, 27, 25, 26, 25, 27, 30, 30, 28, 26, 28,
30, 32, 34, 36, 36, 36, 32, 36, 37, 34, 34, 35, 33, 33, 30, 33, 36,
36, 36, 33, 33, 39, 38, 34, 32, 32, 29, 28, 29, 30, 32, 32, 28, 30,
32, 34, 30, 28, 32, 34, 34, 35, 33, 35, 33, 33, 35, 37, 39], dtype=uint32)
【问题讨论】:
-
您的复制粘贴有问题。
print语句应该打印<type 'numpy.uint32'>而不仅仅是numpy.uint32。否则你的错误是你用字符串而不是类型来索引你的dict。 -
是的,你是对的,我实际上并没有在源代码中打印它,而是在 spyder 的 Ipython 控制台中执行它,这就是为什么我的输出看起来不同的原因。我把它改成了正确的形式。
标签: python numpy scikit-image uint32