Merge pull request #2887 from ilya-lavrenov:ipp_morph_fix
[platform/upstream/opencv.git] / samples / cpp / matcher_simple.cpp
1 #include <stdio.h>
2 #include "opencv2/core/core.hpp"
3 #include "opencv2/features2d/features2d.hpp"
4 #include "opencv2/highgui/highgui.hpp"
5 #include "opencv2/nonfree/nonfree.hpp"
6
7 using namespace std;
8 using namespace cv;
9
10 static void help()
11 {
12     printf("\nThis program demonstrates using features2d detector, descriptor extractor and simple matcher\n"
13             "Using the SURF desriptor:\n"
14             "\n"
15             "Usage:\n matcher_simple <image1> <image2>\n");
16 }
17
18 int main(int argc, char** argv)
19 {
20     if(argc != 3)
21     {
22         help();
23         return -1;
24     }
25
26     Mat img1 = imread(argv[1], IMREAD_GRAYSCALE);
27     Mat img2 = imread(argv[2], IMREAD_GRAYSCALE);
28     if(img1.empty() || img2.empty())
29     {
30         printf("Can't read one of the images\n");
31         return -1;
32     }
33
34     // detecting keypoints
35     SurfFeatureDetector detector(400);
36     vector<KeyPoint> keypoints1, keypoints2;
37     detector.detect(img1, keypoints1);
38     detector.detect(img2, keypoints2);
39
40     // computing descriptors
41     SurfDescriptorExtractor extractor;
42     Mat descriptors1, descriptors2;
43     extractor.compute(img1, keypoints1, descriptors1);
44     extractor.compute(img2, keypoints2, descriptors2);
45
46     // matching descriptors
47     BFMatcher matcher(extractor.defaultNorm());
48     vector<DMatch> matches;
49     matcher.match(descriptors1, descriptors2, matches);
50
51     // drawing the results
52     namedWindow("matches", 1);
53     Mat img_matches;
54     drawMatches(img1, keypoints1, img2, keypoints2, matches, img_matches);
55     imshow("matches", img_matches);
56     waitKey(0);
57
58     return 0;
59 }