Learn how easy it is to sync an existing GitHub or Google Code repo to a SourceForge project! See Demo

Close

[71f058]: src / celeste / Main.cpp Maximize Restore History

Download this file

Main.cpp    588 lines (427 with data), 17.1 kB

  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
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
/***************************************************************************
* Copyright (C) 2008 by Tim Nugent *
* timnugent@gmail.com *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include <stdio.h>
#include <fstream>
#include <iostream>
#include <sstream>
#include <vector>
#include <cctype>
#include <string>
#include <algorithm>
#include "svm.h"
#include "CelesteGlobals.h"
#include "Utilities.h"
#include "Celeste.h"
#include <sys/stat.h>
#include "hugin_config.h"
#include <hugin_version.h>
#ifdef __WIN32__
#include <windows.h>
#endif
using namespace std;
bool fileexists(string strFilename) {
struct stat stFileInfo;
bool blnReturn;
int intStat;
// Attempt to get the file attributes
intStat = stat(strFilename.c_str(),&stFileInfo);
if(intStat == 0) {
blnReturn = true;
}else{
blnReturn = false;
}
return(blnReturn);
}
static void tokenize(const string& str, vector<string>& tokens, const string& delimiters = " "){
// Skip delimiters at beginning
string::size_type lastPos = str.find_first_not_of(delimiters, 0);
// Find first non-delimiter
string::size_type pos = str.find_first_of(delimiters, lastPos);
while (string::npos != pos || string::npos != lastPos){
// Found a token, add it to the vector
tokens.push_back(str.substr(lastPos, pos - lastPos));
// Skip delimiters. Note the not_of
lastPos = str.find_first_not_of(delimiters, pos);
// Find next non-delimiter
pos = str.find_first_of(delimiters, lastPos);
}
}
static void parse_pto(string& f,vector<string>& images,vector<string>& pto_file_top,vector<string>&
pto_file_cps,vector<string>& pto_file_end, unsigned int& mask, string& m, double& threshold,string&
mask_format,vector<double>& svm_responses){
// Create some vectors to store image and control point data
vector<unsigned int> xcps;
vector<unsigned int> ycps;
// Open the .pto file and make sure it's good to go
ifstream is(f.c_str());
if(!is.good()){
cout << "Couldn't open Hugin project file " << f << endl << endl;
exit(1);
}
// Get line from stream
string line;
unsigned int current = 0, counter = 0, at_cps = 0, done_cps = 0;
while(getline(is,line)){
string start = line.substr(0, 1);
string autopano_start = line.substr(0, 9);
if (at_cps == 0){
pto_file_top.push_back(line);
}
if (start == ("i") || autopano_start == ("#-imgfile")){
// Parse images from lines beginning 'i'
//vector<string> tokens;
//tokenize(line, tokens);
//string imagename = tokens.back().substr(2, tokens.back().length()-3);
size_t found;
string imagename = ("");
// Hugin files
if (start == ("i")){
found = line.find("n\"");
imagename = line.substr(found + 2, line.find("\"",found + 2) - found - 2);
//cout << imagename << endl;
// (Old?) Autopano Pro files
}else{
found = line.find("\"");
imagename = line.substr(found + 1, line.length() - found - 2);
}
if (fileexists(imagename)){
// Put filenames into a vector
images.push_back(imagename);
// Seach for the image if we can't find it, using path of PTO file
}else{
//cout << imagename << " doesn't exist." << endl;
vector<string> tokens;
imagename = ("");
// *nix file paths
if (f.find("/") < f.length() || f.substr(0, 1) == ("/")){
if (f.substr(0, 1) == ("/")){
imagename += ("/");
}
tokenize(f, tokens, "/");
for (unsigned int p = 0; p < tokens.size() - 1; p++){
imagename += tokens[p];
imagename += "/";
}
imagename += line.substr(found + 1, line.length() - found - 2);
// Windows file paths... experimental!
}else{
tokenize(f, tokens, "\\");
for (unsigned int p = 0; p < tokens.size() - 1; p++){
imagename += tokens[p];
imagename += "\\";
}
imagename += line.substr(found + 1, line.length() - found - 2);
}
//cout << "New search path " << imagename << endl;
if (fileexists(imagename)){
// Put filenames into a vector
images.push_back(imagename);
}else{
cout << "Couldn't open image " << line.substr(found + 2, line.length() - found -
3) << endl;
cout << "Also tried " << imagename << endl << endl;
exit(1);
}
}
//cout << imagename << endl;
}else if (start == ("c")){
pto_file_cps.push_back(line);
//cout << line << endl;
// Marker - we're at CPs
at_cps = 1;
vector<string> tokens;
tokenize(line, tokens);
// Image number for accessing images vector
unsigned int image_no = atoi( tokens[1].substr(1, tokens[1].length()-1).c_str() );
if (image_no != current){
// Create the storage matrix
gNumLocs = xcps.size();
gLocations = CreateMatrix( (int)0, gNumLocs, 2);
//cout << images[current] << " " << image_no << " (" << xcps.size() << "):" << endl;
for (int cp = 0; cp < gNumLocs; cp++){
//cout << "CP " << cp << ": " << xcps[cp] << "," << ycps[cp] << endl;
gLocations[cp][0] = xcps[cp];
gLocations[cp][1] = ycps[cp];
}
// Get response
get_gabor_response(images[current],mask,m,threshold,mask_format,svm_responses);
//gLocations = NULL;
delete[] gLocations;
xcps.clear();
ycps.clear();
current = image_no;
// First CP of next image
// Control point x
unsigned int xcp = atoi( tokens[3].substr(1, tokens[3].length()-1).c_str() );
xcps.push_back(xcp);
// Control point y
unsigned int ycp = atoi( tokens[4].substr(1, tokens[4].length()-1).c_str() );
ycps.push_back(ycp);
counter = 1;
}else{
// Control point x
unsigned int xcp = atoi( tokens[3].substr(1, tokens[3].length()-1).c_str() );
xcps.push_back(xcp);
// Control point y
unsigned int ycp = atoi( tokens[4].substr(1, tokens[4].length()-1).c_str() );
ycps.push_back(ycp);
counter++;
//cout << images[image_no] << " : " << xcp << "," << ycp << endl;
}
}else if (at_cps && start != ("c") && start != ("#") && done_cps == 0){
// Create the storage matrix
gNumLocs = xcps.size();
gLocations = CreateMatrix( (int)0, gNumLocs, 2);
//cout << images[current] << " " << current << " (" << xcps.size() << "):" << endl;
for (unsigned int cp = 0; cp < counter; cp++){
//cout << "CP " << cp << ": " << xcps[cp] << "," << ycps[cp] << endl;
gLocations[cp][0] = xcps[cp];
gLocations[cp][1] = ycps[cp];
}
// Get response
get_gabor_response(images[current],mask,m,threshold,mask_format,svm_responses);
//gLocations = NULL;
delete[] gLocations;
xcps.clear();
ycps.clear();
done_cps = 1;
}
if (at_cps && start != ("c")){
pto_file_end.push_back(line);
}
}
// Process last image - create mask if necessary
//if (images.size() > 1){
// gNumLocs = 0;
// get_gabor_response(images.back(),mask,m,threshold,mask_format,svm_responses);
//}
// Close stream
is.close();
}
static void write_pto(double& threshold,vector<string>& images,string& output_pto,vector<string>&
pto_file_top,vector<string>& pto_file_cps,vector<string>& pto_file_end,vector<double> svm_responses){
// File to write to
ofstream out;
out.open (output_pto.c_str());
if(!out.good()){
cout << "Couldn't write to Hugin project file " << output_pto << endl << endl;
exit(1);
}
// Print the top of the file
for (unsigned int l = 0; l < pto_file_top.size() - 1; l++){
out << pto_file_top[l] << endl;
}
unsigned int current = 0;
cout << images[current] << ":" << endl;
// Print control points
for (unsigned int l = 0; l < pto_file_cps.size(); l++){
//cout << l << ": " << pto_file_cps[l] << endl;
vector<string> tokens;
tokenize(pto_file_cps[l], tokens);
unsigned int image_no = atoi( tokens[1].substr(1, tokens[1].length()-1).c_str() );
if (image_no != current){
cout << endl << images[image_no] << ":" << endl;
}
current = image_no;
cout << "CP ";
unsigned int len = 0;
for (unsigned int t = 1; t < 7; t++){
len += tokens[t].length();
cout << tokens[t] << " ";
}
//cout << pto_file_cps[l] << "\t";
unsigned int count_spaces = 1;
int t = 80 - len;
if (t > 0) count_spaces = t;
for (unsigned int s = 0; s < count_spaces; s++){
cout << " ";
}
if (svm_responses[l] >= threshold){
cout << "Removed (score " << svm_responses[l] << " >= " << threshold << ")" << endl;
}else{
cout << "OK (score " << svm_responses[l] << " < " << threshold << ")" << endl;
// Write to file
out << pto_file_cps[l] << endl;
}
}
// Print bottom of file
for (unsigned int l = 0; l < pto_file_end.size(); l++){
out << pto_file_end[l] << endl;
}
out.close();
}
static void usage(){
// Print usage and exit
cout << endl << "Celeste: Removes cloud-like control points from Hugin project files and creates image masks" << endl;
cout << "using Support Vector Machines." << endl;
cout << endl << "Version " << DISPLAY_VERSION << endl;
cout << endl << "Usage: celeste_standalone [options] image1 image2 [..]" << endl << endl;
cout << "Options:" << endl << endl;
cout << " -i <filename> Input Hugin PTO file. Control points over SVM threshold will" << endl;
cout << " be removed before being written to the output file. If -m is" << endl;
cout << " set to 1, images in the file will be also be masked." << endl;
cout << " -o <filename> Output Hugin PTO file. Default: '<filename>_celeste.pto'" << endl;
cout << " -d <filename> SVM model file. Default: 'data/celeste.model'" << endl;
cout << " -s <int> Maximum dimension for re-sized image prior to processing. A" << endl;
cout << " higher value will increase the resolution of the mask but is" << endl;
cout << " significantly slower. Default: 800" << endl;
cout << " -t <float> SVM threshold. Raise this value to remove fewer control points," << endl;
cout << " lower it to remove more. Range 0 to 1. Default: 0.5" << endl;
cout << " -m <1|0> Create masks when processing Hugin PTO file. Default: 0" << endl;
cout << " -f <string> Mask file format. Options are PNG, JPEG, BMP, GIF and TIFF." << endl;
cout << " Default: PNG" << endl;
cout << " -r <1|0> Filter radius. 0 = large (more accurate), 1 = small (higher" << endl;
cout << " resolution mask, slower, less accurate). Default: 0" << endl;
cout << " -h Print usage." << endl;
cout << " image1 image2.. Image files to be masked." << endl << endl;
exit(1);
}
int main(int argc, const char* argv[]){
// Exit with usage unless filename given as argument
if (argc < 2){
usage();
}
unsigned int i = 1, mask = 0;
double threshold = 0.5;
vector<string> images_to_mask;
string pto_file = (""),output_pto = ("");
string mask_format = ("PNG");
string model_file = ("celeste.model");
int course_fine = 0;
// Deal with arguments
while(i < argc){
if( argv[i][0] == '-'){
if (argc == 2){
usage();
}
i++;
switch(argv[i-1][1]){
// Put some more argument options in here later
case 'h' : {usage();}
case 'i' : {pto_file += argv[i]; break;}
case 'o' : {output_pto += argv[i]; break;}
case 't' : {threshold = atof(argv[i]); break;}
case 'm' : {mask = atoi(argv[i]); break;}
case 'f' : {mask_format = argv[i]; break;}
case 'd' : {model_file = argv[i]; break;}
case 'r' : {course_fine = atoi(argv[i]); break;}
case 's' : {resize_dimension = atoi(argv[i]); break;}
}
}else{
images_to_mask.push_back(argv[i]);
}
i++;
}
// Check model file
if (!fileexists(model_file)){
#if __WIN32__
char buffer[MAX_PATH];//always use MAX_PATH for filepaths
GetModuleFileName(NULL,buffer,sizeof(buffer));
string working_path=(buffer);
string install_path_model="";
//remove filename
std::string::size_type pos=working_path.rfind("\\");
if(pos!=std::string::npos)
{
working_path.erase(pos);
//remove last dir: should be bin
pos=working_path.rfind("\\");
if(pos!=std::string::npos)
{
working_path.erase(pos);
//append path delimiter and path
working_path.append("\\share\\hugin\\data\\");
install_path_model=working_path;
}
}
#else
string install_path_model = (INSTALL_DATA_DIR);
#endif
install_path_model.append(model_file);
if (!fileexists(install_path_model)){
cout << endl << "Couldn't open SVM model file " << model_file << endl;
cout << "Also tried " << install_path_model << endl << endl;
exit(1);
}else{
model_file = install_path_model;
}
}
// Set output .pto filename if not given
if (output_pto == ("") && pto_file != ("")){
output_pto = pto_file.substr(0,pto_file.length()-4).append("_celeste.pto");
}
// Convert mask format to upper case
transform(mask_format.begin(), mask_format.end(), mask_format.begin(),(int(*)(int)) toupper);
if (mask_format == ("JPG")){
mask_format = ("JPEG");
}
if (mask_format != ("PNG") &&mask_format != ("BMP") && mask_format != ("GIF") && mask_format != ("JPEG") && mask_format != ("TIFF")){
mask_format = ("TIFF");
}
// Print some stuff out
cout << endl << "Celeste: Removes cloud-like control points from Hugin project files and creates image masks" << endl;
cout << "using Support Vector Machines." << endl;
cout << endl << "Version " << DISPLAY_VERSION << endl << endl;
cout << "Arguments:" << endl;
cout << "Input Hugin file:\t" << pto_file << endl;
cout << "Output Hugin file:\t" << output_pto << endl;
cout << "SVM model file:\t\t" << model_file << endl;
cout << "Max dimension:\t\t" << resize_dimension << endl;
cout << "SVM threshold:\t\t" << threshold << endl;
cout << "Create PTO masks:\t";
if (mask){
cout << "Yes" << endl;
}else{
cout << "No" << endl;
}
cout << "Mask format:\t\t" << mask_format << endl;
cout << "Filter radius:\t\t";
// Mask resolution
if (course_fine){
gRadius = 10;
spacing = (gRadius * 2) + 1;
cout << "Small" << endl << endl;
}else{
cout << "Large" << endl << endl;
}
// Convert mask format to lower case
transform(mask_format.begin(), mask_format.end(), mask_format.begin(),(int(*)(int)) tolower);
// Vectors to store SVM responses and PTO file info etc
vector<string> images,pto_file_top,pto_file_cps,pto_file_end;
vector<double> svm_responses;
// Mask images
if (images_to_mask.size()){
cout << "Masking images..." << endl << endl;
unsigned int n = 1;
for (unsigned int l = 0; l < images_to_mask.size(); l++){
gNumLocs = 0;
get_gabor_response(images_to_mask[l],n,model_file,threshold,mask_format,svm_responses);
}
}
// Process PTO file
if (pto_file != ("")){
cout << "Parsing Hugin project file " << pto_file << endl << endl;
parse_pto(pto_file,images,pto_file_top,pto_file_cps,pto_file_end,mask,model_file,threshold,mask_format,svm_responses);
// Prune CPs and write new pto file
write_pto(threshold,images,output_pto,pto_file_top,pto_file_cps,pto_file_end,svm_responses);
cout << endl << "Written file " << output_pto << endl << endl;
}
return(1);
}