Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

there's a couple of things you could try:

1 nearest neighbour search with some distance function:

Mat train; // like 50k rows
Mat test; // a single row feature

double minD=DBL_MAX;
int bestId=0;
for (size_t i=0; i<train.size(); i++)
{
    double d = compareHist(test, train.row(i), HISTCMP_CHISQR); // also try HELLINGER !
    if (d<minD)
    {
          minD=d;
          bestId=i;
    }
}

also, cosine distance instead of compareHist:

static double cosdistance(const cv::Mat &testFeature, const cv::Mat &trainFeature)
{
    double a = trainFeature.dot(testFeature);
    double b = trainFeature.dot(trainFeature);
    double c = testFeature.dot(testFeature);
    return -a / sqrt(b*c);
}

then, you could try a flann::Index:

    // train it:
    Ptr<cv::flann::Index> index =  
          makePtr<cv::flann::Index>(train, cv::flann::LinearIndexParams(), cvflann::FLANN_DIST_L2);
    // predict:
    int K=5;
    cv::flann::SearchParams params;
    cv::Mat dists;
    cv::Mat indices;
    index->knnSearch(test, indices, dists, K, params);

there's a couple of things you could try:

1 nearest neighbour search with some distance function:

Mat train; // like 50k rows
Mat test; // a single row feature

double minD=DBL_MAX;
int bestId=0;
for (size_t i=0; i<train.size(); i++)
{
    double d = compareHist(test, train.row(i), HISTCMP_CHISQR); // also try HELLINGER !
    if (d<minD)
    {
          minD=d;
          bestId=i;
    }
}

also, cosine distance instead of compareHist:

static double cosdistance(const cv::Mat &testFeature, const cv::Mat &trainFeature)
{
    double a = trainFeature.dot(testFeature);
    double b = trainFeature.dot(trainFeature);
    double c = testFeature.dot(testFeature);
    return -a / sqrt(b*c);
}

then, you could try a flann::Index:

    // train it:
    Ptr<cv::flann::Index> index =  
         makePtr<cv::flann::Index>(train, cv::flann::LinearIndexParams(), cvflann::FLANN_DIST_L2);
    // predict:
    int K=5;
    cv::flann::SearchParams params;
    cv::Mat dists;
    cv::Mat indices;
    index->knnSearch(test, indices, dists, K, params);

there's a couple of things you could try:

1 nearest neighbour search with some distance function:

Mat train; // like 50k rows
Mat test; // a single row feature

double minD=DBL_MAX;
int bestId=0;
for (size_t i=0; i<train.size(); i++)
{
    double d = compareHist(test, train.row(i), HISTCMP_CHISQR); // also try HELLINGER !
    if (d<minD)
    {
          minD=d;
          bestId=i;
    }
}

also, cosine distance instead of compareHist:

static double cosdistance(const cv::Mat &testFeature, const cv::Mat &trainFeature)
{
    double a = trainFeature.dot(testFeature);
    double b = trainFeature.dot(trainFeature);
    double c = testFeature.dot(testFeature);
    return -a / sqrt(b*c);
}

then, you could try a flann::Index:

    // train it:
    Ptr<cv::flann::Index> index =  
        makePtr<cv::flann::Index>(train, cv::flann::LinearIndexParams(), cvflann::FLANN_DIST_L2);
    // predict:
    int K=5;
    cv::flann::SearchParams params;
    cv::Mat dists;
    cv::Mat indices;
    index->knnSearch(test, indices, dists, K, params);