[英]ValueError: Could not broadcast input array from shape (x,y) into shape (x-1, y) for arrays with shape defined with the same variables
我正在尝试通过排除目标数组的边距来将数组保存到更大的数组中。 它在大多数情况下都有效,但有时目标数组的形状似乎会在没有明显原因的情况下发生变化。 我真的不明白错误来自哪里,因为两个形状都是用相同的变量(Size_X 和 Size_Y)定义的。
Image_target_no_margins = cv2.resize((Result_cllRuntime['writeResult']['depthData']*-1), dsize=(Size_X,Size_Y))
# create an array full of 0 with the shape of the Image
Image_target = np.zeros((Size_Y_ini, Size_X_ini))
Image_target[Corner_index_Y:Corner_index_Y+Size_Y, Corner_index_X:Corner_index_X+Size_X] = Image_target_no_margins
在这种情况下,我得到的错误是“ValueError:无法将输入数组从形状(1500,1500)广播到形状(1499、1500)”
我打印了一些变量:
尺寸_X = 1500
尺寸_Y = 1500
大小_X_ini = 1700
大小_Y_ini = 1600
Corner_index_X = 102
Corner_index_Y = 101
我只是看不出形状会有什么不同,但也许我看错了。 非常感谢您的回答。
在 numpy 数组中,第一个轴是 Y,第二个是 X。所以代码应该是:
Image_target = np.zeros((Size_Y_ini, Size_X_ini))
Image_target[Corner_index_Y:Corner_index_Y+Size_Y, Corner_index_X:Corner_index_X+Size_X] = Image_target_no_margins
我意识到这一行中的数值
Image_target[Corner_index_Y:Corner_index_Y+Size_Y, Corner_index_X:Corner_index_X+Size_X] = Image_target_no_margins
在这种情况下将是
Image_target[101:1601, 102:1602] = Image_target_no_margins
由于Image_target
的形状为 (1600, 1700), Image_target[101:1601, 102:1602]
将变为Image_target[101:1600, 102:1602]
,使其成为新的形状 (1499,1500) 而不是 1500。
它大部分时间都在工作的原因是Corner_index_Y
<= 100。现在我只需要找到我在定义Corner_index_Y
出错的地方
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.