Skip to content

Commit

Permalink
Fixed scale channels
Browse files Browse the repository at this point in the history
  • Loading branch information
tiffany1618 committed Mar 9, 2021
1 parent 370359f commit 44b2c86
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
4 changes: 3 additions & 1 deletion src/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64>, current_min: f64, scaled_min: f64, current_max: f64, scaled_max: f64) -> ImgProcResult<Image<f64>> {
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<f64>` with channels in range 0 to `scale` to an `Image<u8>` with channels
Expand Down
9 changes: 5 additions & 4 deletions src/filter/edge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -45,16 +45,17 @@ pub fn laplacian(input: &Image<f64>) -> ImgProcResult<Image<f64>> {
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<f64>, size: u32, sigma: f64) -> ImgProcResult<Image<f64>> {
let kernel = util::generate_log_kernel(size, sigma)?;
Ok(filter::unseparable_filter(input, &kernel)?)
}

/// Normalizes the result of a Laplacian or Laplacian of Gaussian operator to the range [0, 255]
pub fn normalize_laplacian(input: &Image<f64>) -> ImgProcResult<Image<u8>> {
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();
Expand Down
2 changes: 1 addition & 1 deletion tests/filter_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<f64> = colorspace::rgb_to_grayscale(&setup("images/scaled.png").unwrap()).into();

Expand Down

0 comments on commit 44b2c86

Please sign in to comment.