2016-03-06 88 views
0

这是代码。有关可能进行优化的位置,请参阅注释行(位于foreach)。任何人都有提高速度的建议吗?如何加快EmguCV C#代码中的图像匹配代码?

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

回答

0

你可以通过改变foreach循环如下改进工作:

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

然后,它工作正常