python - 从 OpenCV 中的一维 float 组计算直方图

标签 python opencv numpy histogram

我想创建直方图并使用 opencv 方法 cv.CalcHist 计算它。但是我的数据是一维数组而不是 IplImage 对象。为什么以下代码会产生零直方图?:

hist =  cv.CreateHist([3, 3], cv.CV_HIST_ARRAY, [[0, 1], [0, 1]])
angles, magnitudes = np.random.rand(100), np.random.rand(100)
cv.CalcHist([cv.GetImage(cv.fromarray(np.array([x]))) for x in [angles, magnitudes]], hist)
np.array(hist.bins)

>>> array([[ 0.,  0.,  0.],
>>>    [ 0.,  0.,  0.],
>>>    [ 0.,  0.,  0.]], dtype=float32)

最佳答案

您上面的代码抛出异常(opencv 2.3.1):

OpenCV Error: Unsupported format or combination of formats () in calcHist, file /usr/ports/graphics/opencv-core/work/OpenCV-2.3.1/modules/imgproc/src/histogram.cpp, line 632
Traceback (most recent call last):
  File "ocv.py", line 8, in <module>
cv.CalcHist([cv.GetImage(cv.fromarray(np.array([x]))) for x in [angles, magnitudes]], hist)
cv2.error

对角度和大小使用 np.float32 解决了这个问题:

hist =  cv.CreateHist([3, 3], cv.CV_HIST_ARRAY, [[0, 1], [0, 1]])
angles =np.random.rand(100).astype(np.float32)     
magnitude = np.random.rand(100).astype(np.float32)
cv.CalcHist([cv.GetImage(cv.fromarray(np.array([x]))) for x in [angles, magnitudes]], hist)
print np.array(hist.bins)

...

[[ 11.   9.   7.]
 [ 10.  11.  15.]
 [ 11.  14.  12.]]

关于python - 从 OpenCV 中的一维 float 组计算直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5778163/

相关文章:

python - 如何创建一个 tzinfo 设置为 'UTC' 的日期时间对象?

java - 如何处理10位拜耳数据(字节数组)

python - 使用Python打印400空消息代码中的变量输出

c++ - 在图像/opencv/c++ 中获取检测到的圆的中心

visualc++2010中的opencv2.1设置

python - Skyfield 中地球的形状似乎不对 - 我的 python 正确吗?

python - 在 numpy 数组中将 3 个值表示为 1

python - 带有空字符的 numpy.genfromtxt csv 文件

python - 检测 Python 字符串是数字还是字母

Python re question - 子挑战