From 6651aeba6fed49f8c4dc56ff5b8239d2aec0afb2 Mon Sep 17 00:00:00 2001 From: Marc Haubenstock Date: Sat, 27 Jul 2019 21:58:47 +0200 Subject: [PATCH] simple convolutoin --- src/linalg/convolution.rs | 32 +++++++++++++++++++++++++++++++- 1 file changed, 31 insertions(+), 1 deletion(-) diff --git a/src/linalg/convolution.rs b/src/linalg/convolution.rs index 528e4488..a943ec21 100644 --- a/src/linalg/convolution.rs +++ b/src/linalg/convolution.rs @@ -163,8 +163,10 @@ impl DMatrix { mat_rows, ker_rows, mat_cols, ker_cols); } + let kernel_size = ker_rows; + let kernel_min = kernel_size/2; let zero = zero::(); - let mut conv = DMatrix::from_diagonal_element(mat_cols, mat_rows, zero); + let mut conv = DMatrix::from_element(mat_cols, mat_rows, zero); // // for i in 0..(vec + ker - 1) { // let u_i = if i > vec { i - ker } else { 0 }; @@ -181,6 +183,34 @@ impl DMatrix { // } // } + for i in 0..mat_rows { + for j in 0..mat_cols { + let val = *self.index((i,j)); + for k_i in 0..kernel_size { + for k_j in 0..kernel_size { + let i_matrix = (i + k_i - kernel_min) as i32; + let j_matrix = (j + k_j - kernel_min) as i32; + + let is_i_in_range = i_matrix >=0 && i_matrix < mat_rows as i32; + let is_j_in_range = j_matrix >=0 && j_matrix < mat_cols as i32; + + let convolved_value = + match is_i_in_range && is_i_in_range { + true => { + let pixel_value = *self.index((i_matrix as usize, j_matrix as usize)); + let kernel_value = *kernel.index((k_i,k_j)); + kernel_value*pixel_value + } + false => zero + }; + + *conv.index_mut((i,j)) = convolved_value; + } + } + + } + } + conv }