Skip to content

Commit 3d49839

Browse files
committed
Attempted to fix conv some more
Simplified trait bounds and made it so kernel and image didn't need to take the same time hoping that made things line up nicer..
1 parent eba3eff commit 3d49839

File tree

4 files changed

+19
-10
lines changed

4 files changed

+19
-10
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
/target
22
**/*.rs.bk
3-
Cargo.lock
3+
Cargo.lock
4+
*.swp

Cargo.toml

+4
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22
name = "ndarray-vision"
33
version = "0.1.0"
44
authors = ["xd009642 <[email protected]>"]
5+
license = "MIT/Apache-2.0"
6+
description = "A computer vision library built on top of ndarray"
7+
keywords = ["image", "vision", "image-processing"]
8+
categories = ["Science", "Multimedia"]
59
edition = "2018"
610

711
[dependencies]

examples/basic.rs

+6-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
use ndarray_vision::core::*;
33

4-
use ndarray::arr1;
4+
use ndarray::{arr1, arr3, Array3};
55

66
fn main() {
77
let format = PixelFormat::RGB;
@@ -15,7 +15,11 @@ fn main() {
1515

1616
// Set top left pixel to RGB 1.0, 0.0, 0.0
1717
image.pixel_mut(0,0).assign(&arr1(&[1.0, 0.0, 0.0]));
18-
18+
19+
let k: Array3::<f64> = arr3(&[[[0.,0.,0.],[4.,4.,4.],[0.,0.,0.]],
20+
[[0.,0.,0.],[10.,10.,10.],[0.,0.,0.]],
21+
[[0.,0.,0.],[4.,4.,4.],[0.,0.,0.]]]);
22+
image.conv(k.view());
1923
// Print pixel and image data to show change
2024
println!("{:?}", image.pixel(0, 0));
2125
println!("{:?}", image.data);

src/core/image.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ pub struct Image<T> {
1414
format: PixelFormat,
1515
}
1616

17-
impl<T> Image<T> where T: One, T: Zero, T: Clone, T: Default {
17+
impl<T> Image<T> where T: One + Zero + Clone + Default {
1818
pub fn new(rows: usize, columns: usize, format: PixelFormat) -> Self {
1919
Image {
2020
data: Array3::<T>::zeros((rows, columns, format.channels())),
@@ -30,30 +30,30 @@ impl<T> Image<T> where T: One, T: Zero, T: Clone, T: Default {
3030
self.data.slice_mut(s![row, col, ..])
3131
}
3232

33-
pub fn conv(&self, kernel: ArrayView3<T>) -> Image<T> {
33+
pub fn conv<U>(&self, kernel: ArrayView3<U>) -> Image<T> {
3434
Image {
3535
data: conv(self.data.view(), kernel),
3636
format: self.format
3737
}
3838
}
3939

40-
pub fn conv_inplace(&mut self, kernel: ArrayView3<T>) {
40+
pub fn conv_inplace<U>(&mut self, kernel: ArrayView3<U>) {
4141
self.data = conv(self.data.view(), kernel);
4242
}
4343
}
4444

4545

46-
pub fn conv<T>(image: ArrayView3<T>, kernel: ArrayView3<T>) -> Array3<T> where T: Default, T: Clone {
47-
let mut result = Array3::<T>::default(image.dim());
46+
pub fn conv<T, U>(image: ArrayView3<T>, kernel: ArrayView3<U>) -> Array3<T> where T: Clone + Zero {
47+
let mut result = Array3::<T>::zeros(image.dim());
4848
let k_s = kernel.shape();
4949
let row_offset = k_s[0]/2;
5050
let col_offset = k_s[1]/2;
5151

5252
Zip::indexed(image.windows(kernel.dim()))
5353
.apply(|(i, j, _), window| {
54-
let mult = window * kernel;
54+
let mult = window * kernel;
5555
let sums = mult.sum_axis(Axis(2));
56-
result.slice_mut(s![i+row_offset, j+col_offset, ..]).assign(sums);
56+
result.slice_mut(s![i+row_offset, j+col_offset, ..]).assign(&sums);
5757
});
5858

5959
result

0 commit comments

Comments
 (0)