c# - OpenCV C++ vector DMatch 到 C#

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

我在 C++ 中使用 OpenCV,并编写了一个使用 SURF 检测器检测关键点的函数,并使用强力匹配器 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 的 vector 导入 C#?像点列表之类的东西?或者我必须在 C++ 端做什么才能将 DMatches 放入点数组中?我对 OpenCV 数据结构没有太多经验。

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

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

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

最佳答案

将 vector 转换为匹配点的 vector :

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

这将为您提供两个点 vector ,matched_points1 和 matched_points2。 matched_points1[1] 与 matched_points[2] 等匹配。

关于c# - OpenCV C++ vector DMatch 到 C#,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21253251/

相关文章:

c++ - 将 url 存储到文件中,以便可以快速访问它们

c# - 使用 C# 代码将 zoho crm 与 sql 数据库同步

c++ - 整数不等于它的值

c# - 你如何在 C# 中转换纪元时间?

C++ 如何使用类和函数将我的代码转换为 OOP?

c++ - OpenCV:将 Mat 转换为 UChar4

python - opencv、BGR2HSV 会产生大量伪影

python - 值错误: too many values to unpack opencv-python

c# - 在 ASP.NET MVC View 中转换 IEnumerable(到数组、列表)

c# - XAML DataTemplate 数据绑定(bind)