use std::path::Path;
use image::{imageops::FilterType, ImageBuffer, Pixel, Rgb};
use ndarray::s;
pub fn load_bw_image(
image_path: &Path,
width: usize,
height: usize,
) -> ndarray::ArrayBase<ndarray::OwnedRepr<f32>, ndarray::Dim<[usize; 4]>> {
let image_buffer: ImageBuffer<Rgb<u8>, Vec<u8>> = image::open(image_path)
.unwrap()
.resize_exact(width as u32, height as u32, FilterType::Nearest)
.to_rgb8();
ndarray::Array::from_shape_fn((1, 1, width, height), |(_, c, j, i)| {
let pixel = image_buffer.get_pixel(i as u32, j as u32);
let channels = pixel.channels();
(channels[c] as f32) / 255.0
})
}
pub fn load_rgb_image(
image_path: &Path,
width: usize,
height: usize,
) -> ndarray::ArrayBase<ndarray::OwnedRepr<f32>, ndarray::Dim<[usize; 4]>> {
log::info!("load_rgb_image {:?} {}x{}", image_path, width, height);
let image_buffer: ImageBuffer<Rgb<u8>, Vec<u8>> = image::open(image_path)
.unwrap()
.resize_to_fill(width as u32, height as u32, FilterType::Nearest)
.to_rgb8();
let mut array = ndarray::Array::from_shape_fn((1, 3, height, width), |(_, c, j, i)| {
let pixel = image_buffer.get_pixel(i as u32, j as u32);
let channels = pixel.channels();
(channels[c] as f32) / 255.0
});
let mean = [0.485, 0.456, 0.406];
let std = [0.229, 0.224, 0.225];
for c in 0..3 {
let mut channel_array = array.slice_mut(s![0, c, .., ..]);
channel_array -= mean[c];
channel_array /= std[c];
}
array
}