【问题标题】:ValueError: could not broadcast input array from shape (same shape)ValueError:无法从形状广播输入数组(相同形状)
【发布时间】:2018-06-24 05:03:13
【问题描述】:

我收到关于从形状广播输入数组的错误。通常,这似乎是由于将某个维度数组p 转换为某个维度数组:p+1p-1。但是,我的输入和输出的形状似乎是相同的:3 维。所以我的问题是,我做错了什么,我该如何解决这个问题?

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 

以下是打印语句的输出,以便更好地理解上述代码。

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

更大的代码sn-p:

[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

【问题讨论】:

  • 尝试打印overlay_img[int(y):int(y+h),int(x):int(x+w)]的形状。
  • 你得到的确切错误是什么?
  • 您不能用(34, 99, 3) 填写(34, 0, 3)

标签: python


【解决方案1】:

您的x 超出范围,无法为您提供的overlay_img 创建子图像。您的图像尺寸为 (365, 365, 3),但您将 x 设置为 623,将 x+w 设置为 722。这会创建一个无法填充 overlay_glasses 内容的空子图像。显然x坐标有问题。

【讨论】:

    猜你喜欢
    • 2018-06-30
    • 2018-06-08
    • 2020-01-21
    • 2020-10-18
    • 2018-09-25
    • 2018-04-21
    • 2020-10-09
    • 2021-03-30
    • 2021-08-24
    相关资源
    最近更新 更多