pub struct Filter { /* private fields */ }Expand description
A 2D convolution kernel (filter / mask / window).
§Kernel Layout
The kernel is stored in row-major order as a flat Vec<f32>.
For a 3×3 Sobel-X kernel:
j=0 j=1 j=2 Flat storage:
i=0 [ 1 0 -1 ] index: 0 1 2 3 4 5 6 7 8
i=1 [ 2 0 -2 ] value: 1, 0,-1, 2, 0,-2, 1, 0,-1
i=2 [ 1 0 -1 ]Access formula: kernel[i * width + j]
Implementations§
Source§impl Filter
impl Filter
Sourcepub fn new(width: usize, height: usize) -> Self
pub fn new(width: usize, height: usize) -> Self
Creates a zero-initialised filter of given dimensions.
Filter::new(3, 3) → [0, 0, 0, 0, 0, 0, 0, 0, 0]Sourcepub fn from(kernel_buffer: Vec<f32>, width: usize, height: usize) -> Self
pub fn from(kernel_buffer: Vec<f32>, width: usize, height: usize) -> Self
Creates a filter from a pre-computed flat kernel buffer.
The buffer must have exactly width × height elements, stored
row-major (row 0 first, then row 1, etc.).
§Panics
Exits the process if kernel_buffer.len() != width * height.
Sourcepub fn get_element(&self, x: usize, y: usize) -> Option<f32>
pub fn get_element(&self, x: usize, y: usize) -> Option<f32>
Returns the element at (row, col), or None if out of bounds.
Sourcepub fn set_value_at_pos(&mut self, val: f32, position: (usize, usize))
pub fn set_value_at_pos(&mut self, val: f32, position: (usize, usize))
Sets the element at (row, col) to the given value.
§Panics
Exits the process if the position is out of bounds.
Sourcepub fn try_separable(&self) -> Option<(Vec<f32>, Vec<f32>)>
pub fn try_separable(&self) -> Option<(Vec<f32>, Vec<f32>)>
Tests whether this kernel is separable — i.e. can be expressed as the outer product of a column vector and a row vector.
§The Concept
A 2D kernel K can sometimes be factored: K = col × rowᵀ
[ a ] [ a·c a·d a·e ]
[ b ] × [ c d e ] = [ b·c b·d b·e ]
col 2×1 row 1×3 kernel 2×3When separable, convolution can use two 1D passes instead of one 2D pass: O(fw·fh) → O(fw + fh) per output pixel.
§Decomposition Algorithm
- Find the largest absolute value in the kernel — this is the pivot.
- Extract the pivot’s row as the row vector.
- Extract the pivot’s column, divided by the pivot value, as the column vector.
- Verify that for every element:
col[i] × row[j] ≈ kernel[i][j]. If any element deviates beyond1e-4, the kernel is not separable.
Example: Sobel-X = [1,0,-1; 2,0,-2; 1,0,-1]
Pivot = |2| at (1,0) col = col₀ / 2 = [1/2, 2/2, 1/2]
row = row₁ = [2, 0, -2] row = row₁ = [2, 0, -2]
Verify: [0.5] × [2, 0, -2] = [1,0,-1; 2,0,-2; 1,0,-1] ✓
[1.0]
[0.5]§Returns
Some((col_vec, row_vec))— col hasheightelements, row haswidthelements.None— kernel is not separable (or is all zeros).
Trait Implementations§
Auto Trait Implementations§
impl Freeze for Filter
impl RefUnwindSafe for Filter
impl Send for Filter
impl Sync for Filter
impl Unpin for Filter
impl UnsafeUnpin for Filter
impl UnwindSafe for Filter
Blanket Implementations§
Source§impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
impl<S, D, Swp, Dwp, T> AdaptInto<D, Swp, Dwp, T> for S
Source§fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<Swp, Dwp, T>,
fn adapt_into_using<M>(self, method: M) -> Dwhere
M: TransformMatrix<Swp, Dwp, T>,
Source§fn adapt_into(self) -> D
fn adapt_into(self) -> D
Source§impl<Src, Scheme> ApproxFrom<Src, Scheme> for Srcwhere
Scheme: ApproxScheme,
impl<Src, Scheme> ApproxFrom<Src, Scheme> for Srcwhere
Scheme: ApproxScheme,
Source§fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
fn approx_from(src: Src) -> Result<Src, <Src as ApproxFrom<Src, Scheme>>::Err>
Source§impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Srcwhere
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
impl<Dst, Src, Scheme> ApproxInto<Dst, Scheme> for Srcwhere
Dst: ApproxFrom<Src, Scheme>,
Scheme: ApproxScheme,
Source§type Err = <Dst as ApproxFrom<Src, Scheme>>::Err
type Err = <Dst as ApproxFrom<Src, Scheme>>::Err
Source§fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
fn approx_into(self) -> Result<Dst, <Src as ApproxInto<Dst, Scheme>>::Err>
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T, Dst> ConvAsUtil<Dst> for T
impl<T, Dst> ConvAsUtil<Dst> for T
Source§impl<T> ConvUtil for T
impl<T> ConvUtil for T
Source§fn approx_as<Dst>(self) -> Result<Dst, Self::Err>where
Self: Sized + ApproxInto<Dst>,
fn approx_as<Dst>(self) -> Result<Dst, Self::Err>where
Self: Sized + ApproxInto<Dst>,
Source§fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err>
fn approx_as_by<Dst, Scheme>(self) -> Result<Dst, Self::Err>
Source§impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
impl<T, U> IntoColor<U> for Twhere
U: FromColor<T>,
Source§fn into_color(self) -> U
fn into_color(self) -> U
Source§impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
impl<T, U> IntoColorUnclamped<U> for Twhere
U: FromColorUnclamped<T>,
Source§fn into_color_unclamped(self) -> U
fn into_color_unclamped(self) -> U
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§impl<T> Pointable for T
impl<T> Pointable for T
Source§impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
impl<SS, SP> SupersetOf<SS> for SPwhere
SS: SubsetOf<SP>,
Source§fn to_subset(&self) -> Option<SS>
fn to_subset(&self) -> Option<SS>
self from the equivalent element of its
superset. Read moreSource§fn is_in_subset(&self) -> bool
fn is_in_subset(&self) -> bool
self is actually part of its subset T (and can be converted to it).Source§fn to_subset_unchecked(&self) -> SS
fn to_subset_unchecked(&self) -> SS
self.to_subset but without any property checks. Always succeeds.Source§fn from_subset(element: &SS) -> SP
fn from_subset(element: &SS) -> SP
self to the equivalent element of its superset.Source§impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
impl<T, U> TryIntoColor<U> for Twhere
U: TryFromColor<T>,
Source§fn try_into_color(self) -> Result<U, OutOfBounds<U>>
fn try_into_color(self) -> Result<U, OutOfBounds<U>>
OutOfBounds error is returned which contains
the unclamped color. Read more