Python错误集锦:OpenCV merge()方法合并图像时提示error: (-5:Bad argument) in function ‘merge’ > Overload resolution failed: > – merge() takes at most 2 arguments (3 given)

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

错误提示:

OpenCV merge()方法合并图像时提示error: (-5:Bad argument) in function ‘merge’ > Overload resolution failed: > – merge() takes at most 2 arguments (3 given)

#VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('lena.jpg')  
b = img[:,:,0]
g = img[:,:,1]    
r = img[:,:,2]             
img2 = cv2.merge(b,g,r)
cv2.imshow('merged',img2) 
cv2.waitKey(0)
==========运行结果:
cv2.__version__: 4.5.2
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-1-1ce04759d82f> in <module>
      6 g = img[:,:,1]
      7 r = img[:,:,2]
----> 8 img2 = cv2.merge(b,g,r)
      9 cv2.imshow('merged',img2)
     10 cv2.waitKey(0)

error: OpenCV(4.5.2) :-1: error: (-5:Bad argument) in function 'merge'
> Overload resolution failed:
>  - merge() takes at most 2 arguments (3 given)
>  - merge() takes at most 2 arguments (3 given)

 

 

错误原因:

1、merge()合并的图像对象应该是一个tuple

 

解决方法:

1、构造成一个三元组(b,g,r)传入给merge():img2 = cv2.merge((b,g,r))

 #VX公众号:桔子code / juzicode.com
import cv2
print('cv2.__version__:',cv2.__version__)
img = cv2.imread('lena.jpg')  
b = img[:,:,0]
g = img[:,:,1]    
r = img[:,:,2]             
#img2 = cv2.merge(b,g,r)
img2 = cv2.merge((b,g,r))
cv2.imshow('merged',img2) 
cv2.waitKey(0)

 

扩展内容:

  1.  OpenCV-Python教程
  2. OpenCV-Python教程:通道分离、通道合并

 


 

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

发表评论

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