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