Ask Your Question
0

opencv3 EM.getCovs(covs)

asked 2015-07-24 13:40:37 -0600

l3an gravatar image

what type should covs be in getCovs(covs)?

From the 3.0-beta c++, "Returns vector std::vector<Mat>& covs of covariation matrices. Number of matrices is the number of gaussian mixtures, each matrix is a square floating-point matrix NxN, where N is the space dimensionality."

from this old opencv2 bug, I understand that "vector<mat> corresponds to a list of numpy arrays, not 3D numpy array (since matrices in vector can in general have different size.) "

from cv2 import ml
import numpy as np

def dictionary(descriptors, number_clusters=N):
    covs = [np.zeros(( N, N), np.float64)]
    em = ml.EM_create()
    em.setClustersNumber(N)
    em.trainEM(descriptors)
    means = em.getMeans()
    covs = em.getCovs(covs)
    weights = em.getWeights()
    return np.float32(means), np.float32(covs), np.float32(weights)[0]

printing out covs just prints out the original [np.zeros(( N, N), np.float64)]. Any body know how to get the covs?

Note that attempting to run this code will not work without fixing this bug and rebuilding.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
1

answered 2015-07-25 01:16:24 -0600

berak gravatar image

updated 2015-07-25 01:43:12 -0600

thanks a lot for reporting back. it was indeed missing a second tweak (a CV_OUT before vector<mat>) so it actually returns the covs array. (you do not have to pre-allocate or feed it in).

patch is updated, please try again !

from cv2 import ml
import numpy as np

data = np.ones((10,10), np.float32)

em = ml.EM_create()
em.setClustersNumber(5)
em.trainEM(data)
covs = em.getCovs()
print(covs)

[array([[  2.22044605e-16,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00,   0.00000000e+00,   0.00000000e+00,
          0.00000000e+00],
       ....

(5 arrays a 10x10)

edit flag offensive delete link more

Comments

you're the man. Thank you

l3an gravatar imagel3an ( 2015-07-25 07:54:23 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2015-07-24 13:40:37 -0600

Seen: 727 times

Last updated: Jul 25 '15