mirror of
https://github.com/storytold/lightcraft.git
synced 2026-12-02 20:19:40 -05:00
Merge pull request #5003 from wanikhawar/claude/issue-4953-6608cd
perf(merge): grid ring search for ANMS point thinning (#4953)
This commit is contained in:
1 file changed
+168
-11
+168
-11
@@ -163,23 +163,25 @@ fn harris(lvl: &Plane) -> Vec<(f32, f32, f32)> {
|
||||
}
|
||||
|
||||
/// Adaptive non-maximal suppression: indices (into `pts`, sorted by decreasing strength) of the `max`
|
||||
/// points with the largest suppression radii.
|
||||
/// points with the largest suppression radii. The nearest stronger point is found by searching
|
||||
/// rings of a uniform grid outward from each point, which gives exactly the brute-force result in
|
||||
/// near-linear time.
|
||||
fn anms(pts: &[Keypoint], max: usize) -> Vec<usize> {
|
||||
if pts.len() <= max {
|
||||
return (0..pts.len()).collect();
|
||||
}
|
||||
let mut pmax = Vec::with_capacity(pts.len());
|
||||
let mut run = f32::NEG_INFINITY;
|
||||
for p in pts {
|
||||
pmax.push(run);
|
||||
run = run.max(p.strength);
|
||||
}
|
||||
let grid = AnmsGrid::new(pts);
|
||||
let radii: Vec<f32> = (0..pts.len())
|
||||
.into_par_iter()
|
||||
.map(|i| {
|
||||
let p = pts[i];
|
||||
let mut best = f32::INFINITY;
|
||||
for q in &pts[..i] {
|
||||
if q.strength * 0.9 > p.strength {
|
||||
let d = (q.x - p.x).powi(2) + (q.y - p.y).powi(2);
|
||||
best = best.min(d);
|
||||
}
|
||||
}
|
||||
best
|
||||
.map(|i| match (pts.get(i), pmax.get(i)) {
|
||||
(Some(p), Some(m)) if m * 0.9 > p.strength => grid.nearest_stronger(pts, i, p),
|
||||
_ => f32::INFINITY,
|
||||
})
|
||||
.collect();
|
||||
let mut idx: Vec<usize> = (0..pts.len()).collect();
|
||||
@@ -188,6 +190,103 @@ fn anms(pts: &[Keypoint], max: usize) -> Vec<usize> {
|
||||
idx
|
||||
}
|
||||
|
||||
/// Uniform grid over the finite points; each cell lists its point indices in ascending order.
|
||||
struct AnmsGrid {
|
||||
minx: f64,
|
||||
miny: f64,
|
||||
cell: f64,
|
||||
gw: usize,
|
||||
gh: usize,
|
||||
cells: Vec<Vec<usize>>,
|
||||
}
|
||||
|
||||
impl AnmsGrid {
|
||||
fn new(pts: &[Keypoint]) -> Self {
|
||||
let finite = || pts.iter().filter(|p| p.x.is_finite() && p.y.is_finite());
|
||||
let (mut minx, mut miny, mut maxx, mut maxy) = (f64::INFINITY, f64::INFINITY, f64::NEG_INFINITY, f64::NEG_INFINITY);
|
||||
for p in finite() {
|
||||
minx = minx.min(p.x as f64);
|
||||
maxx = maxx.max(p.x as f64);
|
||||
miny = miny.min(p.y as f64);
|
||||
maxy = maxy.max(p.y as f64);
|
||||
}
|
||||
if minx > maxx {
|
||||
(minx, miny, maxx, maxy) = (0.0, 0.0, 0.0, 0.0);
|
||||
}
|
||||
let (w, h, n) = (maxx - minx, maxy - miny, pts.len().max(1) as f64);
|
||||
let mut cell = (w * h / n * 2.0).sqrt().max(1.0);
|
||||
while ((w / cell).floor() + 1.0) * ((h / cell).floor() + 1.0) > 4.0 * n {
|
||||
cell *= 1.5;
|
||||
}
|
||||
let (gw, gh) = ((w / cell) as usize + 1, (h / cell) as usize + 1);
|
||||
let mut g = AnmsGrid { minx, miny, cell, gw, gh, cells: vec![Vec::new(); gw * gh] };
|
||||
for (i, p) in pts.iter().enumerate() {
|
||||
if p.x.is_finite() && p.y.is_finite() {
|
||||
let (cx, cy) = g.locate(p);
|
||||
if let Some(c) = g.cells.get_mut(cy * gw + cx) {
|
||||
c.push(i);
|
||||
}
|
||||
}
|
||||
}
|
||||
g
|
||||
}
|
||||
|
||||
fn locate(&self, p: &Keypoint) -> (usize, usize) {
|
||||
let cx = (((p.x as f64 - self.minx) / self.cell) as usize).min(self.gw - 1);
|
||||
let cy = (((p.y as f64 - self.miny) / self.cell) as usize).min(self.gh - 1);
|
||||
(cx, cy)
|
||||
}
|
||||
|
||||
/// Squared distance from `p` (= `pts[i]`) to the nearest earlier point with `strength * 0.9 > p.strength`.
|
||||
fn nearest_stronger(&self, pts: &[Keypoint], i: usize, p: &Keypoint) -> f32 {
|
||||
let mut best = f32::INFINITY;
|
||||
if !(p.x.is_finite() && p.y.is_finite()) {
|
||||
return best;
|
||||
}
|
||||
let (cx, cy) = self.locate(p);
|
||||
let (cx, cy) = (cx as isize, cy as isize);
|
||||
let rmax = cx.max(self.gw as isize - 1 - cx).max(cy).max(self.gh as isize - 1 - cy);
|
||||
let scan = |x: isize, y: isize, best: &mut f32| {
|
||||
if x < 0 || y < 0 || x >= self.gw as isize || y >= self.gh as isize {
|
||||
return;
|
||||
}
|
||||
let Some(list) = self.cells.get(y as usize * self.gw + x as usize) else { return };
|
||||
for &j in list.get(..list.partition_point(|&j| j < i)).unwrap_or(&[]) {
|
||||
let Some(q) = pts.get(j) else { continue };
|
||||
if q.strength * 0.9 > p.strength {
|
||||
let d = (q.x - p.x).powi(2) + (q.y - p.y).powi(2);
|
||||
*best = best.min(d);
|
||||
}
|
||||
}
|
||||
};
|
||||
for r in 0..=rmax {
|
||||
if r == 0 {
|
||||
scan(cx, cy, &mut best);
|
||||
} else {
|
||||
// (only the ring's edges that lie inside the grid: a long thin grid would otherwise
|
||||
// walk the out-of-grid edges cell by cell on every ring)
|
||||
let (xs, ys) = ((cx - r).max(0)..=(cx + r).min(self.gw as isize - 1), (cy - r + 1).max(0)..=(cy + r - 1).min(self.gh as isize - 1));
|
||||
for y in [cy - r, cy + r].into_iter().filter(|y| (0..self.gh as isize).contains(y)) {
|
||||
for x in xs.clone() {
|
||||
scan(x, y, &mut best);
|
||||
}
|
||||
}
|
||||
for x in [cx - r, cx + r].into_iter().filter(|x| (0..self.gw as isize).contains(x)) {
|
||||
for y in ys.clone() {
|
||||
scan(x, y, &mut best);
|
||||
}
|
||||
}
|
||||
}
|
||||
// points in rings beyond `r` are at least `r * cell` away; 1 px absorbs rounding in the cell assignment
|
||||
let m = (r as f64 * self.cell - 1.0).max(0.0);
|
||||
if best as f64 <= m * m {
|
||||
break;
|
||||
}
|
||||
}
|
||||
best
|
||||
}
|
||||
}
|
||||
|
||||
fn descriptor(img: &Plane, x: f32, y: f32, angle: f32) -> Option<[f32; DESC_LEN]> {
|
||||
let (s, c) = angle.sin_cos();
|
||||
let mut d = [0f32; DESC_LEN];
|
||||
@@ -291,6 +390,64 @@ mod tests {
|
||||
})
|
||||
}
|
||||
|
||||
fn anms_reference(pts: &[Keypoint], max: usize) -> Vec<usize> {
|
||||
if pts.len() <= max {
|
||||
return (0..pts.len()).collect();
|
||||
}
|
||||
let radii: Vec<f32> = (0..pts.len())
|
||||
.map(|i| {
|
||||
let p = pts[i];
|
||||
let mut best = f32::INFINITY;
|
||||
for q in &pts[..i] {
|
||||
if q.strength * 0.9 > p.strength {
|
||||
let d = (q.x - p.x).powi(2) + (q.y - p.y).powi(2);
|
||||
best = best.min(d);
|
||||
}
|
||||
}
|
||||
best
|
||||
})
|
||||
.collect();
|
||||
let mut idx: Vec<usize> = (0..pts.len()).collect();
|
||||
idx.sort_by(|&a, &b| radii[b].total_cmp(&radii[a]).then(a.cmp(&b)));
|
||||
idx.truncate(max);
|
||||
idx
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn grid_anms_matches_quadratic_reference() {
|
||||
let mut state = 0x9E37_79B9_7F4A_7C15u64;
|
||||
let mut rnd = move || {
|
||||
state ^= state << 13;
|
||||
state ^= state >> 7;
|
||||
state ^= state << 17;
|
||||
(state >> 40) as f32 / (1u64 << 24) as f32
|
||||
};
|
||||
let kp = |x: f32, y: f32, strength: f32| Keypoint { x, y, level: 0, angle: 0.0, strength };
|
||||
let mut sets: Vec<(Vec<Keypoint>, usize)> = Vec::new();
|
||||
sets.push(((0..3000).map(|_| kp(rnd() * 2000.0, rnd() * 1500.0, rnd())).collect(), 400));
|
||||
let centres: Vec<(f32, f32)> = (0..6).map(|_| (rnd() * 2000.0, rnd() * 1500.0)).collect();
|
||||
sets.push((
|
||||
(0..3000)
|
||||
.map(|k| {
|
||||
let (cx, cy) = centres[k % centres.len()];
|
||||
let g = |r: f32| (r - 0.5) * 60.0;
|
||||
kp(cx + g(rnd() + rnd()), cy + g(rnd() + rnd()), rnd())
|
||||
})
|
||||
.collect(),
|
||||
400,
|
||||
));
|
||||
sets.push(((0..3000).map(|_| kp((rnd() * 40.0).floor(), (rnd() * 30.0).floor(), rnd())).collect(), 400));
|
||||
sets.push(((0..3000).map(|_| kp(rnd() * 2000.0, rnd() * 1500.0, (rnd() * 4.0).floor() + 1.0)).collect(), 400));
|
||||
// one row of points: a grid one cell tall, where the rings' top and bottom edges are outside it
|
||||
sets.push(((0..3000).map(|i| kp(32.0 + 4.0 * i as f32, 32.0, rnd())).collect(), 400));
|
||||
sets.push(((0..50).map(|_| kp(rnd() * 100.0, rnd() * 100.0, rnd())).collect(), 400));
|
||||
sets.push(((0..500).map(|_| kp(rnd() * 2000.0, rnd() * 1500.0, rnd())).collect(), 0));
|
||||
for (mut pts, max) in sets {
|
||||
pts.sort_by(|a, b| b.strength.total_cmp(&a.strength));
|
||||
assert_eq!(anms(&pts, max), anms_reference(&pts, max));
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn shifted_images_match_with_the_shift() {
|
||||
let a = pattern(400, 300, 0.0, 0.0);
|
||||
|
||||
Reference in new issue
Block a user