Python错误集锦:OpenCV bitwise_or()图像和标量数据按位或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

错误提示:

OpenCV bitwise_or()图像和标量数据按位或发生错误: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
print('cv2.__version__:',cv2.__version__)

img1 = cv2.imread('..\\lena.jpg' ) 
img_ret1 = cv2.bitwise_or(img1,(0x3f,0x3f,0x3f))
print('img1[161,199]:    ',img1[161,199])
print('img_ret1[161,199]:',img_ret1[161,199])
img2 = cv2.imread('..\\opencv-logo.png',cv2.IMREAD_UNCHANGED) 
img_ret2 = cv2.bitwise_or(img2,(0x3f,0x3f,0x3f))
print('img2[161,199]:    ',img2[161,199])
print('img_ret2[161,199]:',img_ret2[161,199])

cv2.imshow('img_ret1',img_ret1) 
cv2.imshow('img_ret2',img_ret2) 
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
img1[161,199]:     [109 105 201]
img_ret1[161,199]: [127 127 255]
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-65-1775816c7538> in <module>
      8 print('img_ret1[161,199]:',img_ret1[161,199])
      9 img2 = cv2.imread('..\\opencv-logo.png',cv2.IMREAD_UNCHANGED)
---> 10 img_ret2 = cv2.bitwise_or(img2,(0x3f,0x3f,0x3f))
     11 print('img2[161,199]:    ',img2[161,199])
     12 print('img_ret2[161,199]:',img_ret2[161,199])

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、lena.jpg是按照默认方式读取的,获取的图像为3通道图像,可以和包含3个数值的三元组相或。而opencv-logo.png按照原图方式获取,实际是4通道图像,在opencv的位运算中,4通道图像和标量数据操作,要么用单个数据,要么用包含4个数值的四元组。

 

解决方法:

1、从下面打印图像的shape属性看,opencv-logo对应的img2是4通道图像。和opencv-logo图像按位或运算,改用4元组。

#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)

img1 = cv2.imread('..\\lena.jpg' ) 
print('img1.shape:',img1.shape)
img_ret1 = cv2.bitwise_or(img1,(0x3f,0x3f,0x3f))
print('img1[161,199]:    ',img1[161,199])
print('img_ret1[161,199]:',img_ret1[161,199])
img2 = cv2.imread('..\\opencv-logo.png',cv2.IMREAD_UNCHANGED) 
print('img2.shape:',img2.shape)
img_ret2 = cv2.bitwise_or(img2,(0x3f,0x3f,0x3f,0)) ##改用四元组
print('img2[161,199]:    ',img2[161,199])
print('img_ret2[161,199]:',img_ret2[161,199])

cv2.imshow('img_ret1',img_ret1) 
cv2.imshow('img_ret2',img_ret2) 
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
img1.shape: (512, 512, 3)
img1[161,199]:     [109 105 201]
img_ret1[161,199]: [127 127 255]
img2.shape: (739, 600, 4)
img2[161,199]:     [  0   0 255 255]
img_ret2[161,199]: [ 63  63 255 255]

 

扩展内容:

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

 


 

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

发表评论

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