Skip to main content

qualia_vision/biosense/face/
face_roi_center.rs

1//! Face ROI proxy: bright/skin-like central region (no mesh weights).
2
3use crate::cv::buffer::RgbView;
4
5#[derive(Debug, Clone, Copy, Default)]
6pub struct FaceRoi {
7    pub x: u32,
8    pub y: u32,
9    pub w: u32,
10    pub h: u32,
11}
12
13/// Heuristic face box: largest connected-ish high-R region in upper half (excellence: replace with mesh).
14pub fn face_roi_center(src: RgbView<'_>) -> FaceRoi {
15    let w = src.width;
16    let h = src.height;
17    // Default central upper ROI for rPPG when detector absent
18    let rw = (w / 3).max(8);
19    let rh = (h / 4).max(8);
20    FaceRoi {
21        x: (w.saturating_sub(rw)) / 2,
22        y: h / 6,
23        w: rw,
24        h: rh,
25    }
26}
27
28/// Mean RGB in ROI into `out` \[r,g,b\].
29pub fn roi_mean_rgb(src: RgbView<'_>, roi: FaceRoi, out: &mut [f32; 3]) {
30    if src.stride == src.width * 3 && roi.w > 0 && roi.h > 0 {
31        *out = etalon_signal::vision::roi_mean_rgb_u8(
32            src.bytes,
33            src.width as usize,
34            src.height as usize,
35            roi.x as usize,
36            roi.y as usize,
37            roi.w as usize,
38            roi.h as usize,
39        );
40        return;
41    }
42    let mut s = [0.0f32; 3];
43    let mut n = 0.0f32;
44    let x1 = roi.x.min(src.width.saturating_sub(1));
45    let y1 = roi.y.min(src.height.saturating_sub(1));
46    let x2 = (roi.x + roi.w).min(src.width);
47    let y2 = (roi.y + roi.h).min(src.height);
48    for y in y1..y2 {
49        for x in x1..x2 {
50            let (r, g, b) = src.pixel(x, y);
51            s[0] += r as f32;
52            s[1] += g as f32;
53            s[2] += b as f32;
54            n += 1.0;
55        }
56    }
57    if n > 0.0 {
58        out[0] = s[0] / n;
59        out[1] = s[1] / n;
60        out[2] = s[2] / n;
61    }
62}