简体   繁体   English

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

[英]How can I speed up this image matching code in EmguCV C# code?

Here's the code. 这是代码。 See the commented line for where possible optimization could be (at the foreach ). 请参阅注释行,以获得可能进行优化的地方(在foreach )。 Anyone have suggestions on improving the speed here? 有人对提高速度有建议吗?

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;
                    }
                }
            }
        }
    }

You can improve your work by changing foreach loop as following: 您可以通过如下更改foreach循环来改进工作:

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

Then it is working fine 那很好

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM