-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathExample_compute_gradient.cpp
174 lines (129 loc) · 4.63 KB
/
Example_compute_gradient.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
//
// Created by cheesema on 21.01.18.
//
const char* usage = R"(
Example calculating the gradient magnitude of an APR and saving the result as a reconstructed TIFF image
Usage:
(using *.apr output of Example_get_apr)
Example_compute_gradient -i input_apr_hdf5 -d directory -o output_tiff_file
Note: input_file_path = directory + input_apr_hdf5
output_file_path = directory + output_tiff_file + ".tif"
if argument -o is not given, no file is written
Options:
-dx value
-dy value voxel size in each dimension (float)
-dz value
-sobel if this flag is given, gradients are computed using Sobel filters. (otherwise central finite differences are used)
)";
#include "Example_compute_gradient.hpp"
int main(int argc, char **argv) {
// INPUT PARSING
cmdLineOptions options = read_command_line_options(argc, argv);
std::string file_name = options.directory + options.input;
APRTimer timer(true);
timer.start_timer("Read APR and particles from file");
APR apr;
ParticleData<uint16_t> parts;
//read file
APRFile aprFile;
aprFile.open(file_name,"READ");
aprFile.read_apr(apr);
aprFile.read_particles(apr,parts);
timer.stop_timer();
//Calculate the gradient of the APR
timer.start_timer("compute APR gradient");
ParticleData<float> output;
std::vector<float> deltas = {options.dy, options.dx, options.dz};
bool done = false;
if(options.use_cuda) {
#ifdef APR_USE_CUDA
auto access = apr.gpuAPRHelper();
auto tree_access = apr.gpuTreeHelper();
if(options.sobel) {
APRNumericsGPU::gradient_magnitude_sobel(access, tree_access, parts.data, output.data, deltas);
} else {
APRNumericsGPU::gradient_magnitude_cfd(access, tree_access, parts.data, output.data, deltas);
}
done = true;
#else
std::cout << "Option -use_cuda was given, but LibAPR was not built with CUDA enabled. Using CPU implementation." << std::endl;
#endif
}
if(!done) {
if (options.sobel) {
APRNumerics::gradient_magnitude_sobel(apr, parts, output, deltas);
} else {
APRNumerics::gradient_magnitude_cfd(apr, parts, output, deltas);
}
}
timer.stop_timer();
if(options.output.length() > 0) {
// reconstruct pixel image from gradient
timer.start_timer("reconstruct pixel image");
PixelData<float> gradient_magnitude_image;
APRReconstruction::reconstruct_constant(apr, gradient_magnitude_image, output);
timer.stop_timer();
timer.start_timer("write pixel image to file");
std::string image_file_name = options.directory + options.output + ".tif";
TiffUtils::saveMeshAsTiff(image_file_name, gradient_magnitude_image);
timer.stop_timer();
}
}
bool command_option_exists(char **begin, char **end, const std::string &option)
{
return std::find(begin, end, option) != end;
}
char* get_command_option(char **begin, char **end, const std::string &option)
{
char ** itr = std::find(begin, end, option);
if (itr != end && ++itr != end)
{
return *itr;
}
return 0;
}
cmdLineOptions read_command_line_options(int argc, char **argv){
cmdLineOptions result;
if(argc == 1) {
std::cerr << argv[0] << std::endl;
std::cerr << "Short Usage: \"Example_compute_gradient -i input_apr_file -d directory\"" << std::endl;
std::cerr << usage << std::endl;
exit(1);
}
if(command_option_exists(argv, argv + argc, "-i"))
{
result.input = std::string(get_command_option(argv, argv + argc, "-i"));
} else {
std::cout << "Input file required" << std::endl;
exit(2);
}
if(command_option_exists(argv, argv + argc, "-d"))
{
result.directory = std::string(get_command_option(argv, argv + argc, "-d"));
}
if(command_option_exists(argv, argv + argc, "-o"))
{
result.output = std::string(get_command_option(argv, argv + argc, "-o"));
}
if(command_option_exists(argv, argv + argc, "-sobel"))
{
result.sobel = true;
}
if(command_option_exists(argv, argv + argc, "-dx"))
{
result.dx = std::stof(get_command_option(argv, argv + argc, "-dx"));
}
if(command_option_exists(argv, argv + argc, "-dy"))
{
result.dy = std::stof(get_command_option(argv, argv + argc, "-dy"));
}
if(command_option_exists(argv, argv + argc, "-dz"))
{
result.dz = std::stof(get_command_option(argv, argv + argc, "-dz"));
}
if(command_option_exists(argv, argv + argc, "-use_cuda"))
{
result.use_cuda = true;
}
return result;
}