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

被折叠的 条评论
为什么被折叠?



