原文链接:http://www.juzicode.com/python-error-opencv-j-nimages-in-function-cv-histprepareimages
错误提示:
calcHist()计算直方图时提示 opencv-4.5.3\modules\imgproc\src\histogram.cpp:160: error: (-215:Assertion failed) j < nimages in function ‘cv::histPrepareImages’
#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], [3], 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-11-15f23d74ec76> in <module>
8 histSize = 256
9 histRange = (0, histSize)
---> 10 b_hist = cv2.calcHist([img_src], [3], None, [histSize], histRange,accumulate=True)
11 plt.title('b hist')
12 plt.plot(b_hist)
error: OpenCV(4.5.3) E:\juzicode\opencv-4.5.3\modules\imgproc\src\histogram.cpp:160: error: (-215:Assertion failed) j < nimages in function 'cv::histPrepareImages'
错误原因:
1、calcHist()第2个入参channels对应的[3]指定的通道号3对于输入的图像img_src来说不存在,img_src通过默认方式读入时通道号编号从0-2,需要修改为对应的通道号。
解决方法:
1、修改calcHist()第2个入参channels参数为0,1或2:
#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], [2], None, [histSize], histRange,accumulate=True)
plt.title('b hist')
plt.plot(b_hist)
plt.show()
扩展内容:
如果本文还没有完全解决你的疑惑,你也可以在微信公众号“桔子code”后台给我留言,欢迎一起探讨交流。