wonnx_preprocessing/
image.rs1use std::path::Path;
2
3use image::{imageops::FilterType, ImageBuffer, Pixel, Rgb};
4use ndarray::s;
5
6pub fn load_bw_image(
8 image_path: &Path,
9 width: usize,
10 height: usize,
11) -> ndarray::ArrayBase<ndarray::OwnedRepr<f32>, ndarray::Dim<[usize; 4]>> {
12 let image_buffer: ImageBuffer<Rgb<u8>, Vec<u8>> = image::open(image_path)
13 .unwrap()
14 .resize_exact(width as u32, height as u32, FilterType::Nearest)
15 .to_rgb8();
16
17 ndarray::Array::from_shape_fn((1, 1, width, height), |(_, c, j, i)| {
26 let pixel = image_buffer.get_pixel(i as u32, j as u32);
27 let channels = pixel.channels();
28
29 (channels[c] as f32) / 255.0
31 })
32}
33
34pub fn load_rgb_image(
36 image_path: &Path,
37 width: usize,
38 height: usize,
39) -> ndarray::ArrayBase<ndarray::OwnedRepr<f32>, ndarray::Dim<[usize; 4]>> {
40 log::info!("load_rgb_image {:?} {}x{}", image_path, width, height);
41 let image_buffer: ImageBuffer<Rgb<u8>, Vec<u8>> = image::open(image_path)
42 .unwrap()
43 .resize_to_fill(width as u32, height as u32, FilterType::Nearest)
44 .to_rgb8();
45
46 let mut array = ndarray::Array::from_shape_fn((1, 3, height, width), |(_, c, j, i)| {
55 let pixel = image_buffer.get_pixel(i as u32, j as u32);
56 let channels = pixel.channels();
57
58 (channels[c] as f32) / 255.0
60 });
61
62 let mean = [0.485, 0.456, 0.406];
64 let std = [0.229, 0.224, 0.225];
65 for c in 0..3 {
66 let mut channel_array = array.slice_mut(s![0, c, .., ..]);
67 channel_array -= mean[c];
68 channel_array /= std[c];
69 }
70
71 array
73}