SOPT
Sparse OPTimisation
Functions
inpainting.cc File Reference
#include <algorithm>
#include <exception>
#include <functional>
#include <iostream>
#include <random>
#include <vector>
#include <ctime>
#include "sopt/imaging_forward_backward.h"
#include "sopt/l1_non_diff_function.h"
#include "sopt/logging.h"
#include "sopt/maths.h"
#include "sopt/relative_variation.h"
#include "sopt/sampling.h"
#include "sopt/types.h"
#include "sopt/utilities.h"
#include "sopt/wavelets.h"
#include "tools_for_tests/directories.h"
#include "tools_for_tests/tiffwrappers.h"
+ Include dependency graph for inpainting.cc:

Go to the source code of this file.

Functions

int main (int argc, char const **argv)
 

Function Documentation

◆ main()

int main ( int  argc,
char const **  argv 
)

Definition at line 24 of file inpainting.cc.

24  {
25  // Some type aliases for simplicity
26  using Scalar = double;
27  // Column vector - linear algebra - A * x is a matrix-vector multiplication
28  // type expected by Forward Backward
30  // Matrix - linear algebra - A * x is a matrix-vector multiplication
31  // type expected by Forward Backward
33  // Image - 2D array - A * x is a coefficient-wise multiplication
34  // Type expected by wavelets and image write/read functions
35  using Image = sopt::Image<Scalar>;
36 
37  std::string const input = argc >= 2 ? argv[1] : "cameraman256";
38  std::string const output = argc == 3 ? argv[2] : "none";
39  if (argc > 3) {
40  std::cout << "Usage:\n"
41  "$ "
42  << argv[0]
43  << " [input [output]]\n\n"
44  "- input: path to the image to clean (or name of standard SOPT image)\n"
45  "- output: filename pattern for output image\n";
46  exit(0);
47  }
48  // Set up random numbers for C and C++
49  auto const seed = std::time(nullptr);
50  std::srand(static_cast<unsigned int>(seed));
51  std::mt19937 mersenne(std::time(nullptr));
52 
53  // Initializes and sets logger (if compiled with logging)
54  // See set_level function for levels.
55  sopt::logging::set_level("debug");
56  SOPT_HIGH_LOG("Read input file {}", input);
57  Image const image = sopt::tools::read_standard_tiff(input);
58  SOPT_HIGH_LOG("Image size: {} x {} = {}", image.cols(), image.rows(), image.size());
59 
60  SOPT_HIGH_LOG("Initializing sensing operator");
61  sopt::t_uint const nmeasure = std::floor(0.5 * image.size());
62  sopt::LinearTransform<Vector> const sampling =
63  sopt::linear_transform<Scalar>(sopt::Sampling(image.size(), nmeasure, mersenne));
64  SOPT_HIGH_LOG("Initializing wavelets");
65  auto const wavelet = sopt::wavelets::factory("DB8", 4);
66 
67  // sopt::wavelets::SARA const wavelet{std::make_tuple("db1", 4u), std::make_tuple("db2", 4u),
68  // std::make_tuple("db3", 4u), std::make_tuple("db4", 4u)};
69 
70  auto const psi = sopt::linear_transform<Scalar>(wavelet, image.rows(), image.cols());
71  SOPT_LOW_LOG("Wavelet coefficients: {}", (psi.adjoint() * image).size());
72 
73  SOPT_HIGH_LOG("Computing Forward Backward parameters");
74  Vector const y0 = sampling * Vector::Map(image.data(), image.size());
75  auto constexpr snr = 30.0;
76  auto const sigma = y0.stableNorm() / std::sqrt(y0.size()) * std::pow(10.0, -(snr / 20.0));
77  auto const epsilon = std::sqrt(nmeasure + 2 * std::sqrt(y0.size())) * sigma;
78 
79  SOPT_HIGH_LOG("Create dirty vector");
80  std::normal_distribution<> gaussian_dist(0, sigma);
81  Vector y(y0.size());
82  for (sopt::t_int i = 0; i < y0.size(); i++) y(i) = y0(i) + gaussian_dist(mersenne);
83  // Write dirty imagte to file
84  if (output != "none") {
85  Vector const dirty = sampling.adjoint() * y;
86  sopt::utilities::write_tiff(Matrix::Map(dirty.data(), image.rows(), image.cols()),
87  "dirty_" + output + ".tiff");
88  }
89 
90  sopt::t_real constexpr regulariser_strength = 18;
91  sopt::t_real const beta = sigma * sigma * 0.5;
92  SOPT_HIGH_LOG("Creating Foward Backward Functor");
94  .itermax(500)
95  .step_size(beta) // stepsize
96  .sigma(sigma) // sigma
97  .regulariser_strength(regulariser_strength) // regularisation paramater
98  .relative_variation(1e-3)
99  .residual_tolerance(0)
100  .tight_frame(true)
101  .Phi(sampling);
102 
103  // Create a shared pointer to an instance of the L1GProximal class
104  // and set its properties
105  auto gp = std::make_shared<sopt::algorithm::L1GProximal<Scalar>>(false);
106  gp->l1_proximal_tolerance(1e-4)
107  .l1_proximal_nu(1)
108  .l1_proximal_itermax(50)
109  .l1_proximal_positivity_constraint(true)
110  .l1_proximal_real_constraint(true)
111  .Psi(psi);
112 
113  // Once the properties are set, inject it into the ImagingForwardBackward object
114  fb.g_function(gp);
115 
116  SOPT_HIGH_LOG("Starting Forward Backward");
117  // Alternatively, forward-backward can be called with a tuple (x, residual) as argument
118  // Here, we default to (y, Φx/ν - y)
119  auto const diagnostic = fb();
120  SOPT_HIGH_LOG("Forward backward returned {}", diagnostic.good);
121 
122  if (output != "none")
123  sopt::utilities::write_tiff(Matrix::Map(diagnostic.x.data(), image.rows(), image.cols()),
124  output + ".tiff");
125  // diagnostic should tell us the function converged
126  // it also contains diagnostic.niters - the number of iterations, and cg_diagnostic - the
127  // diagnostic from the last call to the conjugate gradient.
128  if (not diagnostic.good) throw std::runtime_error("Did not converge!");
129 
130  SOPT_HIGH_LOG("SOPT-Forward Backward converged in {} iterations", diagnostic.niters);
131 
132  return 0;
133 }
sopt::t_real Scalar
Joins together direct and indirect operators.
LinearTransform< VECTOR > adjoint() const
Indirect transform.
An operator that samples a set of measurements.
Definition: sampling.h:17
t_LinearTransform const & Phi() const
Measurement operator.
std::unique_ptr< std::mt19937_64 > mersenne(new std::mt19937_64(0))
#define SOPT_LOW_LOG(...)
Low priority message.
Definition: logging.h:227
#define SOPT_HIGH_LOG(...)
High priority message.
Definition: logging.h:223
void set_level(const std::string &level)
Method to set the logging level of the default Log object.
Definition: logging.h:154
Image read_standard_tiff(std::string const &name)
Reads tiff image from sopt data directory if it exists.
Definition: tiffwrappers.cc:9
void write_tiff(Image<> const &image, std::string const &filename)
Writes a tiff greyscale file.
Definition: utilities.cc:68
Wavelet factory(const std::string &name, t_uint nlevels)
Creates a wavelet transform object.
Definition: wavelets.cc:8
int t_int
Root of the type hierarchy for signed integers.
Definition: types.h:13
Vector< T > dirty(sopt::LinearTransform< Vector< T >> const &sampling, sopt::Image< T > const &image, RANDOM &mersenne)
Definition: inpainting.h:25
double t_real
Root of the type hierarchy for real numbers.
Definition: types.h:17
size_t t_uint
Root of the type hierarchy for unsigned integers.
Definition: types.h:15
Eigen::Array< T, Eigen::Dynamic, Eigen::Dynamic > Image
A 2-dimensional list of elements of given type.
Definition: types.h:39
real_type< T >::type epsilon(sopt::LinearTransform< Vector< T >> const &sampling, sopt::Image< T > const &image)
Definition: inpainting.h:38
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
A vector of a given type.
Definition: types.h:24
real_type< T >::type sigma(sopt::LinearTransform< Vector< T >> const &sampling, sopt::Image< T > const &image)
Definition: inpainting.h:17
Eigen::Matrix< T, Eigen::Dynamic, Eigen::Dynamic > Matrix
A matrix of a given type.
Definition: types.h:29
sopt::Vector< Scalar > Vector
Definition: inpainting.cc:28
sopt::Matrix< Scalar > Matrix
Definition: inpainting.cc:29
sopt::Image< Scalar > Image
Definition: inpainting.cc:30

References sopt::LinearTransform< VECTOR >::adjoint(), sopt::dirty(), sopt::epsilon(), sopt::wavelets::factory(), mersenne(), sopt::algorithm::ImagingForwardBackward< SCALAR >::Phi(), sopt::tools::read_standard_tiff(), sopt::logging::set_level(), sopt::sigma(), SOPT_HIGH_LOG, SOPT_LOW_LOG, and sopt::utilities::write_tiff().