VLFeat opencv 3.0 Fisher Vector空

时间:2016-03-14 15:35:58

标签: c++ opencv3.0 vlfeat

我试着了解Fisher vector在Opencv3.0和VLFeat库中如何与SIFT一起工作。但我得到了空的矢量和结束。我不确定我在哪里做错了或我的逻辑错了? 如果有人知道这个可能吗?赞赏你的建议。这是我的代码如下:

int main(int argc, char** argv)
{

    int num_clusters = 15; //subject to change
    Ptr<FeatureDetector> detector = xfeatures2d::SIFT::create(400);
    Ptr<DescriptorExtractor> extractor = xfeatures2d::SIFT::create();
    vector<KeyPoint> keypoints;
    Mat descriptors, img;
    Point2f loc;

    //Define for GMM train
    Mat labels, posterior,logLikelihood;
    Ptr<EM> em_model = EM::create();
    em_model->setClustersNumber(num_clusters);
    em_model->setCovarianceMatrixType(EM::COV_MAT_DIAGONAL);
    em_model->setTermCriteria(TermCriteria(TermCriteria::EPS+TermCriteria::MAX_ITER,100, 0.1 ));

    string filename = "21.jpg";

    //local keypoint descriptor based on ORB
    img = imread(filename, CV_LOAD_IMAGE_COLOR);
    detector->detect(img, keypoints);
    extractor->compute(img, keypoints, descriptors); //**

    cout << "Discriptor Dimension: " << descriptors.rows << endl;
    cout << "Training GMM... " << flush;
    em_model->trainEM(descriptors, logLikelihood, labels, posterior);
    cout << "Done!" << endl;

    // extract gaussian information
    Mat means = em_model->getMeans();  //r10nc c2
    vector<Mat> covs; // 10nc [2:2]//covariance matric
    em_model->getCovs(covs); //Returns covariation matrices, size = num 2x2 gaussian

    // extract covariance matrix
    Mat covariaceMatrix;
    for (int itk =0; itk < num_clusters; itk++)
    {
        Mat coVR = Mat(covs[itk]);
        covariaceMatrix.push_back(coVR);
    }
    cout<< "Covariance Matrix \n" << covariaceMatrix << endl;

    Mat weights = em_model->getWeights(); //r1 c30nc

    //define vector to store vector ans
    Mat fvlist;

    //descriptor/centroid?
    Mat *dataToEncode = &descriptors;

    vl_size dimension = descriptors.rows;
    vl_size numClusters = num_clusters;

    //nb of dimensions to keep in local descriptors
    vl_size numData = 64;
    vl_size numDataToEncode = 128;
    //nb of dimensions to keep after Fisher descriptor is projected

    //create a new instance of a GMM object for float data
    VlGMM* gmm = vl_gmm_new(VL_TYPE_FLOAT, dimension, numClusters);

    // cluster the data, i.e. learn the GMM
    vl_gmm_cluster(gmm, &dataToEncode, numData);

    // allocate space for the encoding
    void* enc = vl_malloc(sizeof(float) * 2 * dimension * numClusters);

    // run fisher encoding
    vl_fisher_encode(enc, VL_TYPE_FLOAT, vl_gmm_get_means(gmm), dimension,
                     numClusters, vl_gmm_get_covariances(gmm), vl_gmm_get_priors(gmm),
                     &dataToEncode, numDataToEncode, VL_FISHER_FLAG_IMPROVED);


    fvlist.push_back(enc);
    vl_free(enc);
    vl_gmm_delete(gmm);

    cout << "Fisher Vector\n" << fvlist;


    return 0;
}

0 个答案:

没有答案