numerical-collection-cpp 0.10.0
A collection of algorithms in numerical analysis implemented in C++
Loading...
Searching...
No Matches
regularization/image_denoising_tgv2_admm.cpp

Example of image denoising using tgv2_admm class.

/*
* Copyright 2021 MusicScience37 (Kenta Kabashima)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <string>
#include <Eigen/Core>
#include <Eigen/SparseCore>
#include "image_denoising_common.h"
auto main(int argc, char** argv) -> int {
constexpr num_collect::index_type size = rows * cols;
// Perform common initialization for examples.
int sample_image_index = 1;
if (!initialize(argc, argv, sample_image_index)) {
return 1;
}
// Generate the original image.
Eigen::MatrixXd origin;
if (!generate_sample_image(sample_image_index, origin)) {
return 1;
}
// Prepare data with noise.
Eigen::MatrixXd data = origin;
// Reshape the data to a vector for processing.
const Eigen::VectorXd data_vec = data.reshaped<Eigen::ColMajor>();
// Prepare a coefficient matrix from parameters to data.
// For denoising, the coefficient matrix is an identity matrix.
using coeff_type = Eigen::SparseMatrix<double>;
coeff_type coeff;
coeff.resize(size, size);
coeff.setIdentity();
// Prepare a matrix for the 1st order derivative operator.
using derivative_matrix_type = Eigen::SparseMatrix<double>;
const auto first_derivative_matrix =
derivative_matrix_type>(cols, rows);
// Prepare a matrix for the 2nd order derivative operator.
const auto second_derivative_matrix =
derivative_matrix_type>(cols, rows);
// Prepare a solver.
using solver_type = num_collect::regularization::tgv2_admm<coeff_type,
derivative_matrix_type, Eigen::VectorXd>;
solver_type solver;
solver.compute(
coeff, first_derivative_matrix, second_derivative_matrix, data_vec);
// Search for an optimal regularization parameter.
const auto& initial_solution = data_vec;
solver, data_vec, initial_solution};
gcv.search();
// Solve the problem using the optimal parameter.
Eigen::VectorXd solution_vec = initial_solution;
gcv.solve(solution_vec);
// Reshape the solution vector to a matrix for visualization.
// The solution is the denoised image.
const Eigen::MatrixXd solution =
solution_vec.reshaped<Eigen::ColMajor>(rows, cols);
// Visualize the result.
visualize_result(
origin, data, solution, "TGV2 Regularization", "tgv2_admm");
return 0;
}
Definition of add_noise function.
Class to search optimal regularization parameter using GCV.
void solve(data_type &solution) const
Solver with the optimal regularization parameter.
void search()
Search the optimal regularization parameter.
Class to solve linear equations with 2nd order total generalized variation (TGV) regularization bredi...
Definition tgv2_admm.h:180
void compute(const Coeff &coeff, const DerivativeMatrix &first_derivative_matrix, const DerivativeMatrix &second_derivative_matrix, const Data &data)
Compute internal parameters.
Definition tgv2_admm.h:220
Definition of implicit_gcv class.
Definition of index_type type.
std::ptrdiff_t index_type
Type of indices in this library.
Definition index_type.h:33
auto tgv2_second_derivative_matrix_2d(num_collect::index_type outer_size, num_collect::index_type inner_size) -> Matrix
Create a second derivative matrix for 2nd order TGV regularization in 2D images.
auto sparse_diff_matrix_2d(num_collect::index_type outer_size, num_collect::index_type inner_size) -> Matrix
Create a sparse differential matrix in 2D.
void add_noise(Eigen::MatrixXd &data, double rate)
Add noise to data.
Definition add_noise.h:36
Definition of sparse_diff_matrix_2d function.
Definition of tgv2_admm class.
Definition of the tgv2_second_derivative_matrix_2d function.