-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.cpp
105 lines (85 loc) · 2.54 KB
/
main.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
#include "image.h"
#include "image_ops.h"
#include "vec.h"
#include "canny.h"
#include <iostream>
#include <filesystem>
#include <algorithm>
#include <vector>
#include <functional>
#include <cmath>
#include <cfloat>
using namespace std;
namespace fs = filesystem;
struct Args {
fs::path inputPath;
fs::path outputPath;
fs::path debugOutputPath;
float th = 0.1;
float tl = 0.3;
};
void usage(const string& appName, ostream& os) {
os << "Usage: " << appName << " -o outputFile intputFile "
<< "[-d debugOutputDir -th lowThreshold -th highThreshold]" << endl;
}
bool parseArgs(int argc, char* argv[], Args* pathsPtr) {
Args& args = *pathsPtr;
for (int i = 1; i < argc;) {
if (argv[i] == string("-o") && i + 1 < argc) {
args.outputPath = fs::path(argv[i + 1]);
i += 2;
} else if (argv[i] == string("-d") && i + 1 < argc) {
args.debugOutputPath = fs::path(argv[i + 1]);
i += 2;
} else if (argv[i] == string("-tl") && i + 1 < argc) {
try {
float tl = stof(argv[i + 1]);
args.tl = tl;
} catch (std::invalid_argument const& ex) {
cerr << ex.what() << endl;
return false;
}
i += 2;
} else if (argv[i] == string("-th") && i + 1 < argc) {
try {
float th = stof(argv[i + 1]);
args.th = th;
} catch (std::invalid_argument const& ex) {
cerr << ex.what() << endl;
return false;
}
i += 2;
} else {
if (!args.inputPath.empty()) {
return false;
}
args.inputPath = fs::path(argv[i]);
++i;
}
}
return !(args.inputPath.empty() || args.outputPath.empty());
}
int main(int argc, char* argv[]) {
if (argc < 4) {
usage(argv[0], cerr);
return 1;
}
Args args;
if (!parseArgs(argc, argv, &args)) {
usage(argv[0], cerr);
return 1;
}
EdgeFinder edgeFinder;
if (!edgeFinder.readImage(args.inputPath)) {
cerr << "Failed to read: " << args.inputPath << endl;
return 1;
}
edgeFinder.calcGrads();
edgeFinder.calcNonMaxSuppression(args.th, args.tl);
Image edgeImage = invert(edgeFinder.getLines().toUint8());
if (!edgeImage.writePng(args.outputPath)) {
cerr << "Failed to write: " << args.outputPath << endl;
return 1;
}
return 0;
}