Added psnr from mse

This commit is contained in:
Thomas Forgione 2018-06-29 14:19:23 +02:00
parent a521e879f8
commit 0d9946dc31
No known key found for this signature in database
GPG Key ID: 203DAEA747F48F41
3 changed files with 17 additions and 2 deletions

1
Cargo.lock generated
View File

@ -182,6 +182,7 @@ name = "psnr"
version = "0.1.0"
dependencies = [
"image 0.17.0 (registry+https://github.com/rust-lang/crates.io-index)",
"lazy_static 0.2.11 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]

View File

@ -5,6 +5,7 @@ authors = ["Thomas Forgione <thomas@tforgione.fr>"]
[dependencies]
image = "*"
lazy_static = "*"
[[bin]]
name = "psnr"

View File

@ -1,7 +1,14 @@
#[macro_use]
extern crate lazy_static;
extern crate image;
use image::{Pixel, GenericImage, ImageResult, Rgba};
lazy_static! {
/// This values is 20 * log10(255)
pub static ref TWENTY_LOG10_MAX: f64 = 20.0 * (std::u8::MAX as f64).log10();
}
/// Compute a MSE between an image and its color.
///
/// Will panic an error if the two images don't have the same size.
@ -58,9 +65,15 @@ pub fn mse<P: Pixel<Subpixel=u8>, T: GenericImage<Pixel=P>>(img1: &T, img2: &T)
mse / (img1.dimensions().0 * img1.dimensions().1 * 3) as f64
}
/// Compute the PSNR from the MSE.
/// Returns 10 * log10(255^2) - 10 * log10(mse)
pub fn psnr_from_mse(mse: f64) -> f64 {
*TWENTY_LOG10_MAX - 10.0 * mse.log10()
}
pub fn psnr<P: Pixel<Subpixel=u8>, T: GenericImage<Pixel=P>>(img1: &T, img2: &T) -> f64 {
let twenty_log10_max: f64 = 20.0 * (std::u8::MAX as f64).log10();
twenty_log10_max - 10.0 * mse(img1, img2).log10()
*TWENTY_LOG10_MAX - 10.0 * mse(img1, img2).log10()
}
pub fn psnr_files(path1: &str, path2: &str) -> ImageResult<f64> {