有沒有更漂亮的方法可以做到這一點?具體來說,這些最大值可以通過numpy API獲得嗎?我無法在API中找到它們,盡管可以在docs中輕松找到它們。MAX_VALUES = {np.uint8: 255, np.uint16: 65535, np.uint32: 4294967295, \ np.uint64: 18446744073709551615}try: image = MAX_VALUES[image.dtype] - imageexcept KeyError: raise ValueError, "Image must be array of unsigned integers."諸如PIL和cv2之類的軟件包提供了用于反轉(zhuǎn)圖像的便捷工具,但是在代碼中,我現(xiàn)在有一個numpy數(shù)組-接下來是更復(fù)雜的分析-我想堅持使用numpy。
2 回答

哈士奇WWW
TA貢獻1799條經(jīng)驗 獲得超6個贊
順便說一句,您不需要定義MAX_VALUES自己。NumPy內(nèi)置了它們:
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]]
添加回答
舉報
0/150
提交
取消