错误:When the input arrays in add/subtract/multiply/divide functions have different types, ...

在使用OpenCV的加权和函数时遇到了错误,原因是输入图像的数据类型不一致。解决方法包括在操作前将图像转换为相同的格式,如astype(np.uint8),或者在调用函数时指定dtype参数进行类型转换。通过这两种方式,可以避免'arithm_op'函数中因输入数组类型不同引发的错误。

"When the input arrays in add/subtract/multiply/divide functions have different "

报错:"When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified in function 'arithm_op'" 

在写热力图绘制函数时,遇到以下错误:

Traceback (most recent call last):
  File "/media/wzg/Disk3T/ano_voting_cascade/inference_heatmap.py", line 177, in <module>
    heatmap_img = Vis_obj.intensity_heatmap(background_img=frame,intensity_map=intensity_map,blue_mask_weight=0.3,heat_map_weight=0.5)
  File "/media/wzg/Disk3T/ano_voting_cascade/src/tools/Class_Visualization.py", line 444, in intensity_heatmap
    heatmap_base = cv2.addWeighted(blue_mask, blue_mask_weight, background_img_norm, 1 - blue_mask_weight, 0).astype(np.uint8)
cv2.error: OpenCV(4.2.0) /io/opencv/modules/core/src/arithm.cpp:693: error: (-5:Bad argument) When the input arrays in add/subtract/multiply/divide functions have different types, the output array type must be explicitly specified in function 'arithm_op'

原因:错误的主要原因是 cv2.addWeighted(img,0.01,img0,0.99,0, CV_32F) 的两幅图像的格式不一致。

修改方法1:强制把两幅图的格式转为相同的数据格式就行了,我用的是“.astype(np.uint8)”,如下

blue_mask = cv2.rectangle(background_img.copy(), (0, 0), background_img.shape[0:2], (0, 0, 255), -1).astype(np.uint8)
heatmap_base = cv2.addWeighted(blue_mask, blue_mask_weight, background_img_norm, 1 - blue_mask_weight, 0).astype(np.uint8)

修改方法2:在加权和时强迫格式转换

img2 = cv2.addWeighted(img,0.01,img0,0.99,0, dtype = cv2.CV_32F)

参考:https://blog.youkuaiyun.com/qq_35250841/article/details/108320967

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值