1use candle_core::{Result, Tensor};
2
3pub fn pad(image: &Tensor, max_h: usize, max_w: usize) -> Result<Tensor> {
5 let (c, h, w) = image.dims3()?;
6 let new_image = Tensor::zeros((c, max_h, max_w), image.dtype(), image.device())?;
7 new_image.slice_assign(&[&(..c), &(..h), &(..w)], image)
8}
9
10pub fn make_pixel_mask(image: &Tensor, h: usize, w: usize) -> Result<Tensor> {
15 let (_c, max_h, max_w) = image.dims3()?;
16 let mask = Tensor::ones((h, w), image.dtype(), image.device())?;
17 let zeros = Tensor::zeros((max_h, max_w), image.dtype(), image.device())?;
18 zeros.slice_assign(&[&(..h), &(..w)], &mask)
20}
21
22pub fn get_resize_image_size(
25 (h, w): (usize, usize),
26 (min_len, max_len): (usize, usize),
27) -> (usize, usize) {
28 let aspect_ratio = w as f64 / h as f64;
29
30 let (new_h, new_w) = if w >= h && w > max_len {
31 ((max_len as f64 / aspect_ratio) as usize, max_len)
32 } else if h > w && h > max_len {
33 (max_len, (max_len as f64 * aspect_ratio) as usize)
34 } else {
35 (h, w)
36 };
37 (new_h.max(min_len), new_w.max(min_len))
38}