简体   繁体   English

ValueError:无法从形状(相同形状)广播输入数组

[英]ValueError: could not broadcast input array from shape (same shape)

I am getting an error about broadcasting an input array from shape. 我在从形状广播输入数组时遇到错误。 Normally, this seems to be caused by converting some array of dimension p into some array of dimension: p+1 or p-1 . 通常,这似乎是由于将维度为p某个数组转换为维度为p+1p-1数组引起的。 However, the shapes of my input and output seems to be the same: 3 dimensions. 但是,我的输入和输出的形状似乎是相同的:3个尺寸。 So my question is, what am I doing wrong and how do I go about addressing this issue? 所以我的问题是,我在做错什么,如何解决这个问题?

ValueErrorTraceback (most recent call last)
<ipython-input-21-e821b7e8e0de> in <module>()
    108                   "overlay_glasses.shape: ", overlay_glasses.shape)
    109 
--> 110             overlay_img[int(y):int(y+h),int(x):int(x+w)] = overlay_glasses
    111 

The following are the output of print statements, to give a better understanding of the above code. 以下是print语句的输出,以便更好地理解上述代码。

'overlay_img.shape: '    , (365, 365, 3), 
'overlay_glasses.shape: ', ( 34,  99, 3)
'x: ', 623.26, 'y: ', 265.9
'h: ', 34, 'w: ', 99

larger code snippet: 较大的代码段:

[omitted code here]
if len(centers)>0:
    # change the given value of 2.15 according to the size of the detected face
    glasses_width = 2.16*abs(centers[1][0]-centers[0][0])
    overlay_img = np.ones(shape = roi_color.shape,
                          dtype = np.uint8)*255
    h,w = glass_img.shape[:2]
    scaling_factor = glasses_width/w
    overlay_glasses = cv2.resize(src           = glass_img,
                                 dsize         = None,
                                 fx            = scaling_factor, # scale factor along x-axis; when it equals 0, it is computed as \texttt{(double)dsize.width/src.cols}
                                 fy            = scaling_factor, # scale factor along y-axis
                                 interpolation = cv2.INTER_AREA) # INTER_AREA: resampling using pixel area relation. It may be a preferred method for image decimation,

    x = centers[0][0] if centers[0][0] < centers[1][0] else centers[1][0]

    #   The x and y variables  below depend upon the size of the detected face.
    x   -=  0.26*overlay_glasses.shape[1]
    y   +=  0.85*overlay_glasses.shape[0]
    print("x: ", x,
          "y: ", y)

    #slice the height, width of the overlay image.
    h,  w   =   overlay_glasses.shape[:2]

    print("h: ", h,
          "w: ", w)

    print("overlay_img.shape: ", overlay_img.shape,
          "overlay_glasses.shape: ", overlay_glasses.shape)

    overlay_img[int(y):int(y+h),int(x):int(x+w)] = overlay_glasses # this is the offending line of code

Your x is out of range to create a subimage for the overlay_img you have provided. 您的x超出了范围,无法为您提供的overlay_img创建子图像。 The dimensions of your image are (365, 365, 3), but you provide x to be 623 and x+w to be 722. This creates an empty subimage which cannot be filled with the overlay_glasses 's contents. 图像的尺寸为(365,365,3),但您提供的x为623, x+w为722。这将创建一个空的子图像,该子图像无法用overlay_glasses的内容填充。 Obviously there is something wrong with the x coordinate. 显然, x坐标有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python - ValueError:无法将输入数组从形状 (5) 广播到形状 (2) - Python - ValueError: could not broadcast input array from shape (5) into shape (2) ValueError: 无法将输入数组从形状 (3) 广播到形状 (2) 简单解决方案 - ValueError: could not broadcast input array from shape (3) into shape (2) simple solution ValueError:无法将输入数组从形状 (2,2,5) 广播到形状 (2,) - ValueError: could not broadcast input array from shape (2,2,5) into shape (2,) ValueError:无法将输入数组从形状(6)广播到形状(1) - ValueError: could not broadcast input array from shape (6) into shape (1) ValueError:无法将形状 (3,3) 中的输入数组广播到形状 (3,) - ValueError: could not broadcast input array from shape (3,3) into shape (3,) ValueError:无法将输入数组从形状(22500,3)广播到形状(1) - ValueError: could not broadcast input array from shape (22500,3) into shape (1) ValueError:无法将输入数组从形状(3)广播到形状(2) - ValueError: could not broadcast input array from shape (3) into shape (2) ValueError:无法将输入数组从形状(83)广播到形状(84) - ValueError: could not broadcast input array from shape (83) into shape (84) Numpy:ValueError:无法将输入数组从形状(4,1)广播到形状(4) - Numpy: ValueError: could not broadcast input array from shape (4,1) into shape (4) ValueError:无法将输入数组从形状(1,3)广播到形状(3,1) - ValueError: could not broadcast input array from shape (1,3) into shape (3,1)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM