Python错误集锦:OpenCV calcHist计算直方图提示Can’t convert object to vector for ‘histSize’, unsupported type

原文链接:http://www.juzicode.com/python-error-convert-object-to-vector-for-histsize-unsupported-type

错误提示:

OpenCV calcHist计算直方图提示Can’t convert object to vector for ‘histSize’, unsupported type

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

img_src = cv2.imread('lena.jpg') 
histSize = 256
histRange = (0, histSize)  
b_hist = cv2.calcHist([img_src], [0], None, histSize, histRange,accumulate=True) 
plt.title('b hist')
plt.plot(b_hist) 
plt.show() 
==========运行结果:
cv2.__version__: 4.5.3
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-8-777008a0af16> in <module>
      8 histSize = 256
      9 histRange = (0, histSize)
---> 10 b_hist = cv2.calcHist([img_src], [0], None, histSize, histRange,accumulate=True)
     11 plt.title('b hist')
     12 plt.plot(b_hist)

error: OpenCV(4.5.3) :-1: error: (-5:Bad argument) in function 'calcHist'
> Overload resolution failed:
>  - Can't convert object to vector for 'histSize', unsupported type
>  - Can't convert object to vector for 'histSize', unsupported type

错误原因:

1、calcHist的histSize入参要求为tuple或list类型,这里输入的为整数类型,图示类型转换错误。

解决方法:

1、将histSize改为list或tuple类型输入,比如用[histSize]传入:

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

img_src = cv2.imread('lena.jpg') 
histSize = 256
histRange = (0, histSize)  
b_hist = cv2.calcHist([img_src], [0], None, [histSize], histRange,accumulate=True)  
plt.title('b hist')
plt.plot(b_hist) 
plt.show() 

扩展内容:

  1. OpenCV-Python教程


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

发表评论

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