-
Notifications
You must be signed in to change notification settings - Fork 0
/
Example_compress_apr.cpp
205 lines (141 loc) · 6.22 KB
/
Example_compress_apr.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
//////////////////////////////////////////////////////
///
/// Bevan Cheeseman 2018
///
const char* usage = R"(
APR Compression example:
Example of performing additional compression on the APR intensities (APR adaptation of the within noise lossy compression from Balázs et al. 2017, A real-time compression library for
microscopy images)
Usage:
(using *.apr output of Example_get_apr)
Example_compress_apr -i input_apr_hdf5 -d input_directory
Note: inpute file path is 'input_apr_hdf5 + input_directory'
writes the compressed APR to a file *_compressed.apr in the input directory
optionally writes a reconstructed TIFF image to *_compressed.tif (if the flag -output_tiff is given)
Optional:
-compress_type number (1 or 2) (1 - WNL compression, only variance stabalization step (Default), 2 - variance stabalization and x,y,z prediction (note slower for ~30% compression gain)
-quantization_level (Default 1: higher increasing the loss nature of the WNL compression aproach)
-compress_level (the IO uses BLOSC for lossless compression of the APR, this can be set from 1-9, where higher increases the compression level. Note, this can come at a significant time increase.)
e.g. Example_compress_apr -i nuclei.apr -d /Test/Input_examples/ -compress_type 1
Note: fine grained parameters can be tuned within the file, to play with lossless compression level, method used, and other parameters.
)";
#include <algorithm>
#include <iostream>
#include "Example_compress_apr.h"
#include "io/TiffUtils.hpp"
#include "data_structures/APR/particles/ParticleData.hpp"
#include "io/APRFile.hpp"
#include "numerics/APRReconstruction.hpp"
int main(int argc, char **argv) {
// INPUT PARSING
cmdLineOptions options = read_command_line_options(argc, argv);
// Filename
std::string file_name = options.directory + options.input;
std::string name = options.input;
name.erase(name.end()-4,name.end());
std::string output_file_name = options.directory + name + "_compressed.apr";
// Read the apr file into the part cell structure
APRTimer timer;
timer.verbose_flag = true;
// APR datastructure
APR apr;
//read file
APRFile aprFile;
aprFile.open(file_name,"READ");
aprFile.read_apr(apr);
ParticleData<uint16_t>parts;
aprFile.read_particles(apr,parts);
aprFile.close();
APRFile compFile;
compFile.open(output_file_name,"WRITE");
parts.compressor.set_quantization_factor(options.quantization_level); //set this to adjust the compression factor for WNL
parts.compressor.set_compression_type(options.compress_type);
//set the background to the minimum
float background = *std::min_element(parts.data.begin(),parts.data.end());
parts.compressor.set_background(background);
compFile.write_apr(apr);
//compress the APR and write to disk
timer.start_timer("compress and write");
compFile.write_particles("particles",parts);
timer.stop_timer();
float time_write = timer.timings.back();
float original_pixel_image_size = (2.0f*apr.org_dims(0)*apr.org_dims(1)*apr.org_dims(2))/(1000000.0f);
std::cout << std::endl;
std::cout << std::endl;
std::cout << "Original image size: " << original_pixel_image_size << " MB" << std::endl;
float apr_compressed_file_size = compFile.current_file_size_MB();
compFile.close();
timer.start_timer("decompress and read");
compFile.open(output_file_name,"READ");
compFile.read_particles(apr,"particles",parts);
compFile.close();
timer.stop_timer();
float time_read = timer.timings.back();
std::cout << "Compressed (Lossy - WNL) APR: " << apr_compressed_file_size << " MB" << std::endl;
std::cout << "Compression Ratio: " << original_pixel_image_size/apr_compressed_file_size << std::endl;
std::cout << std::endl;
std::cout << std::endl;
std::cout << "Effective Datarate Write (by original image size): " << original_pixel_image_size/time_write << " MB*/s" << std::endl;
std::cout << "Effective Datarate Read (by original image size): " << original_pixel_image_size/time_read << " MB*/s" << std::endl;
std::cout << std::endl;
std::cout << std::endl;
//writes the piece-wise constant reconstruction of the APR to file for comparison
if(options.output_tiff) {
PixelData<uint16_t> img;
APRReconstruction::interp_img(apr,img, parts);
std::string output = options.directory + name + "_compressed.tif";
TiffUtils::saveMeshAsTiff(output, img);
}
}
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 nullptr;
}
cmdLineOptions read_command_line_options(int argc, char **argv){
cmdLineOptions result;
if(argc == 1) {
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, "-compress_type"))
{
result.compress_type = (unsigned int)std::stoi(std::string(get_command_option(argv, argv + argc, "-compress_type")));
}
if(result.compress_type > 2){
std::cerr << "Invalid Compression setting (0,1 or 2)" << std::endl;
exit(1);
}
if(command_option_exists(argv, argv + argc, "-quantization_level"))
{
result.quantization_level =std::stof(std::string(get_command_option(argv, argv + argc, "-quantization_level")));
}
if(command_option_exists(argv, argv + argc, "-compress_level"))
{
result.compress_level = (unsigned int)std::stoi(std::string(get_command_option(argv, argv + argc, "-compress_level")));
}
if(command_option_exists(argv, argv + argc, "-output_tiff"))
{
result.output_tiff = true;
}
return result;
}