Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implementing Laplacian and LaplacianEdgeDetector Builders for Laplacian Edge Detection #516

Merged
merged 7 commits into from
Apr 29, 2024
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 @@ -157,6 +158,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 @@ -564,6 +564,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
Loading