Python错误集锦:subtract()做图像减法时提示图像的大小或通道数不一致: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/5823

错误提示:

使用opencv subtract()做图像减法时如果不是标量计算,而是2个图像,2个图像的大小或通道数不一致: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
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('lena.jpg')  
img2 = cv2.imread('opencv-logo.png') 
img_res = cv2.subtract(img,img2)           
cv2.imshow('img_res',img_res) 
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-11-fa18e8fc5f29> in <module>
      4 img = cv2.imread('lena.jpg')
      5 img2 = cv2.imread('opencv-logo.png')
----> 6 img_res = cv2.subtract(img,img2)
      7 cv2.imshow('img_res',img_res)
      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、subtract()对图像做减法时要求尺寸一样大小,lena.jpg的图像大小是(512,512),opencv-logo.png的大小不是(739,600),所以提示尺寸不一致:

解决方法:

1、将img2的图像截取到(512,512)大小,再做减法。

#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('lena.jpg')  
#img2 = cv2.imread('opencv-logo.png')
img2 = cv2.imread('opencv-logo.png')[0:512,0:512] #改成同样size
print(img.shape,img2.shape)
img_res = cv2.subtract(img,img2)                 
cv2.imshow('img_res',img_res) 
cv2.waitKey(0)
cv2.destroyAllWindows()
==========运行结果:
cv2.__version__: 4.5.2 
(512, 512, 3) (512, 512, 3)

扩展内容:

  1.  
  2.  

 


 

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

发表评论

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