OpenCV Python calcHist

时间:2015-01-09 08:52:20

标签: python opencv

我想计算 image histogram with OpenCV's calcHist function with Python
但它不起作用。

代码:

im = cv2.imread(imPath,0) hist = cv2.calcHist([im],[0],None,[256],[0,256])

我这样得到错误

OpenCV Error: Assertion failed (step(dims[-1] == (size_t) CV_ELEM_SIZE(flags)) 
in create, file opencv/sources/modules/core/src/matrix.cpp line:236

2 个答案:

答案 0 :(得分:1)

您的代码不正确 - 以下是一些执行相同操作的示例代码(taken from here):

img = cv2.imread('home.jpg',0)
hist = cv2.calcHist([img],[0],None,[256],[0,256])

因此,我认为您的imPath可能很复杂。我建议使用静态文件路径,类似于" C:\ myImage.jpg",开始并查看是否出现错误。

答案 1 :(得分:1)

试试这个:

import cv2
im = cv2.imread(r'd:\temp\1.bmp',0)
hist = cv2.calcHist([im],[0],None,256,[0,255])

print im
print hist