2011-04-25 5 views
2

히스토그램을 만들고 opencv 방법 cv.CalcHist을 사용하여 계산하고 싶습니다. 하지만 내 데이터는 IplImage 개체 대신 1 차원 배열입니다. 왜 다음 코드는 제로 히스토그램?OpenCV의 1 차원 플로트 배열에서 히스토그램 계산

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) 

답변

1

귀하의 코드는 위의 예외가 발생합니다 (OpenCV의 2.3.1)을 생성 않습니다 각도와 크기에 대한 np.float32 사용

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 

문제를 해결

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.]] 
+0

내 문제가 해결되었습니다. 고맙습니다! –