Python numpy减法没有负数(4-6给出254)

Num*_*uis 10 python numpy subtraction grayscale array-difference

我希望从彼此中减去2个灰色人脸以查看差异,但我遇到了减去例如[4] - [6]而不是[-2](或差异:[2])的问题.

print(type(face)) #<type 'numpy.ndarray'>
print(face.shape) #(270, 270)
print(type(nface)) #<type 'numpy.ndarray'>
print(nface.shape) #(270, 270)

#This is what I want to do:
sface = face - self.nface #or
sface = np.subtract(face, self.nface)
Run Code Online (Sandbox Code Playgroud)

两者都不给出负数,而是从255减去0之后的其余部分.

sface的输出示例:

[[  8 255   8 ...,   0 252   3]
 [ 24  18  14 ..., 255 254 254]
 [ 12  12  12 ...,   0   2 254]
 ..., 
 [245 245 251 ..., 160 163 176]
 [249 249 252 ..., 157 163 172]
 [253 251 247 ..., 155 159 173]]
Run Code Online (Sandbox Code Playgroud)

我的问题: 我如何得到一个numpy.ndarray(270,270)减去后的负值面和面上的每个点之间的差异?(所以不是numpy.setdiff1d,因为这只返回1维而不是270x270)

工作

从@ajcr的答案我做了以下(abs()显示减去的脸):

face_16 = face.astype(np.int16)
nface_16 = nface.astype(np.int16)
sface_16 = np.subtract(face_16, nface_16)
sface_16 = abs(sface_16)
sface = sface_16.astype(np.int8)
Run Code Online (Sandbox Code Playgroud)

Ale*_*ley 14

听起来像是dtype阵列的uint8.所有数字将被解释为0-255范围内的整数.这里,-2等于256-2,因此减法结果为254.

你需要将数组重铸为dtype支持负整数的数组,例如int16像这样......

face = face.astype(np.int16)
Run Code Online (Sandbox Code Playgroud)

...然后减去.