如何在EmguCV C#代码中加速这个图像匹配代码?

时间:2016-03-06 07:31:14

标签: c# emgucv

这是代码。请参阅注释行,了解可能的优化位置(foreach)。有人提出改善速度的建议吗?

public void FindMatches(Matrix<float> dbDescriptors, Matrix<float> queryDescriptors, ref IList<IndecesMapping> imap)
    {
        var indices = new Matrix<int>(queryDescriptors.Rows, 2); // matrix that will contain indices of the 2-nearest neighbors found
        var dists = new Matrix<float>(queryDescriptors.Rows, 2); // matrix that will contain distances to the 2-nearest neighbors found

        // create FLANN index with 4 kd-trees and perform KNN search over it look for 2 nearest neighbours
        var flannIndex = new Index(dbDescriptors, 4);
        flannIndex.KnnSearch(queryDescriptors, indices, dists, 2, 24);

        for (int i = 0; i < indices.Rows; i++)
        {
            // filter out all inadequate pairs based on distance between pairs
            if (dists.Data[i, 0] < (0.5 * dists.Data[i, 1]))
            {
                // find image from the db to which current descriptor range belongs and increment similarity value.
                // in the actual implementation this should be done differently as it's not very efficient for large image collections.
                foreach (var img in imap)
                {
                    if (img.IndexStart <= indices[i, 0] && img.IndexEnd >= indices[i, 0])
                    {
                        img.Similarity++;
                        break;
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

您可以通过更改foreach循环来改善您的工作:

 foreach (var img in imap)
            {
                if (img.IndexStart <= indices[i, 1] && img.IndexEnd >= indices[i, 1])
                {
                    img.Similarity++;
                    break;
                }
            }

然后它工作正常