How to calculate 3D histogram in python using open CV

老子叫甜甜 提交于 2019-11-30 23:23:20

I came across this question while trying to make a 3D histogram of an HSV image, and encountered the same error. It turns out that the OpenCV documentation is leading us astray here. The docs are written for the C++ API and as such can only be used as a vague guide to the Python cv2 API (although I have found that the docs are misleading for C++ as well at times).

The function signature is as follows:

cv2.calcHist(images, channels, mask, histSize, ranges[, hist[, accumulate]]) -> hist

The key point is that the channels, histSize and ranges parameters should be flat lists, not nested lists as in your example. Try the following, assuming i_lab is a three-channel image:

range_hist = [0, 100, -100, 100, -100, 100]
hist_1 = cv2.calcHist([i_lab], [0, 1, 2], None, [20, 20, 20], range_hist)

For a more complete example, try this code listing from the opencvpython blog.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!