简体   繁体   中英

ValueError: Could not broadcast input array from shape (x,y) into shape (x-1, y) for arrays with shape defined with the same variables

I am trying to save an array into a bigger one by excluding the margins of the destination one. It works most of the time, but sometimes the shape of the destination array seems to change without an obvious reason. I really don't understand where the error is coming from, as both shapes are defined with the same variables (Size_X and 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

The error I got, in this case, is "ValueError: Could not broadcast input array from shape (1500,1500) into shape (1499, 1500)"

I printed out a few variables:

Size_X = 1500

Size_Y = 1500

Size_X_ini = 1700

Size_Y_ini = 1600

Corner_index_X = 102

Corner_index_Y = 101

I just don't see how the shapes can be different, but maybe I am looking at this the wrong way. Many thanks in advance for your answers.

In numpy array first axis is Y and second is X. So the code should be:

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

I realized that the numerical values in this line

Image_target[Corner_index_Y:Corner_index_Y+Size_Y, Corner_index_X:Corner_index_X+Size_X] = Image_target_no_margins

in this case would be

Image_target[101:1601, 102:1602] = Image_target_no_margins

and since Image_target has shape (1600, 1700), Image_target[101:1601, 102:1602] will become Image_target[101:1600, 102:1602] , making its new shape (1499,1500) instead of 1500.

And the reason it was working most of the time is that Corner_index_Y was <= 100. Now I just have to find where I went wrong with defining Corner_index_Y

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM