Skip to content

Commit

Permalink
Add laplacian_filter and laplacian_edge_detector (#516)
Browse files Browse the repository at this point in the history
  • Loading branch information
thecodergus authored Apr 29, 2024
1 parent e0128b6 commit feb8fe7
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/edges.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
//! Functions for detecting edges in images.
use crate::contrast;
use crate::definitions::{HasBlack, HasWhite};
use crate::filter::gaussian_blur_f32;
use crate::filter::{gaussian_blur_f32, laplacian_filter};
use crate::gradients::{horizontal_sobel, vertical_sobel};
use image::{GenericImageView, GrayImage, ImageBuffer, Luma};
use std::f32;
Expand Down Expand Up @@ -156,6 +157,31 @@ fn hysteresis(
out
}

/// Detects edges in a grayscale image using a Laplacian edge detector with Gaussian smoothing and Otsu thresholding.
///
/// # Arguments
///
/// * `image` - The grayscale image to be processed.
///
/// # Return value
///
/// A binary grayscale image where pixels belonging to edges are white and pixels not belonging to edges are black.
///
/// # Details
///
/// The Laplacian edge detector is applied to the image smoothed with a Gaussian filter with standard deviation `sigma`. The threshold for binarization is calculated using Otsu's method, which maximizes the variance between pixel intensity classes.
pub fn laplacian_edge_detector(image: &GrayImage) -> ImageBuffer<Luma<u8>, Vec<u8>> {
let signma = 1.4;

let blurred_img = gaussian_blur_f32(image, signma);

let laplacian_img = laplacian_filter(&blurred_img);

let otsu_threshold = contrast::otsu_level(&laplacian_img);

contrast::threshold(&laplacian_img, otsu_threshold)
}

#[cfg(test)]
mod tests {
use super::canny;
Expand Down
6 changes: 6 additions & 0 deletions src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -552,6 +552,12 @@ where
}
}

/// Apply a Laplacian filter to an image.
#[must_use = "the function does not modify the original image"]
pub fn laplacian_filter(image: &GrayImage) -> ImageBuffer<Luma<u8>, Vec<u8>> {
filter3x3(image, &[1, 1, 1, 1, -8, 1, 1, 1, 1])
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down

0 comments on commit feb8fe7

Please sign in to comment.