原文链接:http://www.juzicode.com/archives/5903
错误提示:
用OpenCV-Python的addWeighted()时提示 Argument ‘gamma’ can not be treated as a double:
#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('..\\lena.jpg')[0:512,0:512] #截取部分,保证大小一致
img2 = cv2.imread('..\\opencv-logo.png' )[0:512,0:512]
img3 = cv2.addWeighted(img,0.5,img2,0.7,(10,20,30))
cv2.imshow('addWeighted',img3)
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
---------------------------------------------------------------------------
error Traceback (most recent call last)
<ipython-input-13-14d3a7fc7f76> in <module>
4 img = cv2.imread('..\\lena.jpg')[0:512,0:512] #截取部分,保证大小一致
5 img2 = cv2.imread('..\\opencv-logo.png' )[0:512,0:512]
----> 6 img3 = cv2.addWeighted(img,0.5,img2,0.7,(10,20,30))
7 cv2.imshow('addWeighted',img3)
8 cv2.waitKey(0)
error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'addWeighted'
> Overload resolution failed:
> - Argument 'gamma' can not be treated as a double
> - Argument 'gamma' can not be treated as a double
错误原因:
1、addWeighted的第5个位置参数gamma不能像add或者subtract使用标量类型数据那样传入元组,必须传入单个的数值 。该数值如果作用域多通道图像,多个通道则都会同时加上gamma所表示的大小
解决方法:
1、修改第5个位置参数gamma为单个数值类型:
#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('..\\lena.jpg')[0:512,0:512] #截取部分,保证大小一致
img2 = cv2.imread('..\\opencv-logo.png' )[0:512,0:512]
#img3 = cv2.addWeighted(img,0.5,img2,0.7,(10,20,30))
img3 = cv2.addWeighted(img,0.5,img2,0.7,10)
cv2.imshow('addWeighted',img3)
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
扩展内容:
如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。