-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.cpp
157 lines (135 loc) · 3.22 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
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
#include <iostream>
#include <cstdlib>
#include <cmath>
#include <cstdio>
#include <unistd.h>
#include <opencv2/opencv.hpp>
#include "graph.hpp"
#include "selectionrule.hpp"
#include "neighborhood.hpp"
#include "image.hpp"
#include "anisotropy.hpp"
using namespace std;
using namespace cv;
int main(int argc, char *argv[])
{
int p = 2;
double beta = 10;
int neighbors = 8;
double sigma = 10.0;
double rho = 10.0;
double gamma = 10000.0;
int c;
/* Read command line parameters beta and p. */
while ((c = getopt(argc, argv, "b:p:r:s:g:n:fh")) != -1) {
switch (c)
{
case 'p':
p = atoi(optarg);
break;
case 'b':
beta = atof(optarg);
break;
case 'g':
gamma = atof(optarg);
break;
case 'r':
rho = atof(optarg);
break;
case 's':
sigma = atof(optarg);
break;
case 'n':
neighbors = atoi(optarg);
break;
case '?':
if (optopt == 'p' || optopt == 'b' || optopt == 'g'
|| optopt == 'n' || optopt == 'r'
|| optopt == 's') {
fprintf(stderr, "Option -%c requires an argument.\n",
optopt);
}
else if (isprint(optopt)) {
fprintf(stderr, "Unknown option `-%c'.\n", optopt);
}
else {
fprintf(stderr, "Unknown option character `\\x%x'.\n",
optopt);
}
return 1;
default:
exit(1);
}
}
/*
* Non-option arguments are now in argv from index optind
* to index argc-1
*/
Mat image;
image = imread(argv[optind], CV_LOAD_IMAGE_GRAYSCALE);
if (!image.data) {
cout << "Loading image failed" << endl;
return -1;
}
cout << "Using gamma = " << gamma << endl;
cout << "Using rho = " << rho << endl;
cout << "Using sigma = " << sigma << endl;
Mat_<Tensor> tensors = Mat_<Tensor>::zeros(image.rows, image.cols);
Mat blur, edge, structure, color;
createAnisotropyTensor(tensors, image, sigma, rho, gamma,
blur, edge, structure, color);
imwrite(argv[optind + 1], blur);
imwrite(argv[optind + 2], edge);
imwrite(argv[optind + 3], structure);
imwrite(argv[optind + 4], color);
/*
* Network only handles integer edges, so we increase the scale a bit.
*/
int a;
int b;
a = 100;
b = beta;
/*
* Specify the neighbors of a pixel.
*/
cout << "Creating size " << neighbors << " neighborhood." << endl;
Neighborhood neigh;
if (neighbors >= 4) {
neigh.add( 1, 0, b * 1.0);
neigh.add( 0, 1, b * 1.0);
neigh.add(-1, 0, b * 1.0);
neigh.add( 0,-1, b * 1.0);
}
if (neighbors >= 8) {
neigh.add( 1, 1, b * 1.0/sqrt(2.0));
neigh.add(-1, 1, b * 1.0/sqrt(2.0));
neigh.add( 1,-1, b * 1.0/sqrt(2.0));
neigh.add(-1,-1, b * 1.0/sqrt(2.0));
}
if (neighbors >= 16) {
neigh.add8(1, 2, 1.0);
}
if (neighbors >= 32) {
neigh.add8(3, 1, 1.0);
neigh.add8(3, 2, 1.0);
}
if (neighbors >= 48) {
neigh.add8(1, 4, 1.0);
neigh.add8(3, 4, 1.0);
}
if (neighbors >= 72) {
neigh.add8(1, 5, 1.0);
neigh.add8(2, 5, 1.0);
neigh.add8(3, 5, 1.0);
}
cout << "Neighborhood: " << endl;
neigh.setupAngles();
for (Neighborhood::iterator it = neigh.begin(); it != neigh.end(); ++it) {
cout << it->x << ", " << it->y << ": " << it->dt * 180 / M_PI << endl;
}
Mat out = image.clone();
restoreAnisotropicTV(image, out, tensors, neigh, a, b, p);
cout << "Writing output to " << argv[optind + 5] << endl;
imwrite(argv[optind + 5], out);
return 0;
}