2011-04-25 5 views
2

Je souhaite créer un histogramme et le calculer en utilisant la méthode opencvcv.CalcHist. Mais mes données sont des tableaux unidimensionnels au lieu de IplImage objets. Pourquoi le code suivant produit histogramme zéro ?:Calcul de l'histogramme à partir de matrices flottantes unidimensionnelles dans OpenCV

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) 

Répondre

1

Votre code ci-dessus lancers francs une exception (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 

En utilisant np.float32 pour les angles et l'ampleur résout le problème:

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

vous avez résolu mon problème. Je vous remercie! –

Questions connexes