2014-01-21 112 views
1

我在C++中使用OpenCV並編寫了一個函數,使用SURF檢測器檢測關鍵點,並使用蠻力匹配器BFMachter來搜索匹配項。OpenCV C++矢量DMatch到C#

這裏是我的代碼的相關部分:

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++-Side上做些什麼來將DMatches變成一個點數組?我對OpenCV數據結構沒有太多經驗。

這裏是我的C#代碼中的相關部分:

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

編輯:我需要的是一個點那場比賽的名單。我不確定vector<DMatch>匹配甚至包含此信息。

回答

1

從矢量< 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兩個向量。 matched_points1 [1]與matched_points [2]匹配等等。

+0

你沒有回答我怎麼能把它交給C#,但我已經明白了我自己。我需要的只是知道如何進入比賽點。所以這足以讓我接受你的答案。謝謝! – Mickey

+0

你能清楚說明你是如何得到解決方案的嗎? – user43053534