SOPT
Sparse OPTimisation
inpainting.cc
Go to the documentation of this file.
1 #include "sopt/config.h"
2 #include "sopt/types.h"
3 
4 #include <algorithm>
5 #include <exception>
6 #include <functional>
7 #include <iostream>
8 #include <random>
9 #include <vector>
10 #include <ctime>
11 #include <Eigen/Eigenvalues>
12 
14 #include "sopt/logging.h"
15 #include "sopt/maths.h"
16 #include "sopt/power_method.h"
18 #include "sopt/sampling.h"
19 #include "sopt/utilities.h"
20 #include "sopt/wavelets.h"
21 #include "sopt/wavelets/sara.h"
22 
23 // This header is not part of the installed sopt interface
24 // It is only present in tests
25 #include "tools_for_tests/directories.h"
27 
28 // \min_{x} ||\Psi^Tx||_1 \quad \mbox{s.t.} \quad ||y - Ax||_2 < \epsilon and x \geq 0
29 int main(int argc, char const **argv) {
30  // Some type aliases for simplicity
31  using Scalar = double;
32  // Column vector - linear algebra - A * x is a matrix-vector multiplication
33  // type expected by PrimalDual
35 
36  // using Complex = sopt::Vector<sopt::t_complex>;
37  // Matrix - linear algebra - A * x is a matrix-vector multiplication
38  // type expected by PrimalDual
40  // Image - 2D array - A * x is a coefficient-wise multiplication
41  // Type expected by wavelets and image write/read functions
42  using Image = sopt::Image<Scalar>;
43 
44  std::string const input = argc >= 2 ? argv[1] : "cameraman256";
45  std::string const output = argc == 3 ? argv[2] : "none";
46  if (argc > 3) {
47  std::cout << "Usage:\n"
48  "$ "
49  << argv[0]
50  << " [input [output]]\n\n"
51  "- input: path to the image to clean (or name of standard SOPT image)\n"
52  "- output: filename pattern for output image\n";
53  exit(0);
54  }
55  // Set up random numbers for C and C++
56  auto const seed = std::time(nullptr);
57  std::srand(static_cast<unsigned int>(seed));
58  std::mt19937 mersenne(std::time(nullptr));
59 
60  SOPT_HIGH_LOG("Read input file {}", input);
61  Image const image = sopt::tools::read_standard_tiff(input);
62 
63  SOPT_HIGH_LOG("Initializing sensing operator");
64  sopt::t_uint const nmeasure = 0.33 * image.size();
65  auto const sampling =
66  sopt::linear_transform<Scalar>(sopt::Sampling(image.size(), nmeasure, mersenne));
67 
68  SOPT_HIGH_LOG("Initializing wavelets");
69  // Below we define a simple wavelet set for testing
70  auto const wavelet = sopt::wavelets::factory("DB4", 4);
71 
72  // sopt::wavelets::SARA const wavelet{std::make_tuple("db1", 4u), std::make_tuple("db2", 4u),
73  // std::make_tuple("db3", 4u), std::make_tuple("db4", 4u)};
74 
75  auto const psi = sopt::linear_transform<Scalar>(wavelet, image.rows(), image.cols());
76 
77  SOPT_HIGH_LOG("Computing primal-dual parameters");
78  Vector const y0 = sampling * Vector::Map(image.data(), image.size());
79 
80  auto constexpr snr = 30.0;
81  auto const sigma = y0.stableNorm() / std::sqrt(y0.size()) * std::pow(10.0, -(snr / 20.0));
82  auto const epsilon = std::sqrt(nmeasure + 2 * std::sqrt(y0.size())) * sigma;
83 
84  SOPT_HIGH_LOG("Create dirty vector");
85  std::normal_distribution<> gaussian_dist(0, sigma);
86  Vector y(y0.size());
87  for (sopt::t_int i = 0; i < y0.size(); i++) y(i) = y0(i) + gaussian_dist(mersenne);
88  // Write dirty image to file
89  if (output != "none") {
90  Vector const dirty = sampling.adjoint() * y;
91  sopt::utilities::write_tiff(Matrix::Map(dirty.data(), image.rows(), image.cols()),
92  "dirty_" + output + ".tiff");
93  }
94  sopt::t_real const regulariser_strength = (psi.adjoint() * (sampling.adjoint() * y)).real().maxCoeff() * 1e-2;
95 
96  SOPT_HIGH_LOG("Creating primal-dual Functor");
98  .itermax(500)
99  .regulariser_strength(regulariser_strength)
100  .tau(0.5)
101  .l2ball_proximal_epsilon(epsilon)
102  .Psi(psi)
103  .Phi(sampling)
104  .relative_variation(5e-4)
106  .positivity_constraint(true);
107 
108  SOPT_HIGH_LOG("Starting primal dual");
109  // Alternatively, pd can be called with a tuple (x, residual) as argument
110  // Here, we default to (Φ^Ty/ν, ΦΦ^Ty/ν - y)
111  auto const diagnostic = pd();
112  SOPT_HIGH_LOG("primal dual returned {}", diagnostic.good);
113 
114  // diagnostic should tell us the function converged
115  // it also contains diagnostic.niters - the number of iterations, and cg_diagnostic - the
116  // diagnostic from the last call to the conjugate gradient.
117  if (not diagnostic.good) {
118  SOPT_HIGH_LOG("SOPT-primal-dual converged in {} iterations", diagnostic.niters);
119  // throw std::runtime_error("Did not converge!");
120  }
121  if (output != "none")
122  sopt::utilities::write_tiff(Matrix::Map(diagnostic.x.data(), image.rows(), image.cols()),
123  output + ".tiff");
124 
125  return 0;
126 }
sopt::t_real Scalar
An operator that samples a set of measurements.
Definition: sampling.h:17
ImagingPrimalDual &::type Psi(ARGS &&... args)
ImagingPrimalDual< Scalar > & residual_convergence(Real const &tolerance)
Helper function to set-up default residual convergence function.
ImagingPrimalDual &::type Phi(ARGS &&... args)
std::unique_ptr< std::mt19937_64 > mersenne(new std::mt19937_64(0))
int main(int argc, char const **argv)
Definition: inpainting.cc:24
#define SOPT_HIGH_LOG(...)
High priority message.
Definition: logging.h:223
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