OpenCV C++ Vector DMatch to C#
我在 C 中使用 OpenCV,并编写了一个函数,该函数使用 SURF Detector 检测关键点并使用蛮力匹配器
这是我的代码的相关部分:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 | 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 端做什么才能将 DMatches 放入一个点数组中?我对 OpenCV 数据结构没有太多经验。
这是我的 C# 代码的相关部分:
1 2 | [DllImport("OpenCVTest1.dll", CallingConvention = CallingConvention.Cdecl)] public static extern **???** FeatureDetection(...); |
编辑:我需要的是匹配点的列表。我不确定
从向量 < DMatch > 转换为匹配点的向量:
1 2 3 4 5 6 7 8 9 10 11 12 | 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] 匹配,以此类推。