-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprocessImages.cpp
287 lines (238 loc) · 11.1 KB
/
processImages.cpp
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
/* ============================================================================================
processImages.cpp Version 3 04/07/2017 Arthur Koehl
This program reads in an input directory contianing a set of images, processes them,
computes features and descriptors and outputs them to YAML format files in output directory
============================================================================================ */
#include <iostream>
#include <dirent.h>
#include <iomanip>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include "opencv2/highgui/highgui.hpp"
#include "opencv2/calib3d/calib3d.hpp"
#include "opencv2/features2d/features2d.hpp"
#include "opencv2/nonfree/nonfree.hpp"
using namespace std;
using namespace cv;
int usage ();
void read_flags(int argc, char** argv, string *path2dir, string *path2outdir, string *param, int *minh, int *octaves, int *layers, int *sizemin, double *responsemin);
void read_surfparams (string param, int *minh, int *octaves, int *layers, int *sizemin, double *responsemin);
int get_filelist (string path, vector <string> &allfiles);
void filter_keypoints (vector <KeyPoint> &keypoints, int sizemin, double responsemin);
int main(int argc, char **argv)
{
/* ===============================================================================================
Display usage if necessary
=============================================================================================== */
if (argc < 2)
return usage();
string argument = argv[1];
if (argument == "-h" || argument == "-help")
return usage();
/* ===============================================================================================
(1) Initialize all variables and surf parameters (2) parse command line (3) read in parameters
=============================================================================================== */
string path2dir, path2outdir;
string param = "";
int minh = 2000, octaves = 5, layers = 5;
int sizemin = 50;
double responsemin = 100;
string name, nameful;
string extension = ".yml";
read_flags (argc, argv, &path2dir, &path2outdir, ¶m, &minh, &octaves, &layers, &sizemin, &responsemin);
if (param != "")
read_surfparams (param, &minh, &octaves, &layers, &sizemin, &responsemin);
/* ===============================================================================================
Create all structures needed for SURF key point detection and feature extraction
=============================================================================================== */
SurfFeatureDetector detector (minh, octaves, layers);
Ptr<DescriptorExtractor> extractor = new SurfDescriptorExtractor();
vector <KeyPoint> keypoints;
Mat descriptors;
/* ===============================================================================================
Got to input directory, (1) get the file list (2) keep only image files (.jpg extension)
=============================================================================================== */
vector <string> allfiles;
vector <string> files; //remaining images
int error = get_filelist (path2dir, allfiles);
if ( error != 0)
{
cout << "no files in directory" << endl;
return -1;
}
//filter for image files
for (int i = 0; i < allfiles.size(); i++)
{
string filename = allfiles[i];
if (filename.substr (filename.find_last_of(".") + 1) == "jpg")
files.push_back (filename);
}
/* ===============================================================================================
For each image file:
(1) generate output file name
(2) detect keypoints
(3) filter them
(4) compute descriptors
(5) write in YAML format the keypoints and descriptors to output file
=============================================================================================== */
for (int i = 0; i < files.size(); i++)
{
if (i % 100 == 0 || i == files.size())
cout << "Processing image # " << setw(4) << i << " out of " << files.size() << " files" << endl;
//read in image file and generate output file name
name = path2dir + files[i];
nameful = files[i];
nameful.erase(nameful.find_last_of("."), string::npos);
nameful = path2outdir + nameful + extension;
Mat image = imread (name);
//SURF detection and then filter
detector.detect (image, keypoints);
//cout << "keypoints: " << keypoints.size();
filter_keypoints (keypoints, sizemin, responsemin);
//cout << " after filter: " << keypoints.size() << endl;
//write into output file
FileStorage fs (nameful, FileStorage::WRITE);
fs << "keypoints" << keypoints;
if (keypoints.size() > 0)
{
extractor->compute (image, keypoints, descriptors);
fs << "descriptors" << descriptors;
}
fs.release();
}
cout << "Processed all " << files.size() << " images, and placed the .yml files in " << path2outdir << endl;
return 0;
}
/* ===============================================================================================
Procedure that generates the usage for the code, only called if incorrectly run
=============================================================================================== */
int usage()
{
cout << "\n\n" <<endl;
cout << " " << "================================================================================================" << endl;
cout << " " << "================================================================================================" << endl;
cout << " " << "= =" << endl;
cout << " " << "= ProcessImages =" << endl;
cout << " " << "= =" << endl;
cout << " " << "= This program reads in a collection of images, finds keypoints, computers features =" << endl;
cout << " " << "= of those keypoints, and store them =" << endl;
cout << " " << "= =" << endl;
cout << " " << "= Usage is: =" << endl;
cout << " " << "= ./processImages.exe =" << endl;
cout << " " << "= -i <path to directory with images> =" << endl;
cout << " " << "= -o <path to output directory for keypoints> =" << endl;
cout << " " << "= -p <path to param file for SURF> =" << endl;
cout << " " << "= =" << endl;
cout << " " << "================================================================================================" << endl;
cout << " " << "================================================================================================" << endl;
cout << "\n\n" <<endl;
cout << "otherwise if not using a parameter file:" << endl;
cout << "./a.out -i -o -h -oct -l -s -r" << endl;
}
/* ===============================================================================================
Procedure to parse the command line options for the program
=============================================================================================== */
void read_flags(int argc, char** argv, string *path2dir, string *path2outdir, string *param, int *minh, int *octaves, int *layers, int *sizemin, double *responsemin)
{
string input;
for(int i = 1; i < argc; i++)
{
input = argv[i];
if (input == "-i")
*path2dir = argv[i + 1];
if (input == "-o")
*path2outdir = argv[i + 1];
if (input == "-p")
*param = argv[i + 1];
if (input == "-h")
*minh = atoi(argv[i+1]);
if (input == "-oct")
*octaves = atoi(argv[i+1]);
if (input == "-l")
*layers = atoi(argv[i+1]);
if (input == "-s")
*sizemin = atoi(argv[i+1]);
if (input == "-r")
*responsemin = atoi(argv[i+1]);
}
}
/* ===============================================================================================
Procedure to read parameters for SURF from the parameter file
=============================================================================================== */
void read_surfparams(string param, int *minHessian, int *octaves, int *octaveLayers, int *SizeMin, double *RespMin)
{
ifstream inFile;
inFile.open(param.c_str());
string record;
stringstream ss;
while ( !inFile.eof () ) {
getline(inFile,record);
if (record.find("minHessian") != std::string::npos) {
ss<<record.substr(record.find_last_of(":") + 1);
ss>> *minHessian;
ss.str("");
ss.clear();
}
if (record.find("octaves") != std::string::npos) {
ss<<record.substr(record.find_last_of(":") + 1);
ss>> *octaves;
ss.str("");
ss.clear();
}
if (record.find("octaveLayers") != std::string::npos) {
ss<<record.substr(record.find_last_of(":") + 1);
ss>> *octaveLayers;
ss.str("");
ss.clear();
}
if (record.find("min Size") != std::string::npos) {
ss<<record.substr(record.find_last_of(":") + 1);
ss>> *SizeMin;
ss.str("");
ss.clear();
}
if (record.find("min Resp") != std::string::npos) {
ss<<record.substr(record.find_last_of(":") + 1);
ss>> *RespMin;
ss.str("");
ss.clear();
}
}
}
/* ===============================================================================================
Procedure to extract list of files from a directory
=============================================================================================== */
int get_filelist (string path, vector <string> &allfiles)
{
//uses dirent.h to get the name of all the files within a directory
DIR *dp; //directory stream
struct dirent *entry; //directoryentry
if ((dp = opendir (path.c_str())) == NULL)
return -1;
while ((entry = readdir(dp)) != NULL)
allfiles.push_back (string (entry->d_name));
closedir (dp);
return 0;
}
/* ===============================================================================================
Procedure to filter the keypoints from the keypoint vector by minimum size and response
=============================================================================================== */
void filter_keypoints (vector <KeyPoint> &keypoints, int sizemin, double responsemin)
{
vector <KeyPoint> temp;
int npoints = keypoints.size();
int size;
double response;
//filter based on size and response size
for (int i = 0; i < npoints; i++)
{
size = keypoints[i].size;
response = keypoints[i].response;
if (size > sizemin && response > responsemin)
temp.push_back(keypoints[i]);
}
keypoints.clear();
keypoints = temp;
return;
}