SOPT
Sparse OPTimisation
Functions
tv_inpainting.cc File Reference
#include "sopt/config.h"
#include "sopt/logging.h"
#include <algorithm>
#include <exception>
#include <functional>
#include <iostream>
#include <random>
#include <vector>
#include <ctime>
#include <Eigen/Eigenvalues>
#include "sopt/types.h"
#include "sopt/gradient_operator.h"
#include "sopt/maths.h"
#include "sopt/power_method.h"
#include "sopt/relative_variation.h"
#include "sopt/sampling.h"
#include "sopt/tv_primal_dual.h"
#include "sopt/utilities.h"
#include "tools_for_tests/directories.h"
#include "tools_for_tests/tiffwrappers.h"
+ Include dependency graph for tv_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 29 of file tv_inpainting.cc.

29  {
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 
70  auto const psi = sopt::gradient_operator::gradient_operator<Scalar>(image.rows(), image.cols());
71 
72  SOPT_HIGH_LOG("Computing primal-dual parameters");
73  Vector const y0 = sampling * Vector::Map(image.data(), image.size());
74 
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 image 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  const Vector grad = psi.adjoint() * (sampling.adjoint() * y);
90  const sopt::t_real regulariser_strength = (grad.segment(0, image.size()).array().square() +
91  grad.segment(image.size(), image.size()).array().square())
92  .sqrt()
93  .real()
94  .maxCoeff() *
95  2e-2;
96 
97  SOPT_HIGH_LOG("Creating primal-dual Functor");
98  auto const pd = sopt::algorithm::TVPrimalDual<Scalar>(y)
99  .itermax(2000)
100  .regulariser_strength(regulariser_strength)
101  .tau(0.5 / (1. + 1.))
102  .l2ball_proximal_epsilon(epsilon)
103  .Psi(psi)
104  .Phi(sampling)
105  .relative_variation(1e-4)
107  .positivity_constraint(true);
108 
109  SOPT_HIGH_LOG("Starting primal dual");
110  // Alternatively, pd can be called with a tuple (x, residual) as argument
111  // Here, we default to (Φ^Ty/ν, ΦΦ^Ty/ν - y)
112  auto const diagnostic = pd();
113  SOPT_HIGH_LOG("primal dual returned {}", diagnostic.good);
114 
115  // diagnostic should tell us the function converged
116  // it also contains diagnostic.niters - the number of iterations, and cg_diagnostic - the
117  // diagnostic from the last call to the conjugate gradient.
118  if (not diagnostic.good) {
119  SOPT_HIGH_LOG("SOPT-primal-dual converged in {} iterations", diagnostic.niters);
120  // throw std::runtime_error("Did not converge!");
121  }
122  if (output != "none")
123  sopt::utilities::write_tiff(Matrix::Map(diagnostic.x.data(), image.rows(), image.cols()),
124  output + ".tiff");
125 
126  return 0;
127 }
sopt::t_real Scalar
An operator that samples a set of measurements.
Definition: sampling.h:17
TVPrimalDual &::type Phi(ARGS &&... args)
TVPrimalDual< Scalar > & residual_convergence(Real const &tolerance)
Helper function to set-up default residual convergence function.
TVPrimalDual &::type Psi(ARGS &&... args)
std::unique_ptr< std::mt19937_64 > mersenne(new std::mt19937_64(0))
#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
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::dirty(), sopt::epsilon(), mersenne(), sopt::algorithm::TVPrimalDual< SCALAR >::Phi(), sopt::algorithm::TVPrimalDual< SCALAR >::Psi(), sopt::tools::read_standard_tiff(), sopt::algorithm::TVPrimalDual< SCALAR >::residual_convergence(), sopt::sigma(), SOPT_HIGH_LOG, and sopt::utilities::write_tiff().