Crate image_recovery
source ·Expand description
Image recovery algorithms, implemented in Rust.
The solvers on this library are based on the algorithms presented in Chambolle, A. and Pock, T. (2011), with modifications inspired by Bredies, K. (2014).
Uses the image
crate for loading and saving images, and the ndarray
crate for manipulating matrices.
How to use it:
Declare the dependency in you Cargo.toml
[dependencies]
image-recovery = "0.1"
Examples:
Examples for each solver can be found in the examples
folder, and those can be run with cargo run --example example_name
. However, a quick example usage is shown below:
Image denoising (multichannel)
use image_recovery::{
image, // re-exported `image` crate
ImageArray, // struct for holding images
};
fn main() {
// the `image` crate provides functionality to decode images
let img = image::open("examples/source_images/angry_birb_noisy.png")
.expect("image could not be open")
.into_rgb8(); // the algorithms in this library are implemented for the Luma and Rgb types
// transform the RGB image into a 3D Array
let img_array = ImageArray::from(&img);
// choose inputs for the denoising solver:
// according to Chambolle, A. and Pock, T. (2011),
// tau and lambda should be chosen such that
// `tau * lambda * L2 norm^2 <= 1`
// while `L2 norm^2 <= 8`
// If we choose `tau * lambda * L2 norm^2 == 1`, then:
let tau: f64 = 1.0 / 2_f64.sqrt();
let sigma: f64 = 1_f64 / (8.0 * tau);
// lambda drives the dual objective function
// closer to zero results in a smoother output image
// closer to infinity results in an output closer to the input
let lambda: f64 = 0.0259624705;
// gamma is a variable used to update the internal
// state of the algorithm's variables, providing
// an accelerated method for convergence.
// Chambolle, A. and Pock, T. (2011), choose
// the value to be `0.35 * lambda`
let gamma: f64 = 0.35 * lambda;
// choose bounds for denoising solver
// the algorithm will run for at most `max_iter` iterations
let max_iter: u32 = 100;
// the algorithm will stop running if:
// `convergence_threshold < norm(current - previous) / norm(previous)`
// where `current` is the output candidate for the current iteration,
// and `previous` is the output candidate of the previous iteration.
let convergence_threshold = 10_f64.powi(-10);
// now we can call the denoising solver with the chosen variables
let denoised_array = img_array
.denoise(lambda, tau, sigma, gamma, max_iter, convergence_threshold)
.unwrap(); // will fail if image shape is 1 pixel in either x or y
// we convert the solution into an RGB image format
let denoised_img = denoised_array.into_rgb();
// encode it and save it to a file
denoised_img.save("examples/result_images/angry_birb_denoised.png")
.expect("image could not be saved");
}
This should provide the following result:
Source image: | Output image: |
---|---|
Re-exports
Structs
- An array representing an image, used with the solvers. The From trait is implemented for the types GrayImage and RgbImage in the
image
crate.