|
| 1 | +// |
| 2 | +// Created by bevan on 29/11/2020. |
| 3 | +// |
| 4 | + |
| 5 | +#ifndef LIBAPR_EXAMPLE_DENOISE_HPP |
| 6 | +#define LIBAPR_EXAMPLE_DENOISE_HPP |
| 7 | + |
| 8 | +#include <functional> |
| 9 | +#include <string> |
| 10 | + |
| 11 | +#include <io/TiffUtils.hpp> |
| 12 | +#include "data_structures/APR/APR.hpp" |
| 13 | +#include <algorithm> |
| 14 | +#include <iostream> |
| 15 | +#include <cmath> |
| 16 | + |
| 17 | +#include "data_structures/APR/particles/ParticleData.hpp" |
| 18 | +#include "io/APRFile.hpp" |
| 19 | +#include "numerics/APRDenoise.hpp" |
| 20 | + |
| 21 | + |
| 22 | +#include "ExampleHelpers.hpp" |
| 23 | + |
| 24 | +const char* usage = R"( |
| 25 | +Examples of denoising an APR |
| 26 | +
|
| 27 | +Usage: |
| 28 | +
|
| 29 | +(using *_apr.h5 output of Example_get_apr) |
| 30 | +
|
| 31 | +Example_denoise |
| 32 | +
|
| 33 | +Note: |
| 34 | +
|
| 35 | +)"; |
| 36 | + |
| 37 | +struct cmdLineOptionsDenoise{ |
| 38 | + std::string output = "output"; |
| 39 | + std::string stats = ""; |
| 40 | + std::string directory = ""; |
| 41 | + std::string input = ""; |
| 42 | + bool stats_file = false; |
| 43 | +}; |
| 44 | + |
| 45 | +bool denoise_example(cmdLineOptionsDenoise& options); |
| 46 | + |
| 47 | +cmdLineOptionsDenoise read_command_line_options(int argc, char **argv); |
| 48 | + |
| 49 | +bool denoise_example(cmdLineOptionsDenoise& options){ |
| 50 | + // Filename |
| 51 | + std::string file_name = options.directory + options.input; |
| 52 | + |
| 53 | + APRTimer timer; |
| 54 | + |
| 55 | + timer.verbose_flag = true; |
| 56 | + |
| 57 | + // APR data structure |
| 58 | + APR apr; |
| 59 | + ParticleData<uint16_t>parts; |
| 60 | + |
| 61 | + //read APR and particles from file |
| 62 | + timer.start_timer("read apr"); |
| 63 | + APRFile aprFile; |
| 64 | + aprFile.open(file_name,"READ"); |
| 65 | + aprFile.read_apr(apr); |
| 66 | + aprFile.read_particles(apr,parts); //default read, will take the first particles added to the file |
| 67 | + aprFile.close(); |
| 68 | + timer.stop_timer(); |
| 69 | + |
| 70 | + // reconstruct noisy input image by piecewise constant interpolation |
| 71 | + timer.start_timer("pc interp"); |
| 72 | + PixelData<uint16_t> recon_pc; |
| 73 | + APRReconstruction::interp_img(apr,recon_pc, parts); |
| 74 | + timer.stop_timer(); |
| 75 | + |
| 76 | + // save reconstructed input as TIFF |
| 77 | + std::string image_file_name = options.directory + apr.name + "_org.tif"; |
| 78 | + TiffUtils::saveMeshAsTiffUint16(image_file_name, recon_pc,false); |
| 79 | + |
| 80 | + /// Start of APR denoising |
| 81 | + timer.start_timer("train APR denoise"); |
| 82 | + |
| 83 | + // init denoising stencils |
| 84 | + APRStencils aprStencils; |
| 85 | + auto it = apr.iterator(); |
| 86 | + aprStencils.dim = it.number_dimensions(); //get the dimension from the file. |
| 87 | + |
| 88 | + // learn stencil weights |
| 89 | + APRDenoise aprDenoise; |
| 90 | + aprDenoise.verbose = false; |
| 91 | + aprDenoise.train_denoise(apr,parts,aprStencils); |
| 92 | + timer.stop_timer(); |
| 93 | + |
| 94 | + |
| 95 | + // apply denoising stencils |
| 96 | + timer.start_timer("apply APR denoise"); |
| 97 | + ParticleData<uint16> parts_denoised; |
| 98 | + aprDenoise.apply_denoise(apr,parts,parts_denoised,aprStencils); |
| 99 | + timer.stop_timer(); |
| 100 | + |
| 101 | + // reconstruct image from denoised particles by piecewise constant interpolation |
| 102 | + timer.start_timer("pc interp"); |
| 103 | + APRReconstruction::interp_img(apr,recon_pc, parts_denoised); |
| 104 | + timer.stop_timer(); |
| 105 | + |
| 106 | + // save denosied image as TIFF |
| 107 | + image_file_name = options.directory + apr.name + "_denoised.tif"; |
| 108 | + TiffUtils::saveMeshAsTiff(image_file_name, recon_pc,false); |
| 109 | + |
| 110 | + // write APR and denoised particles to file |
| 111 | + timer.start_timer("write denoised APR to file"); |
| 112 | + file_name = options.directory + options.output; |
| 113 | + aprFile.open(file_name,"WRITE"); |
| 114 | + aprFile.write_apr(apr); |
| 115 | + aprFile.write_particles("particles",parts_denoised); |
| 116 | + aprFile.close(); |
| 117 | + timer.stop_timer(); |
| 118 | + |
| 119 | + return true; |
| 120 | +} |
| 121 | + |
| 122 | +cmdLineOptionsDenoise read_command_line_options(int argc, char **argv){ |
| 123 | + |
| 124 | + cmdLineOptionsDenoise result; |
| 125 | + |
| 126 | + if(argc == 1) { |
| 127 | + std::cerr << "Usage: \"Example_apr_iterate -i input_apr_file -d directory\"" << std::endl; |
| 128 | + std::cerr << usage << std::endl; |
| 129 | + exit(1); |
| 130 | + } |
| 131 | + |
| 132 | + if(command_option_exists(argv, argv + argc, "-i")) |
| 133 | + { |
| 134 | + result.input = std::string(get_command_option(argv, argv + argc, "-i")); |
| 135 | + } else { |
| 136 | + std::cout << "Input file required" << std::endl; |
| 137 | + exit(2); |
| 138 | + } |
| 139 | + |
| 140 | + if(command_option_exists(argv, argv + argc, "-d")) |
| 141 | + { |
| 142 | + result.directory = std::string(get_command_option(argv, argv + argc, "-d")); |
| 143 | + } |
| 144 | + |
| 145 | + if(command_option_exists(argv, argv + argc, "-o")) |
| 146 | + { |
| 147 | + result.output = std::string(get_command_option(argv, argv + argc, "-o")); |
| 148 | + } |
| 149 | + |
| 150 | + return result; |
| 151 | + |
| 152 | +} |
| 153 | + |
| 154 | + |
| 155 | + |
| 156 | +#endif //LIBAPR_EXAMPLE_DENOISE_HPP |
0 commit comments