Python numpy中数组之间的差异

Python numpy中数组之间的差异,python,numpy,scikit-learn,scikit-image,Python,Numpy,Scikit Learn,Scikit Image,所以,我有3个RGB通道的灰度图像。 当我在图像[0]和图像[1]之间取差时,以及当我在图像[1]和图像[0]之间取差时,我取两个通道之间的差分,结果不同 我做错了什么 观察: >>> import numpy as np >>> a, b = np.random.randint(0, 255, (2, 10), dtype=np.uint8) >>> a - b array([ 62, 10, 126, 206, 157, 36, 17

所以,我有3个RGB通道的灰度图像。 当我在
图像[0]
图像[1]
之间取差时,以及当我在
图像[1]
图像[0]
之间取差时,我取两个通道之间的差分,结果不同

我做错了什么

观察:

>>> import numpy as np
>>> a, b = np.random.randint(0, 255, (2, 10), dtype=np.uint8)
>>> a - b
array([ 62,  10, 126, 206, 157,  36, 170,  42,  54,   1], dtype=uint8)
>>> b - a
array([194, 246, 130,  50,  99, 220,  86, 214, 202, 255], dtype=uint8)

>>> a.astype(np.int) - b.astype(np.int)
array([  62, -246, -130,  -50,  -99,   36,  -86,   42,   54,    1])
>>> b.astype(np.int) - a.astype(np.int)
array([-62, 246, 130,  50,  99, -36,  86, -42, -54,  -1])
RGB图像包含无符号(仅非负)8位整数,因此所有操作将返回
np.uint8

>>> np.uint8(1) - np.uint8(5)
__main__:1: RuntimeWarning: overflow encountered in ubyte_scalars
252  # not -4
>>> np.int8(1) - np.int8(5)
-4

请以格式化文本而不是图像的形式发布代码和输出。另外,
3-1==2
,但
1-3==2
这一事实是否让您感到惊讶?数组也一样。但目前我有不同的数字,而不是符号。你确定你的数字一开始就是有符号的吗?不,我怎么能将数组转换为有符号的?a)你在帖子中没有提到任何关于规范的内容;b) 这仍然可以归结为完全相同的问题。您应该决定要使用的数据类型。