OpenCV C ++ Vector DMatch to C#

时间:2014-01-21 08:52:40

标签: c# c++ opencv vector type-conversion

我在C ++中使用OpenCV,编写了一个使用SURF Detector检测关键点的函数,并使用强力匹配器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#调用此函数。所以我的问题是,有没有办法将DMat的矢量导入C#?像点列表的东西?或者我在C ++上需要做些什么 - 将DMatches放入点数组中?我对OpenCV数据结构没有多少经验。

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

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

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

1 个答案:

答案 0 :(得分:1)

从vector&lt;转换DMatch&gt;到匹配的点矢量:

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。 matched_points1 [1]与matched_points [2]匹配,依此类推。