Add next and previous navigation links to all tutorials
[platform/upstream/opencv.git] / doc / tutorials / features2d / akaze_matching / akaze_matching.markdown
1 AKAZE local features matching {#tutorial_akaze_matching}
2 =============================
3
4 @prev_tutorial{tutorial_detection_of_planar_objects}
5 @next_tutorial{tutorial_akaze_tracking}
6
7 Introduction
8 ------------
9
10 In this tutorial we will learn how to use AKAZE @cite ANB13 local features to detect and match keypoints on
11 two images.
12 We will find keypoints on a pair of images with given homography matrix, match them and count the
13 number of inliers (i.e. matches that fit in the given homography).
14
15 You can find expanded version of this example here:
16 <https://github.com/pablofdezalc/test_kaze_akaze_opencv>
17
18 Data
19 ----
20
21 We are going to use images 1 and 3 from *Graffiti* sequence of [Oxford dataset](http://www.robots.ox.ac.uk/~vgg/data/data-aff.html).
22
23 ![](images/graf.png)
24
25 Homography is given by a 3 by 3 matrix:
26 @code{.none}
27 7.6285898e-01  -2.9922929e-01   2.2567123e+02
28 3.3443473e-01   1.0143901e+00  -7.6999973e+01
29 3.4663091e-04  -1.4364524e-05   1.0000000e+00
30 @endcode
31 You can find the images (*graf1.png*, *graf3.png*) and homography (*H1to3p.xml*) in
32 *opencv/samples/data/*.
33
34 ### Source Code
35
36 @add_toggle_cpp
37 -   **Downloadable code**: Click
38     [here](https://raw.githubusercontent.com/opencv/opencv/3.4/samples/cpp/tutorial_code/features2D/AKAZE_match.cpp)
39
40 -   **Code at glance:**
41     @include samples/cpp/tutorial_code/features2D/AKAZE_match.cpp
42 @end_toggle
43
44 @add_toggle_java
45 -   **Downloadable code**: Click
46     [here](https://raw.githubusercontent.com/opencv/opencv/3.4/samples/java/tutorial_code/features2D/akaze_matching/AKAZEMatchDemo.java)
47
48 -   **Code at glance:**
49     @include samples/java/tutorial_code/features2D/akaze_matching/AKAZEMatchDemo.java
50 @end_toggle
51
52 @add_toggle_python
53 -   **Downloadable code**: Click
54     [here](https://raw.githubusercontent.com/opencv/opencv/3.4/samples/python/tutorial_code/features2D/akaze_matching/AKAZE_match.py)
55
56 -   **Code at glance:**
57     @include samples/python/tutorial_code/features2D/akaze_matching/AKAZE_match.py
58 @end_toggle
59
60 ### Explanation
61
62 -   **Load images and homography**
63
64 @add_toggle_cpp
65 @snippet samples/cpp/tutorial_code/features2D/AKAZE_match.cpp load
66 @end_toggle
67
68 @add_toggle_java
69 @snippet samples/java/tutorial_code/features2D/akaze_matching/AKAZEMatchDemo.java load
70 @end_toggle
71
72 @add_toggle_python
73 @snippet samples/python/tutorial_code/features2D/akaze_matching/AKAZE_match.py load
74 @end_toggle
75
76 We are loading grayscale images here. Homography is stored in the xml created with FileStorage.
77
78 -   **Detect keypoints and compute descriptors using AKAZE**
79
80 @add_toggle_cpp
81 @snippet samples/cpp/tutorial_code/features2D/AKAZE_match.cpp AKAZE
82 @end_toggle
83
84 @add_toggle_java
85 @snippet samples/java/tutorial_code/features2D/akaze_matching/AKAZEMatchDemo.java AKAZE
86 @end_toggle
87
88 @add_toggle_python
89 @snippet samples/python/tutorial_code/features2D/akaze_matching/AKAZE_match.py AKAZE
90 @end_toggle
91
92 We create AKAZE and detect and compute AKAZE keypoints and descriptors. Since we don't need the *mask*
93 parameter, *noArray()* is used.
94
95 -   **Use brute-force matcher to find 2-nn matches**
96
97 @add_toggle_cpp
98 @snippet samples/cpp/tutorial_code/features2D/AKAZE_match.cpp 2-nn matching
99 @end_toggle
100
101 @add_toggle_java
102 @snippet samples/java/tutorial_code/features2D/akaze_matching/AKAZEMatchDemo.java 2-nn matching
103 @end_toggle
104
105 @add_toggle_python
106 @snippet samples/python/tutorial_code/features2D/akaze_matching/AKAZE_match.py 2-nn matching
107 @end_toggle
108
109 We use Hamming distance, because AKAZE uses binary descriptor by default.
110
111 -   **Use 2-nn matches and ratio criterion to find correct keypoint matches**
112 @add_toggle_cpp
113 @snippet samples/cpp/tutorial_code/features2D/AKAZE_match.cpp ratio test filtering
114 @end_toggle
115
116 @add_toggle_java
117 @snippet samples/java/tutorial_code/features2D/akaze_matching/AKAZEMatchDemo.java ratio test filtering
118 @end_toggle
119
120 @add_toggle_python
121 @snippet samples/python/tutorial_code/features2D/akaze_matching/AKAZE_match.py ratio test filtering
122 @end_toggle
123
124 If the closest match distance is significantly lower than the second closest one, then the match is correct (match is not ambiguous).
125
126 -   **Check if our matches fit in the homography model**
127
128 @add_toggle_cpp
129 @snippet samples/cpp/tutorial_code/features2D/AKAZE_match.cpp homography check
130 @end_toggle
131
132 @add_toggle_java
133 @snippet samples/java/tutorial_code/features2D/akaze_matching/AKAZEMatchDemo.java homography check
134 @end_toggle
135
136 @add_toggle_python
137 @snippet samples/python/tutorial_code/features2D/akaze_matching/AKAZE_match.py homography check
138 @end_toggle
139
140 If the distance from first keypoint's projection to the second keypoint is less than threshold,
141 then it fits the homography model.
142
143 We create a new set of matches for the inliers, because it is required by the drawing function.
144
145 -   **Output results**
146
147 @add_toggle_cpp
148 @snippet samples/cpp/tutorial_code/features2D/AKAZE_match.cpp draw final matches
149 @end_toggle
150
151 @add_toggle_java
152 @snippet samples/java/tutorial_code/features2D/akaze_matching/AKAZEMatchDemo.java draw final matches
153 @end_toggle
154
155 @add_toggle_python
156 @snippet samples/python/tutorial_code/features2D/akaze_matching/AKAZE_match.py draw final matches
157 @end_toggle
158
159 Here we save the resulting image and print some statistics.
160
161 Results
162 -------
163
164 ### Found matches
165
166 ![](images/res.png)
167
168 Depending on your OpenCV version, you should get results coherent with:
169
170 @code{.none}
171  Keypoints 1:   2943
172  Keypoints 2:   3511
173  Matches:       447
174  Inliers:       308
175  Inlier Ratio: 0.689038
176 @endcode