Python错误集锦:OpenCV divide()除法运算 :error: (-5:Bad argument) in function ‘divide’ Overload resolution failed: Argument ‘scale’ can not be treated as a double

原文链接:http://www.juzicode.com/archives/5921

错误提示:

OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function ‘divide’ Overload resolution failed:
Argument ‘scale’ can not be treated as a double

#VX公众号:桔子code / juzicode.com
import cv2
import numpy as np 
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('lena.jpg')
img2 = cv2.imread('opencv-logo.png')[0:512,0:512]
img_ret = cv2.divide(img,img2,scale=(100,100,100,0))  
cv2.imshow('img_ret',img_ret)
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-26-4cab3c4cb786> in <module>
      5 img = cv2.imread('lena.jpg')
      6 img2 = cv2.imread('opencv-logo.png')[0:512,0:512]
----> 7 img_ret = cv2.divide(img,img2,scale=(100,100,100,0))
      8 cv2.imshow('img_ret',img_ret)
      9 cv2.waitKey(0)

error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'divide'
> Overload resolution failed:
>  - Argument 'scale' can not be treated as a double
>  - Argument 'scale' can not be treated as a double
>  - argument for divide() given by name ('scale') and position (1)
>  - argument for divide() given by name ('scale') and position (1)

 

错误原因:

1、divide()如果用于2个图像对象相除入参src1和src2都是图像对象,scale参数只能是数值类型,不能使用tuple,这点和add()、subtract()使用标量数值参数不同,add()、subtract()可以使用单个数值作用于第1通道,或者4个数值组成的元组作用域4个通道,但是和addWeighted()的gamma参数一样只能是数值类型。

 

解决方法:

1、修改scale为数值类型scale=100:

#VX公众号:桔子code / juzicode.com
import cv2
import numpy as np 
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('lena.jpg')
img2 = cv2.imread('opencv-logo.png')[0:512,0:512]
img_ret = cv2.divide(img,img2,scale=100)  
cv2.imshow('img_ret',img_ret)
cv2.waitKey(0)
cv2.destroyAllWindows()

 运行结果:

扩展内容:

  1.  OpenCV-Python教程:图像的除法运算

 


 

如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。

发表评论

您的电子邮箱地址不会被公开。 必填项已用*标注