-
Notifications
You must be signed in to change notification settings - Fork 31
/
main.py
175 lines (135 loc) · 5.4 KB
/
main.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#!/usr/bin/env python
# coding: utf-8
# Feature Description and Matching
# Imports
import argparse
import cv2 as cv
import features
import globals
import numpy as np
import outputs
import save_figures
# Initializing global variables
globals.initialize()
# Message from usage
message = '''main.py [-h]
--detector {SIFT, SURF, KAZE, ORB, BRISK,AKAZE}
--descriptor {SIFT, SURF, KAZE, BRIEF, ORB, BRISK, AKAZE, FREAK}
--matcher {BF, FLANN}'''
# Create the parser
parser = argparse.ArgumentParser(description = 'Feature Description and Matching.',
usage = message)
# Argument --detector
parser.add_argument('--detector',
action = 'store',
choices = ['SIFT', 'SURF', 'KAZE', 'ORB', 'BRISK', 'AKAZE'],
required = True,
metavar = '',
dest = 'detector',
help = 'select the detector to be used in this experiment')
# Argument --descriptor
parser.add_argument('--descriptor',
action = 'store',
choices = ['SIFT', 'SURF', 'KAZE', 'BRIEF', 'ORB', 'BRISK', 'AKAZE', 'FREAK'],
required = True,
metavar = '',
dest = 'descriptor',
help = 'select the descriptor to be used in this experiment')
# Argument --matcher
parser.add_argument('--matcher',
action = 'store',
choices = ['BF', 'FLANN'],
required = True,
metavar = '',
dest = 'matcher',
help = 'select the matcher to be used in this experiment')
# Execute the parse_args() method
arguments = parser.parse_args()
# Initiate Detector and Descriptor
# Initiate detector selected
if arguments.detector == 'SIFT':
globals.detector = features.SIFT()
elif arguments.detector == 'SURF':
globals.detector = features.SURF()
elif arguments.detector == 'KAZE':
globals.detector = features.SIFT()
elif arguments.detector == 'ORB':
globals.detector = features.ORB()
elif arguments.detector == 'BRISK':
globals.detector = features.BRISK()
elif arguments.detector == 'AKAZE':
globals.detector = features.AKAZE()
# Initiate descriptor selected
if arguments.descriptor == 'SIFT':
globals.descriptor = features.SIFT()
elif arguments.descriptor == 'SURF':
globals.descriptor = features.SURF()
elif arguments.descriptor == 'KAZE':
globals.descriptor = features.SIFT()
elif arguments.descriptor == 'BRIEF':
globals.descriptor = features.BRIEF()
elif arguments.descriptor == 'ORB':
globals.descriptor = features.ORB()
elif arguments.descriptor == 'BRISK':
globals.descriptor = features.BRISK()
elif arguments.descriptor == 'AKAZE':
globals.descriptor = features.AKAZE()
elif arguments.descriptor == 'FREAK':
globals.descriptor = features.FREAK()
# Open and Convert the input image from BGR to GRAYSCALE
image1 = cv.imread(filename = 'Figures/image1.jpg',
flags = cv.IMREAD_GRAYSCALE)
# Open and Convert the training-set image from BGR to GRAYSCALE
image2 = cv.imread(filename = 'Figures/image2.jpg',
flags = cv.IMREAD_GRAYSCALE)
# Could not open or find the images
if image1 is None or image2 is None:
print('\nCould not open or find the images.')
exit(0)
# Find the keypoints and compute
# the descriptors for input image
globals.keypoints1, globals.descriptors1 = features.features(image1)
# Print
print('\nInput image:\n')
# Print infos for input image
features.prints(keypoints = globals.keypoints1,
descriptor = globals.descriptors1)
# Find the keypoints and compute
# the descriptors for training-set image
globals.keypoints2, globals.descriptors2 = features.features(image2)
# Print
print('Training-set image:\n')
# Print infos for training-set image
features.prints(keypoints = globals.keypoints2,
descriptor = globals.descriptors2)
# Matcher
output = features.matcher(image1 = image1,
image2 = image2,
keypoints1 = globals.keypoints1,
keypoints2 = globals.keypoints2,
descriptors1 = globals.descriptors1,
descriptors2 = globals.descriptors2,
matcher = arguments.matcher,
descriptor = arguments.descriptor)
# Save Figure Matcher
save_figures.saveMatcher(output = output,
matcher = arguments.matcher,
descriptor = arguments.descriptor)
# Save keypoints and descriptors into a file
# from input image
outputs.saveKeypointsAndDescriptors(keypoints = globals.keypoints1,
descriptors = globals.descriptors1,
matcher = arguments.matcher,
descriptor = arguments.descriptor,
flags = 1)
# Save keypoints and descriptors into a file
# from training-set image
outputs.saveKeypointsAndDescriptors(keypoints = globals.keypoints2,
descriptors = globals.descriptors2,
matcher = arguments.matcher,
descriptor = arguments.descriptor,
flags = 2)
# Print
print('Done!\n')
# Print
print('Feature Description and Matching executed with success!')