|
| 1 | +const char* usage = R"( |
| 2 | +Form the APR form images: Takes an uint16_t input tiff images and forms the APRs and saves it as hdf5. |
| 3 | +The hdf5 output of this program can be used with the other apr examples, and also viewed with HDFView. |
| 4 | +
|
| 5 | +Usage: |
| 6 | +====== |
| 7 | +Example_get_multiapr -d input_directory [-od output_direcotry] |
| 8 | +
|
| 9 | +Additional settings (High Level): |
| 10 | +================================= |
| 11 | +-I_th intensity_threshold (will ignore areas of image below this threshold, useful for removing camera artifacts or auto-fluorescence) |
| 12 | +-sigma_th lower threshold for the local intensity scale |
| 13 | +-grad_th ignore areas in the image where the gradient magnitude is lower than this value |
| 14 | +
|
| 15 | +Advanced (Direct) Settings: |
| 16 | +=========================== |
| 17 | +-lambda lambda_value (directly set the value of the gradient smoothing parameter lambda (reasonable range 0.1-10, default: 3) |
| 18 | +-rel_error rel_error_value (Reasonable ranges are from .08-.15), Default: 0.1 |
| 19 | +-neighborhood_optimization_off turns off the neighborhood optimization (This results in boundary Particle Cells also being increased in resolution after the Pulling Scheme step) |
| 20 | +)"; |
| 21 | + |
| 22 | +#include <algorithm> |
| 23 | +#include <iostream> |
| 24 | +#include "ConfigAPR.h" |
| 25 | +#include "io/APRFile.hpp" |
| 26 | +#include "data_structures/APR/particles/ParticleData.hpp" |
| 27 | +#include "data_structures/APR/APR.hpp" |
| 28 | +#include "algorithm/APRConverter.hpp" |
| 29 | + |
| 30 | + |
| 31 | +struct cmdLineOptions { |
| 32 | + std::string directory = ""; |
| 33 | + std::string output_dir = ""; |
| 34 | + |
| 35 | + float lambda = 3.0; |
| 36 | + float Ip_th = 0; |
| 37 | + float grad_th = 1; |
| 38 | + float sigma_th = 0; |
| 39 | + float rel_error = 0.1; |
| 40 | + |
| 41 | + bool neighborhood_optimization = true; |
| 42 | +}; |
| 43 | + |
| 44 | +bool command_option_exists(const char **begin, const char **end, const std::string &option) |
| 45 | +{ |
| 46 | + return std::find(begin, end, option) != end; |
| 47 | +} |
| 48 | + |
| 49 | +const char* get_command_option(const char **begin, const char **end, const std::string &option) |
| 50 | +{ |
| 51 | + if (const char** itr = std::find(begin, end, option); itr != end && ++itr != end) { |
| 52 | + return *itr; |
| 53 | + } |
| 54 | + return nullptr; |
| 55 | +} |
| 56 | + |
| 57 | +void printUsage() { |
| 58 | + std::cerr << "APR version " << ConfigAPR::APR_VERSION << std::endl <<std::endl; |
| 59 | + std::cerr << usage << std::endl; |
| 60 | + exit(1); |
| 61 | +} |
| 62 | + |
| 63 | +cmdLineOptions read_command_line_options(const int argc, const char **argv) { |
| 64 | + |
| 65 | + cmdLineOptions options; |
| 66 | + |
| 67 | + // --------- Print usage if no args provided |
| 68 | + if (argc == 1) printUsage(); |
| 69 | + |
| 70 | + // --------- Read params |
| 71 | + |
| 72 | + // Input Directory |
| 73 | + if (command_option_exists(argv, argv + argc, "-d")) { |
| 74 | + options.directory = std::string(get_command_option(argv, argv + argc, "-d")); |
| 75 | + } else { |
| 76 | + std::cout << "Input directory required" << std::endl; |
| 77 | + exit(2); |
| 78 | + } |
| 79 | + |
| 80 | + // Output Directory |
| 81 | + if (command_option_exists(argv, argv + argc, "-od")) { |
| 82 | + options.output_dir = std::string(get_command_option(argv, argv + argc, "-od")); |
| 83 | + } else { |
| 84 | + options.output_dir = options.directory; |
| 85 | + } |
| 86 | + |
| 87 | + if (command_option_exists(argv, argv + argc, "-lambda")) { |
| 88 | + options.lambda = std::stof(std::string(get_command_option(argv, argv + argc, "-lambda"))); |
| 89 | + } |
| 90 | + |
| 91 | + if (command_option_exists(argv, argv + argc, "-I_th")) { |
| 92 | + options.Ip_th = std::stof(std::string(get_command_option(argv, argv + argc, "-I_th"))); |
| 93 | + } |
| 94 | + |
| 95 | + if (command_option_exists(argv, argv + argc, "-grad_th")) { |
| 96 | + options.grad_th = std::stof(std::string(get_command_option(argv, argv + argc, "-grad_th"))); |
| 97 | + } |
| 98 | + |
| 99 | + if (command_option_exists(argv, argv + argc, "-sigma_th")) { |
| 100 | + options.sigma_th = std::stof(std::string(get_command_option(argv, argv + argc, "-sigma_th"))); |
| 101 | + } |
| 102 | + |
| 103 | + if (command_option_exists(argv, argv + argc, "-rel_error")) { |
| 104 | + options.rel_error = std::stof(std::string(get_command_option(argv, argv + argc, "-rel_error"))); |
| 105 | + } |
| 106 | + |
| 107 | + if (command_option_exists(argv, argv + argc, "-neighborhood_optimization_off")) { |
| 108 | + options.neighborhood_optimization = false; |
| 109 | + } |
| 110 | + |
| 111 | + return options; |
| 112 | +} |
| 113 | + |
| 114 | + |
| 115 | +int runAPR(const cmdLineOptions &options) { |
| 116 | + |
| 117 | + APRConverter<uint16_t> aprConverter; |
| 118 | + |
| 119 | + // read in the command line options into the parameters file |
| 120 | + aprConverter.par.input_dir = options.directory; |
| 121 | + aprConverter.par.output_dir = options.output_dir; |
| 122 | + |
| 123 | + aprConverter.par.lambda = options.lambda; |
| 124 | + aprConverter.par.Ip_th = options.Ip_th; |
| 125 | + aprConverter.par.grad_th = options.grad_th; |
| 126 | + aprConverter.par.sigma_th = options.sigma_th; |
| 127 | + aprConverter.par.rel_error = options.rel_error; |
| 128 | + |
| 129 | + aprConverter.par.neighborhood_optimization = options.neighborhood_optimization; |
| 130 | + |
| 131 | + |
| 132 | + |
| 133 | + // TODO: read here all input files instead of options.input |
| 134 | + PixelData<uint16_t> input_img = TiffUtils::getMesh<uint16_t>(options.directory + "TODO"); |
| 135 | + |
| 136 | + //Gets the APR |
| 137 | + if(APR apr; aprConverter.get_apr(apr, input_img)){ |
| 138 | + |
| 139 | + ParticleData<uint16_t> particle_intensities; |
| 140 | + particle_intensities.sample_image(apr, input_img); // sample your particles from your image |
| 141 | + |
| 142 | +#ifdef APR_USE_CUDA |
| 143 | + //Below is IO and outputting of the Implied Resolution Function through the Particle Cell level. |
| 144 | + std::cout << apr.linearAccess.y_vec.size() << " particles in APR" << std::endl; |
| 145 | + std::cout << particle_intensities.size() << " intensities in CPU in APR" << std::endl; |
| 146 | + std::cout << aprConverter.parts.size() << " intensities in GPU in APR" << std::endl; |
| 147 | + |
| 148 | + for (int i = 0 ; i < particle_intensities.size(); ++i) { |
| 149 | + if (particle_intensities[i] != aprConverter.parts[i]) { |
| 150 | + std::cout << "Mismatch at " << i << " CPU: " << particle_intensities[i] << " GPU: " << aprConverter.parts[i] << std::endl; |
| 151 | + } |
| 152 | + } |
| 153 | +#endif |
| 154 | + |
| 155 | + |
| 156 | + //output |
| 157 | + std::string save_loc = options.output_dir; |
| 158 | + // TODO Change file_name to currently processed input file and add ".apr" |
| 159 | + std::string file_name = "TODO_fileName"; |
| 160 | + |
| 161 | + APRTimer timer; |
| 162 | + |
| 163 | + timer.verbose_flag = true; |
| 164 | + |
| 165 | + std::cout << std::endl; |
| 166 | + float original_pixel_image_size = 2.0f * apr.org_dims(0) * apr.org_dims(1) * apr.org_dims(2) / 1000000.0f; |
| 167 | + std::cout << "Original image size: " << original_pixel_image_size << " MB" << std::endl; |
| 168 | + |
| 169 | + timer.start_timer("writing output"); |
| 170 | + |
| 171 | + std::cout << "Writing the APR to hdf5..." << std::endl; |
| 172 | + |
| 173 | + //write the APR to hdf5 file |
| 174 | + APRFile aprFile; |
| 175 | + |
| 176 | + aprFile.open(save_loc + file_name + ".apr"); |
| 177 | + |
| 178 | + aprFile.write_apr(apr, 0, "t", false); |
| 179 | + aprFile.write_particles("particles",particle_intensities); |
| 180 | + |
| 181 | + float apr_file_size = aprFile.current_file_size_MB(); |
| 182 | + |
| 183 | + timer.stop_timer(); |
| 184 | + |
| 185 | + float computational_ratio = 1.0f * apr.org_dims(0) * apr.org_dims(1) * apr.org_dims(2) / (1.0f * apr.total_number_particles()); |
| 186 | + |
| 187 | + std::cout << std::endl; |
| 188 | + std::cout << "Computational Ratio (Pixels/Particles): " << computational_ratio << std::endl; |
| 189 | + std::cout << "Lossy Compression Ratio: " << original_pixel_image_size/apr_file_size << std::endl; |
| 190 | + std::cout << std::endl; |
| 191 | + } else { |
| 192 | + std::cout << "Oops, something went wrong. APR not computed :(." << std::endl; |
| 193 | + } |
| 194 | + return 0; |
| 195 | +} |
| 196 | + |
| 197 | + |
| 198 | +int main(const int argc, const char **argv) { |
| 199 | + const cmdLineOptions options = read_command_line_options(argc, argv); |
| 200 | + const auto result = runAPR(options); |
| 201 | + |
| 202 | + return result; |
| 203 | +} |
0 commit comments