1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
//! A bilateral filter
//!
//! A bilateral filter is a non-linear, edge-preserving,
//! and noise-reducing smoothing filter for images.
//!
//! It is a type of non-linear filter that reduces noise while preserving edges.
//! The filter works by averaging the pixels in a neighborhood around a given pixel,
//! but the weights of the pixels are determined not only by their spatial distance from the given pixel,
//! but also by their intensity difference from the given pixel
//!
//! A description can be found [here](https://homepages.inf.ed.ac.uk/rbf/CVonline/LOCAL_COPIES/MANDUCHI1/Bilateral_Filtering.html)
//!
use zune_core::bit_depth::BitType;
use zune_image::channel::Channel;
use zune_image::errors::ImageErrors;
use zune_image::image::Image;
use zune_image::traits::OperationsTrait;
use crate::pad::{pad, PadMethod};
use crate::spatial::spatial;
use crate::traits::NumOps;
/// The bilateral filter struct
///
/// # Alpha channel
/// - Alpha channel is ignored
///
/// # Example
///
/// ```
/// use zune_core::colorspace::ColorSpace;
/// use zune_image::errors::ImageErrors;
/// use zune_image::image::Image;
/// use zune_image::traits::OperationsTrait;
/// use zune_imageprocs::bilateral_filter::BilateralFilter;
/// // random values
/// let filter= BilateralFilter::new(10,25.0,25.0);
///
/// let mut image =Image::fill(10_u8,ColorSpace::RGB,10,10);
/// filter.execute(&mut image)?;
/// # Ok::<(),ImageErrors>(())
/// ```
pub struct BilateralFilter {
d: i32,
sigma_color: f32,
sigma_space: f32
}
impl BilateralFilter {
/// Create a new bilateral filter
///
/// # Arguments
/// - `d`:Diameter of each pixel neighborhood that is used during filtering. If it is non-positive, it is computed from sigma_space.
///
/// - `sigma_color`:Filter sigma in the color space.
/// A larger value of the parameter means that farther colors within the pixel neighborhood (see sigmaSpace)
/// will be mixed together, resulting in larger areas of semi-equal color.
///- `sigma_space`: Filter sigma in the coordinate space.
/// A larger value of the parameter means that farther pixels will influence each other as
/// long as their colors are close enough (see sigma_color ).
/// When d>0, it specifies the neighborhood size regardless of sigma_space. Otherwise, d is proportional to sigma_space.
#[must_use]
pub fn new(d: i32, sigma_color: f32, sigma_space: f32) -> BilateralFilter {
BilateralFilter {
d,
sigma_color,
sigma_space
}
}
}
impl OperationsTrait for BilateralFilter {
fn name(&self) -> &'static str {
"Bilateral Filter"
}
fn execute_impl(&self, image: &mut Image) -> Result<(), ImageErrors> {
let depth = image.depth();
let (w, h) = image.dimensions();
if self.d < 1 {
return Ok(());
}
// initialize bilateral coefficients outside of the main loop
let coeffs = init_bilateral(
self.d,
self.sigma_color,
self.sigma_space,
usize::from(depth.max_value()) + 1
);
#[cfg(feature = "threads")]
{
std::thread::scope(|s| {
let mut t_results = vec![];
for channel in image.channels_mut(true) {
let result = s.spawn(|| {
let mut new_channel =
Channel::new_with_bit_type(channel.len(), depth.bit_type());
match depth.bit_type() {
BitType::U8 => bilateral_filter_int::<u8>(
channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?,
w,
h,
&coeffs
),
BitType::U16 => bilateral_filter_int::<u16>(
channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?,
w,
h,
&coeffs
),
d => {
return Err(ImageErrors::ImageOperationNotImplemented(
self.name(),
d
));
}
}
*channel = new_channel;
Ok(())
});
t_results.push(result);
}
t_results
.into_iter()
.map(|x| x.join().unwrap())
.collect::<Result<Vec<()>, ImageErrors>>()
})?;
}
#[cfg(not(feature = "threads"))]
{
for channel in image.channels_mut(true) {
let mut new_channel = Channel::new_with_bit_type(channel.len(), depth.bit_type());
match depth.bit_type() {
BitType::U8 => {
bilateral_filter_int::<u8>(
channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?,
w,
h,
&coeffs
);
}
BitType::U16 => {
bilateral_filter_int::<u16>(
channel.reinterpret_as()?,
new_channel.reinterpret_as_mut()?,
w,
h,
&coeffs
);
}
d => {
return Err(ImageErrors::ImageOperationNotImplemented(self.name(), d));
}
}
// overwrite with the filtered channel
*channel = new_channel;
}
}
Ok(())
}
fn supported_types(&self) -> &'static [BitType] {
&[BitType::U8, BitType::U16]
}
}
struct BilateralCoeffs {
color_weight: Vec<f64>,
space_weight: Vec<f64>,
radius: usize,
makx: usize
}
#[allow(clippy::cast_possible_truncation, clippy::cast_precision_loss)]
fn init_bilateral(
d: i32, sigma_color: f32, mut sigma_space: f32, color_range: usize
) -> BilateralCoeffs {
let gauss_color_coeff = f64::from(-0.5 / (sigma_color * sigma_color));
let gauss_space_coeff = f64::from(-0.5 / (sigma_space * sigma_space));
let cn = 1;
// if sigma_color <= 0.0 {
// sigma_color = 1.0;
// }
if sigma_space <= 0.0 {
sigma_space = 1.0;
}
let radius: i32 = if d <= 0 { (sigma_space * 1.5).round() as _ } else { d / 2 };
let mut color_weight = vec![0.0_f64; cn * color_range];
let mut space_weight = vec![0.0_f64; (d * d).unsigned_abs() as usize];
// initialize color-related bilateral filter coeffs
for (i, item) in color_weight.iter_mut().enumerate().take(color_range) {
let c = i as f64;
*item = (c * c * gauss_color_coeff).exp();
}
let mut makx = 0;
// initialize space-related bilateral coeffs
for i in -radius..=radius {
for j in -radius..=radius {
let r = f64::from((i * i) + (j * j)).sqrt();
if r > f64::from(radius) {
continue;
}
space_weight[makx] = (r * r * gauss_space_coeff).exp();
makx += 1;
}
}
return BilateralCoeffs {
color_weight,
space_weight,
radius: usize::try_from(radius).unwrap_or_default(),
makx
};
}
fn bilateral_filter_int<T>(
src: &[T], dest: &mut [T], width: usize, height: usize, coeffs: &BilateralCoeffs
) where
T: Copy + NumOps<T> + Default,
i32: std::convert::From<T>
{
let radius = coeffs.radius;
//pad here
let padded_input = pad(src, width, height, radius, radius, PadMethod::Replicate);
// use an inner lambda to implement the bilateral loop as it allows us to borrow
// surrounding variables
// Carry out the bilateral filter on a single pixel
// the mid of the area is considered to be the main pixel, the others
// are it's surrounding.
//
// This impl matches opencv bilateral_filter's inner loop, with less pointer chasing as
// the spatial function sends the right thing to us
let bilateral_func = |area: &[T]| -> T {
let mid = (area.len() + 1) / 2;
let mut sum = 0.0;
let mut wsum = 0.0;
let val0 = i32::from(area[mid]);
for (val, space_w) in area
.iter()
.zip(coeffs.space_weight.iter())
.take(coeffs.makx)
{
let val = i32::from(*val);
let abs_val = (val - val0).unsigned_abs() as usize;
let w = space_w * coeffs.color_weight[abs_val];
sum += f64::from(val) * w;
wsum += w;
}
return T::from_f64((sum / wsum).round());
};
spatial(&padded_input, dest, radius, width, height, bilateral_func);
}
/// Tests to see that the filter can run on supported bit depths
#[test]
fn test_bilateral_simple() {
use nanorand::Rng;
use zune_core::colorspace::ColorSpace;
let w = 100;
let h = 100;
let color = ColorSpace::Luma;
// fill with random items
let mut input = vec![0_u8; w * h * color.num_components()];
nanorand::WyRand::new().fill(&mut input);
let pixels = Image::from_u8(&input, w, h, color);
let filter = BilateralFilter::new(20, 75.0, 75.0);
for d in filter.supported_types() {
let mut c = pixels.clone();
c.convert_depth(d.to_depth()).unwrap();
filter.execute(&mut c).unwrap();
}
}