Python错误集锦:OpenCV divide除法运算error: (-209:Sizes of input arguments do not match) The operation is neither ‘array op array’ (where arrays have the same size and the same number of channels), nor ‘array op scalar’, nor ‘scalar op array’ in function ‘cv::arithm_op’

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

错误提示:

 divide除法运算error: (-209:Sizes of input arguments do not match) The operation is neither ‘array op array’ (where arrays have the same size and the same number of channels), nor ‘array op scalar’, nor ‘scalar op array’ in function ‘cv::arithm_op’

#VX公众号:桔子code / juzicode.com
import cv2
import numpy as np 
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('lena.jpg')
img_ret = cv2.divide((25000,25000,25000),img)  
cv2.imshow('divide(scale,img)',img_ret)
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-32-0763313d583c> in <module>
      4 print('cv2.__version__:',cv2.__version__)
      5 img = cv2.imread('lena.jpg')
----> 6 img_ret = cv2.divide((25000,25000,25000),img)
      7 cv2.imshow('divide(scale,img)',img_ret)
      8 cv2.waitKey(0)

error: OpenCV(4.5.2) C:\Users\runneradmin\AppData\Local\Temp\pip-req-build-kuwfz3h3\opencv\modules\core\src\arithm.cpp:650: error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and the same number of channels), nor 'array op scalar', nor 'scalar op array' in function 'cv::arithm_op'

 

错误原因:

1、divide()如果是用一个标量除以一个图像对象,第1个入参必须是单个数值或者4个数值组成的元组,这点类似于add()和subtract()和标量数据的运算。

 

解决方法:

1、img_ret = cv2.divide((25000,25000,25000),img)修改为img_ret = cv2.divide(25000,img),用单个数值和img对象相除,这样只有第1通道相除,其他通道为0:

#VX公众号:桔子code / juzicode.com
import cv2
import numpy as np 
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('lena.jpg')
img_ret = cv2.divide(25000,img)  #####
cv2.imshow('img_ret',img_ret)
cv2.waitKey(0)
cv2.destroyAllWindows()

==========运行结果:

2、img_ret = cv2.divide((25000,25000,25000),img)修改为img_ret = cv2.divide((25000,25000,25000,0),img),用4个数值组成的元组和img对象相除:

#VX公众号:桔子code / juzicode.com
import cv2
import numpy as np 
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('lena.jpg')
img_ret = cv2.divide((25000,25000,25000,0),img)   #####
cv2.imshow('img_ret',img_ret)
cv2.waitKey(0)
cv2.destroyAllWindows()

 ==========运行结果:

扩展内容:

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

 


 

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

发表评论

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