Skip to content
Xinnony edited this page Apr 29, 2022 · 4 revisions

SIFT

Mat src1 = new Mat("image1.png", LoadMode.GrayScale);
Mat src2 = new Mat("image2.png", LoadMode.GrayScale);

// Detect the keypoints and generate their descriptors using SIFT
SIFT sift = SIFT.Create();

KeyPoint[] keypoints1, keypoints2;
Mat<float> descriptors1 = new Mat<float>();
Mat<float> descriptors2 = new Mat<float>();
sift.DetectAndCompute(src1, null, out keypoints1, descriptors1);
sift.DetectAndCompute(src2, null, out keypoints2, descriptors2);

// Matching descriptor vectors with a brute force matcher
BFMatcher matcher = new BFMatcher(NormTypes.L2, false);
DMatch[] matches = matcher.Match(descriptors1, descriptors2);

// Draw matches
Mat view = new Mat();
Cv2.DrawMatches(src1, keypoints1, src2, keypoints2, matches, view);

using (new Window("SIFT matching", view, WindowFlags.AutoSize))
{
    Cv2.WaitKey();
}

SURF

Mat src1 = new Mat("image1.png", LoadMode.GrayScale);
Mat src2 = new Mat("image2.png", LoadMode.GrayScale);

// Detect the keypoints and generate their descriptors using SURF
SURF surf = SURF.Create(500, 4, 2, true);

KeyPoint[] keypoints1, keypoints2;
Mat<float> descriptors1 = new Mat<float>();
Mat<float> descriptors2 = new Mat<float>();
surf.DetectAndCompute(src1, null, out keypoints1, descriptors1);
surf.DetectAndCompute(src2, null, out keypoints2, descriptors2);

// Matching descriptor vectors with a brute force matcher
BFMatcher matcher = new BFMatcher(NormTypes.L2, false);
DMatch[] matches = matcher.Match(descriptors1, descriptors2);

// Draw matches
Mat view = new Mat();
Cv2.DrawMatches(src1, keypoints1, src2, keypoints2, matches, view);

using (new Window("SURF matching", view, WindowFlags.AutoSize))
{
    Cv2.WaitKey();
}
Clone this wiki locally