From 44b2c8604a92339afc898134115fb3bd5b6f3b33 Mon Sep 17 00:00:00 2001 From: tiffany1618 Date: Mon, 8 Mar 2021 19:51:22 -0800 Subject: [PATCH] Fixed scale channels --- src/convert.rs | 4 +++- src/filter/edge.rs | 9 +++++---- tests/filter_test.rs | 2 +- 3 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/convert.rs b/src/convert.rs index 7e6a68c..c420ffb 100644 --- a/src/convert.rs +++ b/src/convert.rs @@ -5,7 +5,9 @@ use crate::error::ImgProcResult; /// Scales channels from range 0.0 to `current_max` to range 0.0 to `scaled_max` pub fn scale_channels(input: &Image, current_min: f64, scaled_min: f64, current_max: f64, scaled_max: f64) -> ImgProcResult> { - Ok(input.map_channels(|channel| ((channel + (scaled_min - current_min)) / (current_max - current_min) * (scaled_max - scaled_min)))) + Ok(input.map_channels(|channel| { + (channel - current_min) / (current_max - current_min) * (scaled_max - scaled_min) + scaled_min + })) } /// Converts an `Image` with channels in range 0 to `scale` to an `Image` with channels diff --git a/src/filter/edge.rs b/src/filter/edge.rs index ecdb07a..44050aa 100644 --- a/src/filter/edge.rs +++ b/src/filter/edge.rs @@ -4,7 +4,7 @@ use crate::{filter, error, util, convert}; use crate::image::{Image, BaseImage}; -use crate::error::{ImgProcResult, check_grayscale}; +use crate::error::ImgProcResult; use crate::util::constants::{K_PREWITT_1D_VERT, K_PREWITT_1D_HORZ, K_SOBEL_1D_VERT, K_SOBEL_1D_HORZ, K_LAPLACIAN}; /// Applies a separable derivative mask to a grayscale image @@ -45,8 +45,9 @@ pub fn laplacian(input: &Image) -> ImgProcResult> { Ok(filter::unseparable_filter(input, &K_LAPLACIAN)?) } -/// Applies the Laplacian of Gaussian operator to a grayscale image. Output contains positive -/// and negative values - use [`normalize_laplacian()`](fn.normalize_laplacian.html) for visualization +/// Applies the Laplacian of Gaussian operator using a `size x size` kernel to a grayscale image. +/// Output contains positive and negative values - use +/// [`normalize_laplacian()`](fn.normalize_laplacian.html) for visualization pub fn laplacian_of_gaussian(input: &Image, size: u32, sigma: f64) -> ImgProcResult> { let kernel = util::generate_log_kernel(size, sigma)?; Ok(filter::unseparable_filter(input, &kernel)?) @@ -54,7 +55,7 @@ pub fn laplacian_of_gaussian(input: &Image, size: u32, sigma: f64) -> ImgPr /// Normalizes the result of a Laplacian or Laplacian of Gaussian operator to the range [0, 255] pub fn normalize_laplacian(input: &Image) -> ImgProcResult> { - check_grayscale(input)?; + error::check_grayscale(input)?; let min = *input.data().iter().min_by(|x, y| x.partial_cmp(y).unwrap()).unwrap(); let max = *input.data().iter().max_by(|x, y| x.partial_cmp(y).unwrap()).unwrap(); diff --git a/tests/filter_test.rs b/tests/filter_test.rs index 39b111d..ad3a817 100644 --- a/tests/filter_test.rs +++ b/tests/filter_test.rs @@ -144,7 +144,7 @@ fn laplacian() { write(&filter::normalize_laplacian(&filtered).unwrap(), "images/tests/filter/laplacian.png").unwrap(); } -#[test] +// #[test] fn laplacian_of_gaussian() { let img: Image = colorspace::rgb_to_grayscale(&setup("images/scaled.png").unwrap()).into();