#[cfg(feature = "std")]
pub fn gaussian(size: usize, std_dev: f64) -> crate::DynamicMatrix<f64> {
let stride = (size >> 1) as f64;
let exp_coefficient = -0.5 / (std_dev * std_dev);
let coefficient = 1.0 / std_dev;
let allocation = size * size;
let mut data = std::vec![0.0; allocation];
for (i, item) in data.iter_mut().enumerate() {
let r = (i / size) as f64 - stride;
let c = (i % size) as f64 - stride;
let x_sq = r * r + c * c;
*item = coefficient * f64::exp(x_sq * exp_coefficient);
}
let sum = data.iter().sum::<f64>();
if sum > 0.0 {
data.iter_mut().for_each(|x| *x /= sum);
}
crate::DynamicMatrix::new(size, size, data).unwrap()
}
#[cfg(feature = "std")]
pub fn box_blur(size: usize) -> crate::DynamicMatrix<f64> {
let value = 1.0 / (size * size) as f64;
crate::DynamicMatrix::new(size, size, std::vec![value; size * size]).unwrap()
}
pub mod sobel {
use crate::StaticMatrix;
#[rustfmt::skip]
pub fn x<T: From<i8>>() -> StaticMatrix<T, 9> {
StaticMatrix::new(
3,
3,
[
T::from(-1), T::from(0), T::from(1),
T::from(-2), T::from(0), T::from(2),
T::from(-1), T::from(0), T::from(1),
],
)
.unwrap()
}
#[rustfmt::skip]
pub fn y<T: From<i8>>() -> StaticMatrix<T, 9> {
StaticMatrix::new(
3,
3,
[
T::from(1), T::from(2), T::from(1),
T::from(0), T::from(0), T::from(0),
T::from(-1), T::from(-2), T::from(-1),
],
)
.unwrap()
}
}
pub mod laplacian {
use crate::StaticMatrix;
#[rustfmt::skip]
pub fn cross<T: From<i8>>() -> StaticMatrix<T, 9> {
StaticMatrix::new(
3,
3,
[
T::from(0), T::from(-1), T::from(0),
T::from(-1), T::from(4), T::from(-1),
T::from(0), T::from(-1), T::from(0),
],
)
.unwrap()
}
#[rustfmt::skip]
pub fn full<T: From<i8>>() -> StaticMatrix<T, 9> {
StaticMatrix::new(
3,
3,
[
T::from(-1), T::from(-1), T::from(-1),
T::from(-1), T::from(8), T::from(-1),
T::from(-1), T::from(-1), T::from(-1),
],
)
.unwrap()
}
}