Skip to content

Commit

Permalink
Bilateral limitations, dangerous values prevention
Browse files Browse the repository at this point in the history
  • Loading branch information
awxkee committed Sep 27, 2024
1 parent d734dad commit 376156c
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 11 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@ Example comparison time for blurring image 2828x4242 RGBA 8-bit in single-thread
### Fast bilateral blur

This is fast bilateral approximation, note this behaviour significantly differs from OpenCV.
This method has high convergence and will completely blur an image very fast with increasing spatial sigma
This method has high convergence and will completely blur an image very fast with increasing spatial sigma.
By the nature of this filter the more spatial sigma are the faster method is.

```rust
fast_bilateral_filter(
Expand Down
2 changes: 1 addition & 1 deletion src/lib/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "libblur"
version = "0.13.7"
version = "0.13.8"
edition = "2021"
description = "Fast image blurring in pure Rust"
readme = "../../README.md"
Expand Down
17 changes: 15 additions & 2 deletions src/lib/fast_bilateral_filter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,8 +345,18 @@ fn fast_bilateral_filter_impl<
let padding_xy = 2.;
let padding_z = 2.;

let spatial_sigma_scale = 1. / spatial_sigma * (if spatial_sigma > 1.3 { 1.3 } else { 1. });
let range_sigma_scale = 1. / range_sigma;
let spatial_sigma_scale = if spatial_sigma > 1. {
(1. / spatial_sigma * (if spatial_sigma > 1.3 { 1.3 } else { 1. }))
.min(1. / 1.2f32)
.max(1. / 15f32)
} else {
1.
};
let range_sigma_scale = if range_sigma > 1. {
(1. / range_sigma).min(1. / 1.1f32).max(1. / 15f32)
} else {
1.
};

let small_width = (((width - 1) as f32 * spatial_sigma_scale) + 1. + 2. * padding_xy) as usize;
let small_height =
Expand Down Expand Up @@ -822,6 +832,7 @@ fn fast_bilateral_filter_rgba_impl<
///
/// This is fast bilateral approximation, note this behaviour significantly differs from OpenCV.
/// This method has high convergence and will completely blur an image very fast with increasing spatial sigma
/// By the nature of this filter the more spatial sigma are the faster method is.
///
/// # Arguments
///
Expand Down Expand Up @@ -859,6 +870,7 @@ pub fn fast_bilateral_filter(
///
/// This is fast bilateral approximation, note this behaviour significantly differs from OpenCV.
/// This method has high convergence and will completely blur an image very fast with increasing spatial sigma
/// By the nature of this filter the more spatial sigma are the faster method is.
///
/// # Arguments
///
Expand Down Expand Up @@ -896,6 +908,7 @@ pub fn fast_bilateral_filter_u16(
///
/// This is fast bilateral approximation, note this behaviour significantly differs from OpenCV.
/// This method has high convergence and will completely blur an image very fast with increasing spatial sigma
/// By the nature of this filter the more spatial sigma are the faster method is.
///
/// # Arguments
///
Expand Down
3 changes: 2 additions & 1 deletion src/lib/fast_bilateral_image.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ use image::{
/// Performs fast bilateral filter on the image
///
/// This is fast bilateral approximation, note this behaviour significantly differs from OpenCV.
/// This method has high convergence and will completely blur an image very fast with increasing spatial sigma
/// This method has high convergence and will completely blur an image very fast with increasing spatial sigma.
/// By the nature of this filter the more spatial sigma are the faster method is.
///
/// # Arguments
///
Expand Down
12 changes: 6 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ mod split;

use crate::merge::merge_channels_3;
use crate::split::split_channels_3;
use image::{EncodableLayout, GenericImageView, ImageFormat, ImageReader};
use image::{DynamicImage, EncodableLayout, GenericImageView, ImageFormat, ImageReader};
use libblur::{fast_bilateral_filter, fast_bilateral_filter_image, fast_gaussian, fast_gaussian_next, fast_gaussian_next_f32, gaussian_blur_image, stack_blur_image, EdgeMode, FastBlurChannels, GaussianPreciseLevel, ThreadingPolicy};
use std::time::Instant;

Expand Down Expand Up @@ -261,14 +261,11 @@ fn main() {
&mut dst_bytes,
dimensions.0,
dimensions.1,
2.3f32,
1f32,
7f32,
0.5f32,
FastBlurChannels::Channels3,
);

let new_img = fast_bilateral_filter_image(img, 15f32, 1f32).unwrap();
new_img.save_with_format("output.jpg", ImageFormat::Jpeg).unwrap();

// dst_bytes = f16_bytes
// .iter()
// .map(|&x| (x.to_f32() * 255f32) as u8)
Expand All @@ -280,6 +277,9 @@ fn main() {
// Print the elapsed time in milliseconds
println!("Elapsed time: {:.2?}", elapsed_time);

let new_img = fast_bilateral_filter_image(img, 20f32, 1f32).unwrap();
new_img.save_with_format("output.jpg", ImageFormat::Jpeg).unwrap();

// libblur::gaussian_blur(
// &bytes,
// stride as u32,
Expand Down

0 comments on commit 376156c

Please sign in to comment.