簡體   English   中英

ValueError:對於 arrays,無法將輸入數組從形狀 (x,y) 廣播到形狀 (x-1, y),形狀使用相同的變量定義

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM