2

有没有更漂亮的方法来做到这一点?具体来说,这些最大值是否可以通过 numpy API 获得?尽管在 docs中很容易找到它们,但我无法在 API 中找到它们。

MAX_VALUES = {np.uint8: 255, np.uint16: 65535, np.uint32: 4294967295, \
              np.uint64: 18446744073709551615}

try:
    image = MAX_VALUES[image.dtype] - image
except KeyError:
    raise ValueError, "Image must be array of unsigned integers."

像 PIL 和 cv2 这样的包提供了方便的工具来反转图像,但是在代码中的这一点上,我有一个 numpy 数组——下面是更复杂的分析——我想坚持使用 numpy。

4

2 回答 2

4

尝试做

image ^= MAX_VALUES[image.dtype]
于 2013-06-26T21:21:42.500 回答
1

顺便说一句,您不需要定义MAX_VALUES自己。NumPy内置了它们

import numpy as np
h, w = 100, 100
image = np.arange(h*w).reshape((h,w)).astype(np.uint8)
max_val = np.iinfo(image.dtype).max
print(max_val)
# 255
image ^= max_val

print(image)
# [[255 254 253 ..., 158 157 156]
#  [155 154 153 ...,  58  57  56]
#  [ 55  54  53 ..., 214 213 212]
#  ..., 
#  [ 27  26  25 ..., 186 185 184]
#  [183 182 181 ...,  86  85  84]
#  [ 83  82  81 ..., 242 241 240]]
于 2013-06-26T21:39:06.203 回答