DescriptorMatcher OpenCV train()

DescriptorMatcher OpenCV train()

OpenCV 的文档提到了 DescriptorMatcher 中的函数 "train()"。

"虚空 cv::cuda::DescriptorMatcher::train ( )
纯虚拟

训练描述符匹配器。

训练描述符匹配器(例如,flann 索引)。在所有要匹配的方法中,方法 train() 在匹配之前每次都是 运行。"(docs)

那里就是这么说的。有人知道它有效吗?特别是 DescriptorMatcher 需要自我训练的东西。一些 OOP 语言的简短示例会很棒。

这是文档的 Link:

http://docs.opencv.org/master/dd/dc5/classcv_1_1cuda_1_1DescriptorMatcher.html#ab220b434f827962455f430a12c65c074

提前致谢

你可以看到匹配器代码here

Trains a descriptor matcher (for example, the flann index). In all methods to match, the method train() is run every time before matching.

是的,从代码可以看出,在匹配函数中调用了train()

void DescriptorMatcher::knnMatch( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, int knn,
                                  InputArrayOfArrays masks, bool compactResult )
{
    if( empty() || queryDescriptors.empty() )
        return;

    CV_Assert( knn > 0 );

    checkMasks( masks, queryDescriptors.size().height );

    train();
    knnMatchImpl( queryDescriptors, matches, knn, masks, compactResult );
}

void DescriptorMatcher::radiusMatch( InputArray queryDescriptors, std::vector<std::vector<DMatch> >& matches, float maxDistance,
                                     InputArrayOfArrays masks, bool compactResult )
{
    matches.clear();
    if( empty() || queryDescriptors.empty() )
        return;

    CV_Assert( maxDistance > std::numeric_limits<float>::epsilon() );

    checkMasks( masks, queryDescriptors.size().height );

    train();
    radiusMatchImpl( queryDescriptors, matches, maxDistance, masks, compactResult );
}

当你调用 match() 时,它实际上会调用 knnMatchknn = 1

void DescriptorMatcher::match( InputArray queryDescriptors, std::vector<DMatch>& matches, InputArrayOfArrays masks )
{
    std::vector<std::vector<DMatch> > knnMatches;
    knnMatch( queryDescriptors, knnMatches, 1, masks, true /*compactResult*/ );
    convertMatches( knnMatches, matches );
}

train() 的基本实现什么都不做:

void DescriptorMatcher::train()
{}

FlannBasedMatcher重载train():

void FlannBasedMatcher::train()
{
    if( !flannIndex || mergedDescriptors.size() < addedDescCount )
    {
        // FIXIT: Workaround for 'utrainDescCollection' issue (PR #2142)
        if (!utrainDescCollection.empty())
        {
            CV_Assert(trainDescCollection.size() == 0);
            for (size_t i = 0; i < utrainDescCollection.size(); ++i)
                trainDescCollection.push_back(utrainDescCollection[i].getMat(ACCESS_READ));
        }
        mergedDescriptors.set( trainDescCollection );
        flannIndex = makePtr<flann::Index>( mergedDescriptors.getDescriptors(), *indexParams );
    }
}

关于如何使用FlannBasedMatcher的例子你可以参考OpenCV doc example

可以参考这个answer to know what is done in the training phase. In short, you're building the index for the matcher. You can find source code here