Reputation: 933
I'm using OpenCV in C++ and have written a function that detects keypoints using SURF Detector and uses the brute force matcher BFMachter
to search for matches.
Here's the relevant part of my code:
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;
}
Now I want to call this function from C#. So my question is, is there a way to get a vector of DMatches into C#? Something like a list of points? Or what do I have to do on the C++-Side to get DMatches into an array of points? I don't have much experience with OpenCV data structures.
Here's the relevant part of my C# code:
[DllImport("OpenCVTest1.dll", CallingConvention = CallingConvention.Cdecl)]
public static extern **???** FeatureDetection(...);
Edit: What I need is a list of points that match. I'm not sure that the vector<DMatch>
matches even contains this information.
Upvotes: 0
Views: 2664
Reputation: 300
To convert from vector < DMatch > to a vector of points that match :
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);
}
This will give you two vectors of points, matched_points1 and matched_points2. matched_points1[1] is a match with matched_points[2] and so on.
Upvotes: 1