Python错误集锦:OpenCV进行图像加减乘除操作时Overload resolution failed:Argument ‘dtype’ is required to be an integer ,Argument ‘scale’ can not be treated as a double

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

错误提示:

OpenCV进行图像加减乘除操作时Overload resolution failed:Argument ‘dtype’ is required to be an integer ,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,dtype=np.float32)  
cv2.imshow('img_ret',img_ret)
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-36-a696a9966718> 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,dtype=np.float32)
      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 'dtype' is required to be an integer
>  - Argument 'dtype' is required to be an integer
>  - Argument 'scale' can not be treated as a double
>  - Argument 'scale' can not be treated as a double

 

错误原因:

1、divide()方法中dtype类型不能用numpy的数据类型,必须用OpenCV的数据类型:

CV_16S CV_16SC1 CV_16SC2 CV_16SC3 CV_16SC4 
CV_16U CV_16UC1 CV_16UC2 CV_16UC3 CV_16UC4 
CV_32F CV_32FC1 CV_32FC2 CV_32FC3 CV_32FC4 
CV_32S CV_32SC1 CV_32SC2 CV_32SC3 CV_32SC4 
CV_64F CV_64FC1 CV_64FC2 CV_64FC3 CV_64FC4 
CV_8S CV_8SC1 CV_8SC2 CV_8SC3 CV_8SC4 
CV_8U CV_8UC1 CV_8UC2 CV_8UC3 CV_8UC4 

 

解决方法:

1、img_ret = cv2.divide(img,img2,dtype=np.float32) 中的dtype修改为OpenCV中的数据类型cv2.CV_32FC3

#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,dtype=cv2.CV_32FC3)  
print(img_ret[:32,:32])
cv2.imshow('img_ret',img_ret)
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:

cv2.__version__: 4.5.2
[[[0.5019608  0.5411765  0.88235295]
  [0.49803922 0.5372549  0.8784314 ]
  [0.49411765 0.53333336 0.8784314 ]
  ...
  [0.4745098  0.5058824  0.87058824]
  [0.44705883 0.5058824  0.8666667 ]
  [0.43529412 0.5058824  0.8627451 ]]

 [[0.49803922 0.5372549  0.8784314 ]
  [0.49803922 0.5372549  0.8784314 ]
  [0.49803922 0.5372549  0.8784314 ]
  ...

扩展内容:

  1.  OpenCV-Python常量类型
  2.  OpenCV-Python教程

 


 

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

发表评论

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