Skip to content

Commit e94a62c

Browse files
authored
Merge pull request #139 from AdaptiveParticles/denoise_merge
Denoise merge
2 parents c3617f0 + 2dfa852 commit e94a62c

26 files changed

+1974
-48
lines changed

CMakeLists.txt

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ option(APR_PREFER_EXTERNAL_BLOSC "When found, use the installed BLOSC libs inste
1919
option(APR_USE_CUDA "should APR use CUDA? (experimental - under development)" OFF)
2020
option(APR_USE_OPENMP "should APR use OpenMP?" ON)
2121
option(APR_BENCHMARK "add benchmarking code" OFF)
22+
option(APR_DENOISE "enable denoising code" OFF)
2223

2324
# Validation of options
2425
if (NOT APR_BUILD_SHARED_LIB AND NOT APR_BUILD_STATIC_LIB)
@@ -42,6 +43,7 @@ set (APR_VERSION_MAJOR 2)
4243
set (APR_VERSION_MINOR 0)
4344
set (APR_VERSION_PATCH 0)
4445
set (APR_VERSION_STRING ${APR_VERSION_MAJOR}.${APR_VERSION_MINOR}.${APR_VERSION_PATCH})
46+
4547
execute_process(COMMAND git rev-parse HEAD
4648
WORKING_DIRECTORY ${PROJECT_SOURCE_DIR}
4749
OUTPUT_VARIABLE APR_GIT_HASH)
@@ -50,23 +52,36 @@ configure_file (
5052
"${PROJECT_SOURCE_DIR}/src/ConfigAPR.h.in"
5153
"${PROJECT_BINARY_DIR}/ConfigAPR.h"
5254
)
55+
5356
include_directories("${PROJECT_BINARY_DIR}")
5457
message("Configuring for APR version: " ${APR_VERSION_STRING})
5558

5659
###############################################################################
5760
# Find all required libraries
5861
###############################################################################
5962

63+
6064
#pthreads
6165
set(THREADS_PREFER_PTHREAD_FLAG ON)
6266
find_package(Threads REQUIRED)
6367

6468
find_package(HDF5 REQUIRED)
6569
find_package(ZLIB REQUIRED)
70+
71+
if(APR_DENOISE)
72+
find_package(Eigen3 REQUIRED)
73+
MESSAGE( [Main] " EIGEN3_INCLUDE_DIRS = ${EIGEN3_INCLUDE_DIRS}")
74+
include_directories(${EIGEN3_INCLUDE_DIRS})
75+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DAPR_DENOISE")
76+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DAPR_DENOISE")
77+
78+
endif()
79+
80+
6681
if(APR_USE_LIBTIFF)
6782
find_package(TIFF)
68-
set(CMAKE_C_FLAGS "${CMAKE_CFLAGS} -DHAVE_LIBTIFF")
69-
set(CMAKE_CXX_FLAGS "${CMAKE_CXXFLAGS} -DHAVE_LIBTIFF")
83+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_LIBTIFF")
84+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_LIBTIFF")
7085
endif()
7186

7287
# Handle OpenMP
@@ -285,6 +300,9 @@ endif(APR_INSTALL)
285300
###############################################################################
286301
if(APR_BUILD_EXAMPLES)
287302
message(STATUS "APR: Building examples")
303+
set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DHAVE_EXAMPLES")
304+
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DHAVE_EXAMPLES")
305+
288306
add_subdirectory(examples)
289307
endif(APR_BUILD_EXAMPLES)
290308

README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,12 @@ with the older version__.
4343
* CMake 3.6 or higher
4444
* LibTIFF 4.0 or higher
4545

46+
47+
NB: This update to 2.0 introduces changes to IO and iteration that are not compatable with old versions.
48+
49+
If compiling with APR_DENOISE flag the package also requires:
50+
* Eigen3.
51+
4652
## Building
4753

4854
The repository requires submodules, and needs to be cloned recursively:
@@ -76,6 +82,8 @@ cmake -DAPR_USE_OPENMP=OFF ..
7682

7783
On Ubuntu, install the `cmake`, `build-essential`, `libhdf5-dev` and `libtiff5-dev` packages (on other distributions, refer to the documentation there, the package names will be similar). OpenMP support is provided by the GCC compiler installed as part of the `build-essential` package.
7884

85+
For denoising support also requires: `libeigen3-dev`
86+
7987
In the directory of the cloned repository, run
8088

8189
```

examples/CMakeLists.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ buildTarget(Example_apr_tree)
1919

2020
buildTarget(Example_ray_cast)
2121

22+
if(APR_DENOISE)
23+
#requires eigen
24+
buildTarget(Example_denoise)
25+
endif()
26+
2227

examples/ExampleHelpers.hpp

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
//
2+
// Created by bevan on 29/11/2020.
3+
//
4+
5+
#ifndef LIBAPR_EXAMPLEHELPERS_H
6+
#define LIBAPR_EXAMPLEHELPERS_H
7+
8+
#include <iostream>
9+
10+
bool command_option_exists(char **begin, char **end, const std::string &option)
11+
{
12+
return std::find(begin, end, option) != end;
13+
}
14+
15+
char* get_command_option(char **begin, char **end, const std::string &option)
16+
{
17+
char ** itr = std::find(begin, end, option);
18+
if (itr != end && ++itr != end)
19+
{
20+
return *itr;
21+
}
22+
return 0;
23+
}
24+
25+
26+
27+
28+
#endif //LIBAPR_EXAMPLEHELPERS_H

examples/Example_apr_iterate.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ int main(int argc, char **argv) {
4848
aprFile.read_apr(apr);
4949

5050
ParticleData<uint16_t>parts;
51-
aprFile.read_particles(apr,"particles",parts);
51+
aprFile.read_particles(apr,parts);
5252

5353
aprFile.close();
5454
timer.stop_timer();

examples/Example_apr_tree.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ int main(int argc, char **argv) {
5353
aprFile.read_apr(apr);
5454

5555
ParticleData<uint16_t>parts;
56-
aprFile.read_particles(apr,"particles",parts);
56+
aprFile.read_particles(apr,parts);
5757

5858
aprFile.close();
5959

examples/Example_compress_apr.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ int main(int argc, char **argv) {
6767
aprFile.read_apr(apr);
6868

6969
ParticleData<uint16_t>parts;
70-
aprFile.read_particles(apr,"particles",parts);
70+
aprFile.read_particles(apr,parts);
7171

7272
aprFile.close();
7373

examples/Example_compute_gradient.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,8 @@ int main(int argc, char **argv) {
4848
APRFile aprFile;
4949
aprFile.open(file_name,"READ");
5050
aprFile.read_apr(apr);
51-
aprFile.read_particles(apr,"particles",parts);
51+
52+
aprFile.read_particles(apr,parts);
5253

5354
timer.stop_timer();
5455

examples/Example_denoise.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
//
2+
// Created by bevan on 29/11/2020.
3+
//
4+
5+
6+
#include "Example_denoise.hpp"
7+
8+
int main(int argc, char **argv) {
9+
10+
// INPUT PARSING
11+
12+
cmdLineOptionsDenoise options = read_command_line_options(argc, argv);
13+
14+
// Seperated for testing.
15+
denoise_example(options);
16+
17+
}
18+

examples/Example_denoise.hpp

Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
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

Comments
 (0)