Skip to content

Commit 51f8c5a

Browse files
committed
Update ndarray to 0.15
1 parent 0313bb7 commit 51f8c5a

File tree

9 files changed

+45
-43
lines changed

9 files changed

+45
-43
lines changed

Cargo.toml

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,16 @@ processing = []
1919
transform =["ndarray-linalg"]
2020

2121
[dependencies]
22-
ndarray = { version = "0.14", default-features = false }
23-
ndarray-stats = { version = "0.4", default-features = false }
24-
ndarray-linalg = { version = "0.13", default-features = false, optional = true }
25-
noisy_float = { version = "0.1", default-features = false }
22+
ndarray = { version = "0.15", default-features = false }
23+
ndarray-stats = { version = "0.5", default-features = false }
24+
ndarray-linalg = { version = "0.14", default-features = false, optional = true }
25+
noisy_float = { version = "0.2", default-features = false }
2626
num-traits = { version = "0.2", default-features = false }
2727

2828
[dev-dependencies]
29-
ndarray-rand = "0.13.0"
29+
ndarray-rand = "0.14.0"
3030
rand = "0.8"
3131
assert_approx_eq = "1.1.0"
32-
noisy_float = "0.1"
32+
noisy_float = "0.2"
3333
png = "0.16"
34-
ndarray-linalg = { version = "0.13", features = ["intel-mkl"] }
34+
ndarray-linalg = { version = "0.14", features = ["intel-mkl"] }

src/core/colour_models.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -199,7 +199,7 @@ where
199199
let mut res = Array3::<_>::zeros((image.rows(), image.cols(), HSV::channels()));
200200
let window = image.data.windows((1, 1, image.channels()));
201201

202-
Zip::indexed(window).apply(|(i, j, _), pix| {
202+
Zip::indexed(window).for_each(|(i, j, _), pix| {
203203
let red = pix[[0, 0, 0]];
204204
let green = pix[[0, 0, 1]];
205205
let blue = pix[[0, 0, 2]];
@@ -228,7 +228,7 @@ where
228228
let mut res = Array3::<T>::zeros((image.rows(), image.cols(), RGB::channels()));
229229
let window = image.data.windows((1, 1, image.channels()));
230230

231-
Zip::indexed(window).apply(|(i, j, _), pix| {
231+
Zip::indexed(window).for_each(|(i, j, _), pix| {
232232
let h = pix[[0, 0, 0]];
233233
let s = pix[[0, 0, 1]];
234234
let v = pix[[0, 0, 2]];
@@ -257,7 +257,7 @@ where
257257
let mut res = Array3::<T>::zeros((image.rows(), image.cols(), Gray::channels()));
258258
let window = image.data.windows((1, 1, image.channels()));
259259

260-
Zip::indexed(window).apply(|(i, j, _), pix| {
260+
Zip::indexed(window).for_each(|(i, j, _), pix| {
261261
let r = normalise_pixel_value(pix[[0, 0, 0]]);
262262
let g = normalise_pixel_value(pix[[0, 0, 1]]);
263263
let b = normalise_pixel_value(pix[[0, 0, 2]]);
@@ -288,7 +288,7 @@ where
288288
let mut res = Array3::<T>::zeros((image.rows(), image.cols(), RGB::channels()));
289289
let window = image.data.windows((1, 1, image.channels()));
290290

291-
Zip::indexed(window).apply(|(i, j, _), pix| {
291+
Zip::indexed(window).for_each(|(i, j, _), pix| {
292292
let gray = pix[[0, 0, 0]];
293293

294294
res.slice_mut(s![i, j, ..])
@@ -321,7 +321,7 @@ where
321321
[0.0139322, 0.0971045, 0.7141733],
322322
]);
323323

324-
Zip::indexed(window).apply(|(i, j, _), pix| {
324+
Zip::indexed(window).for_each(|(i, j, _), pix| {
325325
let pixel = pix
326326
.index_axis(Axis(0), 0)
327327
.index_axis(Axis(0), 0)
@@ -360,7 +360,7 @@ where
360360
[0.0719453, -0.2289914, 1.4052427],
361361
]);
362362

363-
Zip::indexed(window).apply(|(i, j, _), pix| {
363+
Zip::indexed(window).for_each(|(i, j, _), pix| {
364364
let pixel = pix
365365
.index_axis(Axis(0), 0)
366366
.index_axis(Axis(0), 0)

src/enhancement/histogram_equalisation.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use ndarray::{prelude::*, DataMut};
33
use ndarray_stats::{histogram::Grid, HistogramExt};
44
use num_traits::cast::{FromPrimitive, ToPrimitive};
55
use num_traits::{Num, NumAssignOps};
6-
use std::iter::FromIterator;
76

87
/// Extension trait to implement histogram equalisation on other types
98
pub trait HistogramEqExt<A>

src/lib.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,18 @@
2828
/// The core of `ndarray-vision` contains the `Image` type and colour models
2929
pub mod core;
3030
/// Image enhancement intrinsics and algorithms
31-
#[cfg(feature="enhancement")]
31+
#[cfg(feature = "enhancement")]
3232
pub mod enhancement;
3333
/// Image formats - encoding and decoding images from bytes for saving and
3434
/// loading
35-
#[cfg(feature="format")]
35+
#[cfg(feature = "format")]
3636
pub mod format;
3737
/// Operations relating to morphological image processing
38-
#[cfg(feature="morphology")]
38+
#[cfg(feature = "morphology")]
3939
pub mod morphology;
4040
/// Image processing intrinsics and common filters/algorithms.
41-
#[cfg(feature="processing")]
41+
#[cfg(feature = "processing")]
4242
pub mod processing;
4343
/// Image transforms and warping
44-
#[cfg(feature="transform")]
44+
#[cfg(feature = "transform")]
4545
pub mod transform;

src/morphology/mod.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ where
3232
let (ro, co) = kernel_centre(sh[0], sh[1]);
3333
let mut result = Self::Output::from_elem(self.dim(), false);
3434
if self.shape()[0] >= sh[0] && self.shape()[1] >= sh[1] {
35-
Zip::indexed(self.slice(s![.., .., 0]).windows(kernel.dim())).apply(
35+
Zip::indexed(self.slice(s![.., .., 0]).windows(kernel.dim())).for_each(
3636
|(i, j), window| {
3737
result[[i + ro, j + co, 0]] = (&kernel & &window) == kernel;
3838
},
@@ -50,7 +50,7 @@ where
5050
let (ro, co) = kernel_centre(sh[0], sh[1]);
5151
let mut result = Self::Output::from_elem(self.dim(), false);
5252
if self.shape()[0] >= sh[0] && self.shape()[1] >= sh[1] {
53-
Zip::indexed(self.slice(s![.., .., 0]).windows(kernel.dim())).apply(
53+
Zip::indexed(self.slice(s![.., .., 0]).windows(kernel.dim())).for_each(
5454
|(i, j), window| {
5555
result[[i + ro, j + co, 0]] = (&kernel & &window).iter().any(|x| *x);
5656
},

src/processing/conv.rs

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::core::padding::*;
22
use crate::core::{kernel_centre, ColourModel, Image, ImageBase};
33
use crate::processing::Error;
4+
use core::mem::MaybeUninit;
45
use ndarray::prelude::*;
56
use ndarray::{Data, DataMut, Zip};
67
use num_traits::{Num, NumAssignOps};
@@ -118,9 +119,9 @@ where
118119
let shape = (self.shape()[0], self.shape()[1], self.shape()[2]);
119120

120121
if shape.0 > 0 && shape.1 > 0 {
121-
let mut result = unsafe { Self::Output::uninitialized(shape) };
122+
let mut result = Self::Output::uninit(shape);
122123

123-
Zip::indexed(self.windows(kernel.dim())).apply(|(i, j, _), window| {
124+
Zip::indexed(self.windows(kernel.dim())).for_each(|(i, j, _), window| {
124125
let mut temp;
125126
for channel in 0..k_s[2] {
126127
temp = T::zero();
@@ -130,7 +131,8 @@ where
130131
}
131132
}
132133
unsafe {
133-
*result.uget_mut([i + row_offset, j + col_offset, channel]) = temp;
134+
*result.uget_mut([i + row_offset, j + col_offset, channel]) =
135+
MaybeUninit::new(temp);
134136
}
135137
}
136138
});
@@ -140,7 +142,7 @@ where
140142
apply_edge_convolution(self.view(), kernel.view(), (r, c), strategy);
141143
for chan in 0..k_s[2] {
142144
unsafe {
143-
*result.uget_mut([r, c, chan]) = pixel[chan];
145+
*result.uget_mut([r, c, chan]) = MaybeUninit::new(pixel[chan]);
144146
}
145147
}
146148
let bottom = shape.0 - r - 1;
@@ -152,7 +154,7 @@ where
152154
);
153155
for chan in 0..k_s[2] {
154156
unsafe {
155-
*result.uget_mut([bottom, c, chan]) = pixel[chan];
157+
*result.uget_mut([bottom, c, chan]) = MaybeUninit::new(pixel[chan]);
156158
}
157159
}
158160
}
@@ -163,7 +165,7 @@ where
163165
apply_edge_convolution(self.view(), kernel.view(), (r, c), strategy);
164166
for chan in 0..k_s[2] {
165167
unsafe {
166-
*result.uget_mut([r, c, chan]) = pixel[chan];
168+
*result.uget_mut([r, c, chan]) = MaybeUninit::new(pixel[chan]);
167169
}
168170
}
169171
let right = shape.1 - c - 1;
@@ -175,12 +177,12 @@ where
175177
);
176178
for chan in 0..k_s[2] {
177179
unsafe {
178-
*result.uget_mut([r, right, chan]) = pixel[chan];
180+
*result.uget_mut([r, right, chan]) = MaybeUninit::new(pixel[chan]);
179181
}
180182
}
181183
}
182184
}
183-
Ok(result)
185+
Ok(unsafe { result.assume_init() })
184186
} else {
185187
Err(Error::InvalidDimensions)
186188
}

src/processing/filter.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ use ndarray_stats::interpolate::*;
55
use ndarray_stats::Quantile1dExt;
66
use noisy_float::types::n64;
77
use num_traits::{FromPrimitive, Num, ToPrimitive};
8-
use std::iter::FromIterator;
98
use std::marker::PhantomData;
109

1110
/// Median filter, given a region to move over the image, each pixel is given
@@ -35,7 +34,7 @@ where
3534
let c_offset = shape[1] / 2;
3635
let region = (shape[0], shape[1], 1);
3736
let mut result = Array3::<T>::zeros(self.dim());
38-
Zip::indexed(self.windows(region)).apply(|(i, j, k), window| {
37+
Zip::indexed(self.windows(region)).for_each(|(i, j, k), window| {
3938
let mut flat_window = Array::from_iter(window.iter()).mapv(|x| *x);
4039
if let Ok(v) = flat_window.quantile_mut(n64(0.5f64), &Linear {}) {
4140
result

src/processing/sobel.rs

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use core::mem::MaybeUninit;
12
use crate::core::*;
23
use crate::processing::*;
34
use core::ops::Neg;
@@ -28,16 +29,18 @@ where
2829
let v_temp: Array3<T> = SobelFilter::build_with_params(Orientation::Vertical).unwrap();
2930
let h_temp: Array3<T> = SobelFilter::build_with_params(Orientation::Horizontal).unwrap();
3031
let shape = (v_temp.shape()[0], v_temp.shape()[1], mat.shape()[2]);
31-
let mut h_kernel = unsafe { Array3::<T>::uninitialized(shape) };
32-
let mut v_kernel = unsafe { Array3::<T>::uninitialized(shape) };
32+
let mut h_kernel = Array3::<T>::uninit(shape);
33+
let mut v_kernel = Array3::<T>::uninit(shape);
3334
for i in 0..mat.dim().2 {
34-
h_kernel
35-
.slice_mut(s![.., .., i])
36-
.assign(&h_temp.slice(s![.., .., 0]));
37-
v_kernel
38-
.slice_mut(s![.., .., i])
39-
.assign(&v_temp.slice(s![.., .., 0]));
35+
h_temp
36+
.slice(s![.., .., 0])
37+
.assign_to(h_kernel.slice_mut(s![.., .., i]));
38+
v_temp
39+
.slice(s![.., .., 0])
40+
.assign_to(v_kernel.slice_mut(s![.., .., i]));
4041
}
42+
let h_kernel = unsafe { h_kernel.assume_init() };
43+
let v_kernel = unsafe { v_kernel.assume_init() };
4144
let h_deriv = mat.conv2d(h_kernel.view())?;
4245
let v_deriv = mat.conv2d(v_kernel.view())?;
4346

@@ -54,7 +57,7 @@ where
5457
fn apply_sobel(&self) -> Result<Self::Output, Error> {
5558
let (h_deriv, v_deriv) = get_edge_images(self)?;
5659
let res_shape = h_deriv.dim();
57-
let mut result = unsafe { Self::Output::uninitialized(res_shape) };
60+
let mut result = Self::Output::uninit(res_shape);
5861
for r in 0..res_shape.0 {
5962
for c in 0..res_shape.1 {
6063
for channel in 0..res_shape.2 {
@@ -65,12 +68,12 @@ where
6568
temp = T::one();
6669
}
6770
unsafe {
68-
*result.uget_mut([r, c, channel]) = temp;
71+
*result.uget_mut([r, c, channel]) = MaybeUninit::new(temp);
6972
}
7073
}
7174
}
7275
}
73-
Ok(result)
76+
Ok(unsafe { result.assume_init() } )
7477
}
7578

7679
fn full_sobel(&self) -> Result<(Self::Output, Self::Output), Error> {

src/processing/threshold.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ use ndarray_stats::QuantileExt;
88
use num_traits::cast::FromPrimitive;
99
use num_traits::cast::ToPrimitive;
1010
use num_traits::{Num, NumAssignOps};
11-
use std::iter::FromIterator;
1211
use std::marker::PhantomData;
1312

1413
// Development

0 commit comments

Comments
 (0)