Skip to main content

qualia_vision/biosense/quality/
motion_energy.rs

1//! Mean absolute frame difference (motion energy).
2
3use crate::cv::buffer::GrayView;
4use crate::cv::error::CvError;
5
6/// Motion energy.
7pub fn motion_energy(prev: GrayView<'_>, next: GrayView<'_>) -> Result<f32, CvError> {
8    if prev.width != next.width || prev.height != next.height {
9        return Err(CvError::DimensionMismatch);
10    }
11    if prev.stride == prev.width && next.stride == next.width {
12        let len = (prev.width as usize).saturating_mul(prev.height as usize);
13        return Ok(etalon_signal::vision::frame_motion_mad_u8(
14            &prev.bytes[..len],
15            &next.bytes[..len],
16        ));
17    }
18    let mut s = 0.0f32;
19    let mut n = 0.0f32;
20    for y in 0..prev.height {
21        for x in 0..prev.width {
22            let d = (prev.pixel(x, y) as i16 - next.pixel(x, y) as i16).unsigned_abs() as f32;
23            s += d;
24            n += 1.0;
25        }
26    }
27    Ok(s / n.max(1.0))
28}