Python错误集锦:OpenCV位运算bitwise_and提示error: (-209:Sizes of input arguments do not match) The operation is neither ‘array op array’ (where arrays have the same size and type), nor ‘array op scalar’, nor ‘scalar op array’ in function ‘cv::binary_op’

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

错误提示:

error: (-209:Sizes of input arguments do not match) The operation is neither 'array op array' (where arrays have the same size and type), nor 'array op scalar', nor 'scalar op array' in function 'cv::binary_op'
#VX公众号:桔子code / juzicode.com
import cv2
import numpy as np 
print('cv2.__version__:',cv2.__version__)
img = np.arange(0,50,1,dtype=np.uint8).reshape(5,10)
img2 = np.arange(50,100,1,dtype=np.float32).reshape(5,10)
print(img.shape)
print(img2.shape)
img_ret = cv2.bitwise_and(img,img2)
print(img_ret)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
(5, 10)
(5, 10)
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-58-75e2e601ee55> in <module>
      7 print(img.shape)
      8 print(img2.shape)
----> 9 img_ret = cv2.bitwise_and(img,img2)
     10 print(img_ret)
     11 cv2.destroyAllWindows()

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

 

错误原因:

1、img和img2的数据类型不一致,img为uint8,img2为float32,所以提示数据类型不一致:where arrays have the same size and type

 

解决方法:

1、修改img和img2为同样的数据类型:

#VX公众号:桔子code / juzicode.com
import cv2
import numpy as np 
print('cv2.__version__:',cv2.__version__)
img = np.arange(0,50,1,dtype=np.uint8).reshape(5,10)
img2 = np.arange(50,100,1,dtype=np.uint8).reshape(5,10)
print(img.shape)
print(img2.shape)
img_ret = cv2.bitwise_and(img,img2)
print(img_ret)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
(5, 10)
(5, 10)
[[ 0  1  0  1  4  5  0  1  8  9]
 [ 8  9 12 13  0  1  0  1  0  1]
 [ 4  5  0  1  8  9  8  9 12 13]
 [16 17  0  1  0  1  4  5  0  1]
 [ 8  9  8  9 12 13 32 33 32 33]]

 

扩展内容:

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

 


 

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

发表评论

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