SOPT
Sparse OPTimisation
inpainting.cc
Go to the documentation of this file.
1 #include <algorithm>
2 #include <exception>
3 #include <functional>
4 #include <iostream>
5 #include <random>
6 #include <vector>
7 #include <ctime>
8 #include <catch2/catch_all.hpp>
9 
12 #include "sopt/logging.h"
13 #include "sopt/maths.h"
15 #include "sopt/sampling.h"
16 #include "sopt/types.h"
17 #include "sopt/utilities.h"
18 #include "sopt/wavelets.h"
19 
20 // This header is not part of the installed sopt interface
21 // It is only present in tests
22 #include "tools_for_tests/directories.h"
24 
25 // \min_{x} ||\Psi^Tx||_1 \quad \mbox{s.t.} \quad ||y - Ax||_2 < \epsilon and x \geq 0
26 
27 using Scalar = double;
31 
32 TEST_CASE("Inpainting"){
33  extern std::unique_ptr<std::mt19937_64> mersenne;
34  std::string const input = "cameraman256";
35 
36  Image const image = sopt::tools::read_standard_tiff(input);
37 
38  sopt::t_uint const nmeasure = std::floor(0.5 * image.size());
39  sopt::LinearTransform<Vector> const sampling =
40  sopt::linear_transform<Scalar>(sopt::Sampling(image.size(), nmeasure, *mersenne));
41 
42  auto const wavelet = sopt::wavelets::factory("DB8", 4);
43 
44  auto const psi = sopt::linear_transform<Scalar>(wavelet, image.rows(), image.cols());
45 
46  Vector const y0 = sampling * Vector::Map(image.data(), image.size());
47  auto constexpr snr = 30.0;
48  auto const sigma = y0.stableNorm() / std::sqrt(y0.size()) * std::pow(10.0, -(snr / 20.0));
49  auto const epsilon = std::sqrt(nmeasure + 2 * std::sqrt(y0.size())) * sigma;
50 
51  std::normal_distribution<> gaussian_dist(0, sigma);
52  Vector y(y0.size());
53  for (sopt::t_int i = 0; i < y0.size(); i++) y(i) = y0(i) + gaussian_dist(*mersenne);
54 
55  sopt::t_real constexpr regulariser_strength = 18;
56  sopt::t_real const beta = sigma * sigma * 0.5;
57 
59  fb.itermax(500)
60  .step_size(beta) // stepsize
61  .sigma(sigma) // sigma
62  .regulariser_strength(regulariser_strength) // regularisation paramater
63  .relative_variation(1e-3)
64  .residual_tolerance(0)
65  .tight_frame(true)
66  .Phi(sampling);
67 
68  // Create a shared pointer to an instance of the L1GProximal class
69  // and set its properties
70  auto gp = std::make_shared<sopt::algorithm::L1GProximal<Scalar>>(false);
71  gp->l1_proximal_tolerance(1e-4)
72  .l1_proximal_nu(1)
73  .l1_proximal_itermax(50)
74  .l1_proximal_positivity_constraint(true)
75  .l1_proximal_real_constraint(true)
76  .Psi(psi);
77 
78  // Once the properties are set, inject it into the ImagingForwardBackward object
79  fb.g_function(gp);
80 
81  auto const diagnostic = fb();
82 
83  CHECK(diagnostic.good);
84  CHECK(diagnostic.niters < 500);
85 
86  // compare input image to cleaned output image
87  // calculate mean squared error sum_i ( ( x_true(i) - x_est(i) ) **2 )
88  // check this is less than the number of pixels * 0.01
89 
90  Eigen::Map<const Eigen::VectorXd> flat_image(image.data(), image.size());
91  auto mse = (flat_image - diagnostic.x).array().square().sum() / image.size();
92  CAPTURE(mse);
93  CHECK(mse < 0.01);
94 }
sopt::t_real Scalar
Joins together direct and indirect operators.
An operator that samples a set of measurements.
Definition: sampling.h:17
std::unique_ptr< std::mt19937_64 > mersenne(new std::mt19937_64(0))
Image read_standard_tiff(std::string const &name)
Reads tiff image from sopt data directory if it exists.
Definition: tiffwrappers.cc:9
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
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
TEST_CASE("Inpainting")
Definition: inpainting.cc:32
sopt::Matrix< Scalar > Matrix
Definition: inpainting.cc:29
sopt::Image< Scalar > Image
Definition: inpainting.cc:30