繁体   English   中英

OpenCV C ++矢量DMatch到C#

[英]OpenCV C++ Vector DMatch to C#

我在C ++中使用OpenCV,并编写了一个使用SURF Detector检测关键点并使用蛮力匹配器BFMachter搜索匹配项的BFMachter

这是我的代码的相关部分:

std::vector< DMatch > FeatureDetection(char* source, char* itempl, int x, int y ,int width, int height) // Features2D + Homography to find a known object
{
  /// Load image and template
  Mat img_gray = imread( source, CV_LOAD_IMAGE_GRAYSCALE );
  img = img_gray(Rect(x, y, width, height));

  templ = imread( itempl, CV_LOAD_IMAGE_GRAYSCALE );

  //-- Step 1: Detect the keypoints using SURF Detector
  int minHessian = 400;

  SurfFeatureDetector detector( minHessian );

  std::vector<KeyPoint> keypoints_1, keypoints_2;

  detector.detect( templ, keypoints_1 );
  detector.detect( img, keypoints_2 );

  //-- Step 2: Calculate descriptors (feature vectors)
  SurfDescriptorExtractor extractor;

  Mat descriptors_1, descriptors_2;

  extractor.compute( templ, keypoints_1, descriptors_1 );
  extractor.compute( img, keypoints_2, descriptors_2 );

  //-- Step 3: Matching descriptor vectors with a brute force matcher
  BFMatcher matcher(NORM_L2);
  std::vector< DMatch > matches;
  matcher.match( descriptors_1, descriptors_2, matches );

  return matches;
}

现在,我想从C#调用此函数。 所以我的问题是,有没有办法将DMatches向量导入C#? 有点像点列表吗? 还是我必须在C ++端上做什么才能将DMatch转换成点数组? 我对OpenCV数据结构没有太多经验。

这是我的C#代码的相关部分:

[DllImport("OpenCVTest1.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern **???** FeatureDetection(...);

编辑 :我需要的是匹配点的列表。 我不确定vector<DMatch>匹配是否包含此信息。

要将向量<DMatch>转换为与点匹配的向量:

vector<Point2f> matched_points1, matched_points2; // these are your points that match

for (int i=0;i<matches.size();i++)
{
    // this is how the DMatch structure stores the matching information
    int idx1=matches[i].trainIdx; 
    int idx2=matches[i].queryIdx;

    //now use those match indices to get the keypoints, add to your two lists of points
    matched_points1.push_back(keypoints_1[idx1].pt);
    matched_points2.push_back(keypoints_2[idx2].pt);
}

这将为您提供两个点向量,matched_points1和matched_points2。 match_points1 [1]是与matched_points [2]相匹配的类,依此类推。

暂无
暂无

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

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