首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ValueError:无法从形状(相同形状)广播输入数组

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

Stack Overflow用户
提问于 2018-06-23 20:36:01
回答 1查看 662关注 0票数 0

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

代码语言:javascript
复制
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 

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

代码语言:javascript
复制
'overlay_img.shape: '    , (365, 365, 3), 
'overlay_glasses.shape: ', ( 34,  99, 3)
'x: ', 623.26, 'y: ', 265.9
'h: ', 34, 'w: ', 99

更大的代码片段:

代码语言:javascript
复制
[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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-06-23 21:03:13

您的x超出了为您提供的overlay_img创建子图像的范围。您的图像大小为(365,365,3),但您提供的x为623,x+w为722。这会产生一个空的子图像,不能用overlay_glasses的内容填充。显然,x坐标有问题。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51004840

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档