SOPT
Sparse OPTimisation
Classes | Functions
sopt::utilities Namespace Reference

Classes

struct  bad_lexical_cast
 Exception to be thrown by lexical_cast (below) More...
 

Functions

Image read_tiff (std::string const &name)
 Reads tiff image. More...
 
void write_tiff (Image<> const &image, std::string const &filename)
 Writes a tiff greyscale file. More...
 
template<typename T , typename U >
lexical_cast (const U &in)
 Convert between any types via stringstream. More...
 
template<typename T = std::string>
std::vector< T > split (std::string s, const std::string &sep)
 Split a string on a specified delimiter with optional cast to another type. More...
 
template<typename T >
std::vector< float > imageToFloat (sopt::Vector< T > const &image)
 
template<typename T >
sopt::Vector< T > floatToImage (std::vector< float > const &float_image)
 
template<typename T >
Vector< T > & get_wavelet_basis_coefficients (Vector< T > &coeffs, const t_uint basis_index, const t_uint size)
 return wavelet basis coefficients from a dictionary More...
 
template<typename T >
Vector< T > & get_wavelet_levels_1d (Vector< T > &coeffs, const t_uint level, const t_uint size)
 return wavelet basis coefficients for a given level and below (1d case) More...
 
template<typename T >
Vector< T > & get_wavelet_levels (Vector< T > &coeffs, const t_uint level, const t_uint rows, const t_uint cols)
 return wavelet basis coefficients for a given level and below (2d case) More...
 
template<typename T >
Vector< T > & get_wavelet_low_high_pass (Vector< T > &coeffs, const t_uint level, const t_uint rows, const t_uint cols)
 return wavelet basis coefficients low pass (rows) and high pass (cols) for a given level More...
 
template<typename T >
Vector< T > & get_wavelet_high_high_pass (Vector< T > &coeffs, const t_uint level, const t_uint rows, const t_uint cols)
 return wavelet basis coefficients high pass (rows) and high pass (cols) for a given level More...
 
template<typename T >
Vector< T > & get_wavelet_high_low_pass (Vector< T > &coeffs, const t_uint level, const t_uint rows, const t_uint cols)
 return wavelet basis coefficients high pass (rows) and high pass (cols) for a given level More...
 
template<typename T >
Vector< T > & get_wavelet_low_low_pass (Vector< T > &coeffs, const t_uint level, const t_uint rows, const t_uint cols)
 return wavelet basis coefficients high pass (rows) and high pass (cols) for a given level More...
 
template<typename T >
Vector< T > & get_wavelet_high_pass_1d (Vector< T > &coeffs, const t_uint level, const t_uint size)
 return 1d high pass filter for a given level of a wavelet More...
 

Function Documentation

◆ floatToImage()

template<typename T >
sopt::Vector<T> sopt::utilities::floatToImage ( std::vector< float > const &  float_image)

Definition at line 83 of file utilities.h.

83  {
84  sopt::Vector<T> image(float_image.size());
85  for (int i = 0; i < float_image.size(); i++) {
86  if constexpr(std::is_same<T, t_complex>::value)
87  {
88  image[i] = t_complex(float_image[i], 0);
89  }
90  else
91  {
92  image[i] = static_cast<T>(float_image[i]);
93  }
94  }
95  return image;
96 }
Eigen::Matrix< T, Eigen::Dynamic, 1 > Vector
A vector of a given type.
Definition: types.h:24
std::complex< t_real > t_complex
Root of the type hierarchy for (real) complex numbers.
Definition: types.h:19

◆ get_wavelet_basis_coefficients()

template<typename T >
Vector< T > & sopt::utilities::get_wavelet_basis_coefficients ( Vector< T > &  coeffs,
const t_uint  basis_index,
const t_uint  size 
)

return wavelet basis coefficients from a dictionary

Definition at line 105 of file wavelets.h.

106  {
107  assert(coeffs.size() > basis_index * size);
108  return coeffs.segment(basis_index * size, size);
109 }

◆ get_wavelet_high_high_pass()

template<typename T >
Vector< T > & sopt::utilities::get_wavelet_high_high_pass ( Vector< T > &  coeffs,
const t_uint  level,
const t_uint  rows,
const t_uint  cols 
)

return wavelet basis coefficients high pass (rows) and high pass (cols) for a given level

Definition at line 139 of file wavelets.h.

140  {
141  auto const Nx = rows >> level; // bitshift to divide by 2^level
142  auto const Ny = cols >> level;
143  const Matrix<T> signal =
144  Matrix<T>::Map(get_wavelet_levels(coeffs, level, rows, cols).data(), Nx, Ny);
145  return Vector<T>::Map(signal.bottomRightCorner(signal.rows() / 2, signal.cols() / 2).data(),
146  signal.size() / 4);
147 }
t_uint rows
t_uint cols
Vector< T > & get_wavelet_levels(Vector< T > &coeffs, const t_uint level, const t_uint rows, const t_uint cols)
return wavelet basis coefficients for a given level and below (2d case)
Definition: wavelets.h:121
sopt::Vector< Scalar > Vector
Definition: inpainting.cc:28
sopt::Matrix< Scalar > Matrix
Definition: inpainting.cc:29

References cols, get_wavelet_levels(), and rows.

◆ get_wavelet_high_low_pass()

template<typename T >
Vector< T > & sopt::utilities::get_wavelet_high_low_pass ( Vector< T > &  coeffs,
const t_uint  level,
const t_uint  rows,
const t_uint  cols 
)

return wavelet basis coefficients high pass (rows) and high pass (cols) for a given level

Definition at line 149 of file wavelets.h.

150  {
151  auto const Nx = rows >> level; // bitshift to divide by 2^level
152  auto const Ny = cols >> level;
153  const Matrix<T> signal =
154  Matrix<T>::Map(get_wavelet_levels(coeffs, level, rows, cols).data(), Nx, Ny);
155  return Vector<T>::Map(signal.bottomLeftCorner(signal.rows() / 2, signal.cols() / 2).data(),
156  signal.size() / 4);
157 }

References cols, get_wavelet_levels(), and rows.

◆ get_wavelet_high_pass_1d()

template<typename T >
Vector< T > & sopt::utilities::get_wavelet_high_pass_1d ( Vector< T > &  coeffs,
const t_uint  level,
const t_uint  size 
)

return 1d high pass filter for a given level of a wavelet

Definition at line 116 of file wavelets.h.

116  {
117  auto const N = static_cast<t_uint>(coeffs.size()) >> level; // bitshift to divide by 2^level
118  return get_wavelet_levels(coeffs, level, size).tail(N / 2);
119 }
constexpr auto N
Definition: wavelets.cc:57
size_t t_uint
Root of the type hierarchy for unsigned integers.
Definition: types.h:15

References get_wavelet_levels(), and N.

◆ get_wavelet_levels()

template<typename T >
Vector< T > & sopt::utilities::get_wavelet_levels ( Vector< T > &  coeffs,
const t_uint  level,
const t_uint  rows,
const t_uint  cols 
)

return wavelet basis coefficients for a given level and below (2d case)

Definition at line 121 of file wavelets.h.

122  {
123  const Matrix<T> signal = Matrix<T>::Map(coeffs.data(), rows, cols);
124  auto const Nx = static_cast<t_uint>(signal.rows()) >> level; // bitshift to divide by 2^level
125  auto const Ny = static_cast<t_uint>(signal.cols()) >> level;
126  return Vector<T>::Map(signal.topLeftCorner(Nx, Ny).data(), Nx * Ny);
127 }

References cols, and rows.

Referenced by get_wavelet_high_high_pass(), get_wavelet_high_low_pass(), get_wavelet_high_pass_1d(), get_wavelet_low_high_pass(), and get_wavelet_low_low_pass().

◆ get_wavelet_levels_1d()

template<typename T >
Vector< T > & sopt::utilities::get_wavelet_levels_1d ( Vector< T > &  coeffs,
const t_uint  level,
const t_uint  size 
)

return wavelet basis coefficients for a given level and below (1d case)

Definition at line 111 of file wavelets.h.

111  {
112  auto const N = static_cast<t_uint>(coeffs.size()) >> level; // bitshift to divide by 2^level
113  return coeffs.head(N);
114 }

References N.

◆ get_wavelet_low_high_pass()

template<typename T >
Vector< T > & sopt::utilities::get_wavelet_low_high_pass ( Vector< T > &  coeffs,
const t_uint  level,
const t_uint  rows,
const t_uint  cols 
)

return wavelet basis coefficients low pass (rows) and high pass (cols) for a given level

Definition at line 129 of file wavelets.h.

130  {
131  auto const Nx = rows >> level; // bitshift to divide by 2^level
132  auto const Ny = cols >> level;
133  const Matrix<T> signal =
134  Matrix<T>::Map(get_wavelet_levels(coeffs, level, rows, cols).data(), Nx, Ny);
135  return Vector<T>::Map(signal.topRightCorner(signal.rows() / 2, signal.cols() / 2).data(),
136  signal.size() / 4);
137 }

References cols, get_wavelet_levels(), and rows.

◆ get_wavelet_low_low_pass()

template<typename T >
Vector< T > & sopt::utilities::get_wavelet_low_low_pass ( Vector< T > &  coeffs,
const t_uint  level,
const t_uint  rows,
const t_uint  cols 
)

return wavelet basis coefficients high pass (rows) and high pass (cols) for a given level

Definition at line 159 of file wavelets.h.

160  {
161  auto const Nx = rows >> level; // bitshift to divide by 2^level
162  auto const Ny = cols >> level;
163  const Matrix<T> signal =
164  Matrix<T>::Map(get_wavelet_levels(coeffs, level, rows, cols).data(), Nx, Ny);
165  return Vector<T>::Map(signal.topLeftCorner(signal.rows() / 2, signal.cols() / 2).data(),
166  signal.size() / 4);
167 }

References cols, get_wavelet_levels(), and rows.

◆ imageToFloat()

template<typename T >
std::vector<float> sopt::utilities::imageToFloat ( sopt::Vector< T > const &  image)

Definition at line 67 of file utilities.h.

67  {
68  std::vector<float> float_image(image.size());
69  for (int i = 0; i < image.size(); i++) {
70  if constexpr(std::is_same<T, t_complex>::value)
71  {
72  float_image[i] = image[i].real();
73  }
74  else
75  {
76  float_image[i] = static_cast<float>(image[i]);
77  }
78  }
79  return float_image;
80 }

Referenced by sopt::ONNXDifferentiableFunc< SCALAR >::function(), and sopt::ONNXDifferentiableFunc< SCALAR >::gradient().

◆ lexical_cast()

template<typename T , typename U >
T sopt::utilities::lexical_cast ( const U &  in)

Convert between any types via stringstream.

Definition at line 24 of file utilities.h.

24  {
25  try {
26  std::stringstream ss;
27  ss << in;
28  T out;
29  ss >> out;
30  return out;
31  } catch(const std::exception& e) {
32  throw bad_lexical_cast(e.what());
33  }
34 }

◆ read_tiff()

sopt::Image sopt::utilities::read_tiff ( std::string const &  filename)

Reads tiff image.

Definition at line 38 of file utilities.cc.

38  {
39  SOPT_MEDIUM_LOG("Reading image file {} ", filename);
40  TIFF *tif = TIFFOpen(filename.c_str(), "r");
41  if (tif == nullptr) SOPT_THROW("Could not open file ") << filename;
42 
43  uint32_t width;
44  uint32_t height;
45  uint32_t t;
46 
47  TIFFGetField(tif, TIFFTAG_IMAGEWIDTH, &width);
48  TIFFGetField(tif, TIFFTAG_IMAGELENGTH, &height);
49  TIFFGetField(tif, TIFFTAG_PHOTOMETRIC, &t);
50  SOPT_LOW_LOG("- image size {}, {} ", width, height);
51  Image<> result = Image<>::Zero(height, width);
52 
53  uint32_t *raster = static_cast<uint32_t *>(_TIFFmalloc(width * height * sizeof(uint32_t)));
54  if (raster == nullptr) SOPT_THROW("Could not allocate memory to read file ") << filename;
55  if (TIFFReadRGBAImage(tif, width, height, raster, 0) == 0)
56  SOPT_THROW("Could not read file ") << filename;
57 
58  uint32_t *pixel = raster;
59  for (uint32_t i(0); i < height; ++i)
60  for (uint32_t j(0); j < width; ++j, ++pixel) result(i, j) = convert_to_greyscale(*pixel);
61 
62  _TIFFfree(raster);
63 
64  TIFFClose(tif);
65  return result;
66 }
#define SOPT_THROW(MSG)
Definition: exception.h:46
#define SOPT_LOW_LOG(...)
Low priority message.
Definition: logging.h:227
#define SOPT_MEDIUM_LOG(...)
Medium priority message.
Definition: logging.h:225
sopt::Image< Scalar > Image
Definition: inpainting.cc:30

References SOPT_LOW_LOG, SOPT_MEDIUM_LOG, and SOPT_THROW.

Referenced by main(), and sopt::tools::read_standard_tiff().

◆ split()

template<typename T = std::string>
std::vector<T> sopt::utilities::split ( std::string  s,
const std::string &  sep 
)

Split a string on a specified delimiter with optional cast to another type.

Definition at line 38 of file utilities.h.

38  {
39  std::vector<T> rtn;
40  while (true) {
41  const size_t delim_pos = s.find(sep);
42  if (delim_pos == std::string::npos) break;
43  const std::string sub = s.substr(0, delim_pos);
44  if (sub.length()) {
45  if constexpr(std::is_same<T, std::string>::value) {
46  rtn.push_back(sub);
47  }
48  else {
49  rtn.push_back(lexical_cast<T>(sub));
50  }
51  }
52  s.replace(0, delim_pos+1, "");
53  }
54  // check for trailing component
55  if (s.length()) {
56  if constexpr(std::is_same<T, std::string>::value) {
57  rtn.push_back(s);
58  }
59  else {
60  rtn.push_back(lexical_cast<T>(s));
61  }
62  }
63  return rtn;
64 }

◆ write_tiff()

void sopt::utilities::write_tiff ( Image<> const &  image,
std::string const &  filename 
)

Writes a tiff greyscale file.

Definition at line 68 of file utilities.cc.

68  {
69  SOPT_MEDIUM_LOG("Writing image file {} ", filename);
70  SOPT_LOW_LOG("- image size {}, {} ", image.rows(), image.cols());
71  TIFF *tif = TIFFOpen(filename.c_str(), "w");
72  if (tif == nullptr) SOPT_THROW("Could not open file ") << filename;
73 
74  uint32_t const width = image.cols();
75  uint32_t const height = image.rows();
76 
77  SOPT_TRACE("Allocating buffer");
78  std::vector<uint32_t> raster(width * height);
79 
80  TIFFSetField(tif, TIFFTAG_IMAGEWIDTH, width);
81  TIFFSetField(tif, TIFFTAG_IMAGELENGTH, height);
82  TIFFSetField(tif, TIFFTAG_SAMPLESPERPIXEL, sizeof(decltype(raster)::value_type));
83  TIFFSetField(tif, TIFFTAG_BITSPERSAMPLE, 8);
84  TIFFSetField(tif, TIFFTAG_ROWSPERSTRIP, height);
85  TIFFSetField(tif, TIFFTAG_PLANARCONFIG, PLANARCONFIG_CONTIG);
86  TIFFSetField(tif, TIFFTAG_COMPRESSION, COMPRESSION_LZW);
87  TIFFSetField(tif, TIFFTAG_PHOTOMETRIC, PHOTOMETRIC_RGB);
88  TIFFSetField(tif, TIFFTAG_ORIENTATION, ORIENTATION_BOTLEFT);
89 
90  SOPT_TRACE("Initializing buffer");
91  auto pixel = raster.begin();
92  for (uint32_t i(0); i < height; ++i)
93  for (uint32_t j(0); j < width; ++j, ++pixel) *pixel = convert_from_greyscale(image(i, j));
94 
95  SOPT_TRACE("Writing strip");
96  TIFFWriteEncodedStrip(tif, 0, raster.data(), width * height * sizeof(decltype(raster)::value_type));
97 
98  TIFFWriteDirectory(tif);
99  SOPT_TRACE("Closing tif");
100  TIFFClose(tif);
101  SOPT_TRACE("Freeing raster");
102 }
#define SOPT_TRACE(...)
Definition: logging.h:220

References SOPT_LOW_LOG, SOPT_MEDIUM_LOG, SOPT_THROW, and SOPT_TRACE.

Referenced by main(), and TEST_CASE().