我正在编程一个函数,该函数平均5张图像以提取背景。 “ timg”是具有5个2D numpy数组的数组。在这些阵列中,以黑白存储图像。 imagesize是图像侧面的像素大小(所有图像均为正方形)
我尝试将其更改为numpy数组和python列表
def join_images_average(timg,imagesize):
jimg=timg[0]
print('averaging')
for i in range (4):
print(i+1)
for x in range(imagesize):
for y in range(imagesize):
#print(y)
jimg[x][y]=jimg[x][y]+timg[i+1][x][y]
if(jimg[x][y]<0.0):
jimg[x][y]=0.0
#plt.imshow(jimg, cmap="gray")
#plt.show()
for x in range(imagesize):
for y in range(imagesize):
#print(y)
jimg[x][y]=jimg[x][y]/5
print(jimg[2])
jimg=np.array(jimg)
for i in range(5):
print(i,jimg[i])
sleep(5)
if(i==0):
a=jimg[i]
else:
a=np.sum(a,jimg[i])
return np.divide(a,5)
averaging
1
2
3
4
[0. 0. 0. ... 0. 0. 0.]
0 [0. 0. 0. ... 0. 0. 0.]
1 [0. 0. 0. ... 0. 0. 0.]
<ipython-input-38-89e89acc3bc5> in join_images_average(timg, imagesize)
24 a=jimg[i]
25 else:
---> 26 a=np.sum(a,jimg[i])
27 return np.divide(a,5)
/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py in sum(a, axis, dtype, out, keepdims, initial)
2074
2075 return _wrapreduction(a, np.add, 'sum', axis, dtype, out, keepdims=keepdims,
-> 2076 initial=initial)
2077
2078
/usr/local/lib/python3.6/dist-packages/numpy/core/fromnumeric.py in _wrapreduction(obj, ufunc, method, axis, dtype, out, **kwargs)
84 return reduction(axis=axis, out=out, **passkwargs)
85
---> 86 return ufunc.reduce(obj, axis, dtype, out, **passkwargs)
87
88
TypeError: only integer scalar arrays can be converted to a scalar index