use super::ScalarImage;
use crate::image::{GenericImageView, GrayImage, Luma, SubImage};
use imageproc::distance_transform::Norm::{L1, LInf};
use imageproc::morphology::dilate_mut;
pub struct AreaAverage<'a> {
pub depth: SubImage<&'a ScalarImage<u16>>,
pub mask: SubImage<&'a GrayImage>,
pub x0: u32,
pub y0: u32,
pub width: u32,
pub height: u32,
pub radius_px: u32,
}
impl<'a> AreaAverage<'a> {
pub fn from(
depth: &'a ScalarImage<u16>,
mask: &'a GrayImage,
x: u32,
y: u32,
radius_px: u32,
) -> Self {
let half_size = radius_px as i32 + 2;
let x_min = (x as i32 - half_size).max(0) as u32;
let x_max = (x as i32 + half_size).min(depth.width() as i32 - 1) as u32;
let y_min = (y as i32 - half_size).max(0) as u32;
let y_max = (y as i32 + half_size).min(depth.height() as i32 - 1) as u32;
let x0 = (x as i32 - x_min as i32) as u32;
let y0 = (y as i32 - y_min as i32) as u32;
let width = x_max - x_min + 1;
let height = y_max - y_min + 1;
let depth_view = depth.view(x_min, y_min, width, height);
let mask_view = mask.view(x_min, y_min, width, height);
Self {
depth: depth_view,
mask: mask_view,
x0,
y0,
width,
height,
radius_px,
}
}
pub fn get_average(&self) -> u16 {
let area_mask = self.get_area_mask();
let mut count = 0.0;
let mut sum = 0.0;
for (x, y, v) in area_mask.enumerate_pixels() {
if v[0] == 255 {
count += 1.0;
sum += self.depth.get_pixel(x, y)[0] as f32;
}
}
(sum / count) as u16
}
fn get_area_mask(&self) -> GrayImage {
let mut fill = GrayImage::new(self.width, self.height);
for (_, _, v) in fill.enumerate_pixels_mut() {
*v = Luma([0]);
}
fill.put_pixel(self.x0, self.y0, Luma([255]));
for i in 0..self.radius_px {
if i % 2 == 0 {
dilate_mut(&mut fill, LInf, 1);
} else {
dilate_mut(&mut fill, L1, 1);
}
for (x, y, v) in fill.enumerate_pixels_mut() {
if self.mask.get_pixel(x, y)[0] < 255 {
*v = Luma([0]);
}
}
}
fill
}
}