我想创建直方图并使用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)
答案 0 :(得分:1)
上面的代码抛出异常(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.]]