Added psnr from mse
This commit is contained in:
17
src/lib.rs
17
src/lib.rs
@@ -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> {
|
||||
|
||||
Reference in New Issue
Block a user