Python错误集锦:OpenCV reduce计算行列和 Unsupported combination of input and output array formats in function ‘cv::reduce’

原文链接:http://www.juzicode.com/python-error-opencv-unsupported-combination-of-input-and-output-array-formats-reduce

错误提示:

reduce计算行列和时提示:Unsupported combination of input and output array formats in function ‘cv::reduce’

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

arr = np.array([[1,1,0,2,0],[0,20,20,11,15],[5,5,5,5,5]],dtype=np.uint8)
print('arr:\n',arr)
row_max = cv2.reduce(arr,0,cv2.REDUCE_MAX)
print('row_max: ',row_max) 
row_sum = cv2.reduce(arr,0,cv2.REDUCE_SUM)
print('row_sum: ',row_sum) 
==========运行结果:
#VX公众号:桔子code / juzicode.com
import numpy as np
import cv2
print('cv2.__version__:',cv2.__version__)
​
arr = np.array([[1,1,0,2,0],[0,20,20,11,15],[5,5,5,5,5]],dtype=np.uint8)
print('arr:\n',arr)
row_max = cv2.reduce(arr,0,cv2.REDUCE_MAX)
print('row_max: ',row_max) 
row_sum = cv2.reduce(arr,0,cv2.REDUCE_SUM)
print('row_sum: ',row_sum) 
cv2.__version__: 4.5.3
arr:
 [[ 1  1  0  2  0]
 [ 0 20 20 11 15]
 [ 5  5  5  5  5]]
row_max:  [[ 5 20 20 11 15]]
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-25-c60232546964> in <module>
      8 row_max = cv2.reduce(arr,0,cv2.REDUCE_MAX)
      9 print('row_max: ',row_max)
---> 10 row_sum = cv2.reduce(arr,0,cv2.REDUCE_SUM)
     11 print('row_sum: ',row_sum)

error: OpenCV(4.5.3) E:\juzicode\opencv-4.5.3\modules\core\src\matrix_operations.cpp:853: error: (-210:Unsupported format or combination of formats) Unsupported combination of input and output array formats in function 'cv::reduce'

错误原因:

1、reduce的rtype为REDUCE_SUM时表示计算和,这里没有设定dtype类型,所以默认用源图像的np.uint8,对应OpenCV的CV_8U类型。新生成的数据其范围可能会大于源图像的CV_8U所表示的最大数值,需要设定dtype为能表示更大精度的CV_32S或CV_32F,CV_64F。

解决方法:

1、reduce入参dtype设定为CV_32S或CV_32F,CV_64F

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

arr = np.array([[1,1,0,2,0],[0,20,20,11,15],[5,5,5,5,5]],dtype=np.uint8)
print('arr:\n',arr)
row_max = cv2.reduce(arr,0,cv2.REDUCE_MAX)
print('row_max: ',row_max) 
row_sum = cv2.reduce(arr,0,cv2.REDUCE_SUM,dtype=cv2.CV_32S) #增加dtype入参,指明数据类型
print('row_sum: ',row_sum) 
==========运行结果:
cv2.__version__: 4.5.3
arr:
 [[ 1  1  0  2  0]
 [ 0 20 20 11 15]
 [ 5  5  5  5  5]]
row_max:  [[ 5 20 20 11 15]]
row_sum:  [[ 6 26 25 18 20]]

扩展内容:

  1. OpenCV-Python教程


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

发表评论

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