oximedia-align 0.1.7

Video alignment and registration tools for multi-camera synchronization in OxiMedia
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
//! Patent-free feature detection and matching.
//!
//! This module provides implementations of feature detection and description
//! algorithms that are free from patent restrictions:
//!
//! - FAST corner detection
//! - BRIEF binary descriptors
//! - ORB (Oriented FAST and Rotated BRIEF)
//! - Brute-force matching with Hamming distance

use crate::{AlignError, AlignResult, Point2D};
use rayon::prelude::*;
use std::f64::consts::PI;

/// A detected keypoint feature
#[derive(Debug, Clone)]
pub struct Keypoint {
    /// Location of the keypoint
    pub point: Point2D,
    /// Scale (size) of the feature
    pub scale: f32,
    /// Orientation in radians
    pub orientation: f32,
    /// Response (strength) of the feature
    pub response: f32,
}

impl Keypoint {
    /// Create a new keypoint
    #[must_use]
    pub fn new(x: f64, y: f64, scale: f32, orientation: f32, response: f32) -> Self {
        Self {
            point: Point2D::new(x, y),
            scale,
            orientation,
            response,
        }
    }
}

/// Binary descriptor (256 bits = 32 bytes)
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BinaryDescriptor {
    /// Binary descriptor data
    pub data: [u8; 32],
}

impl BinaryDescriptor {
    /// Create a new binary descriptor
    #[must_use]
    pub fn new(data: [u8; 32]) -> Self {
        Self { data }
    }

    /// Compute Hamming distance to another descriptor
    #[must_use]
    pub fn hamming_distance(&self, other: &Self) -> u32 {
        self.data
            .iter()
            .zip(&other.data)
            .map(|(a, b)| (a ^ b).count_ones())
            .sum()
    }

    /// Create zero descriptor
    #[must_use]
    pub fn zero() -> Self {
        Self { data: [0; 32] }
    }
}

/// Compute Hamming distance between two byte slices using u64 popcount batching.
///
/// Processes 8 bytes at a time by casting to `u64` and using the hardware
/// `POPCNT` instruction via `u64::count_ones()`. This is significantly faster
/// than byte-by-byte XOR when matching large numbers of BRIEF descriptors.
///
/// The slices must be the same length; any trailing bytes (if length is not a
/// multiple of 8) are handled byte-by-byte to avoid out-of-bounds access.
///
/// # Panics
///
/// Panics if `a.len() != b.len()`.
#[must_use]
pub fn hamming_distance_simd(a: &[u8], b: &[u8]) -> u32 {
    assert_eq!(a.len(), b.len(), "descriptor slices must have equal length");

    let mut total = 0u32;
    let chunks = a.len() / 8;

    // Process 8-byte (u64) chunks — maps to POPCNT on x86-64 / AArch64.
    for i in 0..chunks {
        let offset = i * 8;
        let au = u64::from_le_bytes([
            a[offset],
            a[offset + 1],
            a[offset + 2],
            a[offset + 3],
            a[offset + 4],
            a[offset + 5],
            a[offset + 6],
            a[offset + 7],
        ]);
        let bu = u64::from_le_bytes([
            b[offset],
            b[offset + 1],
            b[offset + 2],
            b[offset + 3],
            b[offset + 4],
            b[offset + 5],
            b[offset + 6],
            b[offset + 7],
        ]);
        total += (au ^ bu).count_ones();
    }

    // Handle the remaining bytes (tail) if length % 8 != 0.
    let tail_start = chunks * 8;
    for (&av, &bv) in a[tail_start..].iter().zip(&b[tail_start..]) {
        total += (av ^ bv).count_ones();
    }

    total
}

/// A pair of matched features
#[derive(Debug, Clone)]
pub struct MatchPair {
    /// Index in first image
    pub idx1: usize,
    /// Index in second image
    pub idx2: usize,
    /// Hamming distance
    pub distance: u32,
    /// Point in first image
    pub point1: Point2D,
    /// Point in second image
    pub point2: Point2D,
}

impl MatchPair {
    /// Create a new match pair
    #[must_use]
    pub fn new(idx1: usize, idx2: usize, distance: u32, point1: Point2D, point2: Point2D) -> Self {
        Self {
            idx1,
            idx2,
            distance,
            point1,
            point2,
        }
    }
}

/// FAST corner detector
pub struct FastDetector {
    /// Threshold for corner detection
    pub threshold: u8,
    /// Non-maximum suppression window size
    pub nms_window: usize,
}

impl Default for FastDetector {
    fn default() -> Self {
        Self {
            threshold: 20,
            nms_window: 3,
        }
    }
}

impl FastDetector {
    /// Create a new FAST detector
    #[must_use]
    pub fn new(threshold: u8) -> Self {
        Self {
            threshold,
            nms_window: 3,
        }
    }

    /// Detect FAST corners in a grayscale image
    ///
    /// # Errors
    /// Returns error if image dimensions are invalid
    pub fn detect(&self, image: &[u8], width: usize, height: usize) -> AlignResult<Vec<Keypoint>> {
        if image.len() != width * height {
            return Err(AlignError::InvalidConfig("Image size mismatch".to_string()));
        }

        let mut corners = Vec::new();
        let radius = 3;

        // FAST-9 circle offsets (16 pixels in a circle of radius 3)
        let circle = self.get_circle_offsets(width);

        // Detect corners (skip border)
        for y in radius..height - radius {
            for x in radius..width - radius {
                let idx = y * width + x;
                let center = image[idx];

                if self.is_corner(image, idx, center, &circle) {
                    let response = self.compute_response(image, idx, center, &circle);
                    corners.push(Keypoint::new(x as f64, y as f64, 1.0, 0.0, response));
                }
            }
        }

        // Apply non-maximum suppression
        let corners = self.non_maximum_suppression(&corners, width, height);

        Ok(corners)
    }

    /// Get FAST-9 circle offsets
    fn get_circle_offsets(&self, width: usize) -> Vec<isize> {
        let w = width as isize;
        vec![
            -w * 3,     // 0: top
            -w * 3 + 1, // 1
            -w * 2 + 2, // 2
            -w + 3,     // 3: right
            3,          // 4
            w + 3,      // 5
            w * 2 + 2,  // 6
            w * 3 + 1,  // 7: bottom
            w * 3,      // 8
            w * 3 - 1,  // 9
            w * 2 - 2,  // 10
            w - 3,      // 11: left
            -3,         // 12
            -w - 3,     // 13
            -w * 2 - 2, // 14
            -w * 3 - 1, // 15
        ]
    }

    /// Check if pixel is a corner
    fn is_corner(&self, image: &[u8], center_idx: usize, center_val: u8, circle: &[isize]) -> bool {
        let threshold = i16::from(self.threshold);
        let center = i16::from(center_val);

        // Count consecutive brighter or darker pixels
        let mut brighter = 0;
        let mut darker = 0;
        let mut max_consecutive_brighter = 0;
        let mut max_consecutive_darker = 0;

        for &offset in circle {
            let idx = (center_idx as isize + offset) as usize;
            let val = i16::from(image[idx]);
            let diff = val - center;

            if diff > threshold {
                brighter += 1;
                darker = 0;
                max_consecutive_brighter = max_consecutive_brighter.max(brighter);
            } else if diff < -threshold {
                darker += 1;
                brighter = 0;
                max_consecutive_darker = max_consecutive_darker.max(darker);
            } else {
                brighter = 0;
                darker = 0;
            }
        }

        // Need at least 9 consecutive pixels (FAST-9)
        max_consecutive_brighter >= 9 || max_consecutive_darker >= 9
    }

    /// Compute corner response (strength)
    fn compute_response(
        &self,
        image: &[u8],
        center_idx: usize,
        center_val: u8,
        circle: &[isize],
    ) -> f32 {
        let center = i16::from(center_val);
        let mut sum_abs_diff = 0i16;

        for &offset in circle {
            let idx = (center_idx as isize + offset) as usize;
            let val = i16::from(image[idx]);
            sum_abs_diff += (val - center).abs();
        }

        f32::from(sum_abs_diff)
    }

    /// Non-maximum suppression
    fn non_maximum_suppression(
        &self,
        corners: &[Keypoint],
        _width: usize,
        _height: usize,
    ) -> Vec<Keypoint> {
        let mut suppressed = vec![false; corners.len()];
        let window = self.nms_window;

        for i in 0..corners.len() {
            if suppressed[i] {
                continue;
            }

            let ki = &corners[i];

            for (j, kj) in corners.iter().enumerate().skip(i + 1) {
                if suppressed[j] {
                    continue;
                }

                let dx = (ki.point.x - kj.point.x).abs();
                let dy = (ki.point.y - kj.point.y).abs();

                if dx < window as f64 && dy < window as f64 {
                    // Keep the one with higher response
                    if ki.response > kj.response {
                        suppressed[j] = true;
                    } else {
                        suppressed[i] = true;
                        break;
                    }
                }
            }
        }

        corners
            .iter()
            .enumerate()
            .filter(|(i, _)| !suppressed[*i])
            .map(|(_, k)| k.clone())
            .collect()
    }
}

/// Sub-pixel corner refinement using parabolic fitting.
///
/// After FAST detects corners at integer-pixel positions, this refiner
/// fits a 2-D parabola to the corner response in a small neighbourhood and
/// finds the sub-pixel maximum.  This improves localisation accuracy from
/// ~0.5 px to ~0.05 px, which is critical for high-quality homography
/// estimation and camera calibration.
pub struct SubPixelRefiner {
    /// Half-size of the refinement window.
    pub half_window: usize,
    /// Maximum number of Newton iterations.
    pub max_iterations: usize,
    /// Convergence threshold (pixels).
    pub epsilon: f64,
}

impl Default for SubPixelRefiner {
    fn default() -> Self {
        Self {
            half_window: 3,
            max_iterations: 10,
            epsilon: 0.01,
        }
    }
}

impl SubPixelRefiner {
    /// Create a new sub-pixel refiner.
    #[must_use]
    pub fn new(half_window: usize, max_iterations: usize, epsilon: f64) -> Self {
        Self {
            half_window,
            max_iterations,
            epsilon,
        }
    }

    /// Refine a set of keypoint positions to sub-pixel accuracy.
    ///
    /// Uses a quadratic surface fit: within the window around each keypoint,
    /// fit `I(x,y) = a*x^2 + b*y^2 + c*x*y + d*x + e*y + f` and find the
    /// extremum at `(-dI/dx, -dI/dy) / Hessian`.  This works for both
    /// corners and peaks/blobs, unlike the structure tensor method which
    /// only converges for true corners.
    ///
    /// Keypoints too close to the image border are left unmodified.
    ///
    /// # Errors
    ///
    /// Returns an error if the image dimensions are invalid.
    pub fn refine(
        &self,
        image: &[u8],
        width: usize,
        height: usize,
        keypoints: &[Keypoint],
    ) -> AlignResult<Vec<Keypoint>> {
        if image.len() != width * height {
            return Err(AlignError::InvalidConfig("Image size mismatch".to_string()));
        }

        let mut refined = Vec::with_capacity(keypoints.len());
        let hw = self.half_window as isize;

        for kp in keypoints {
            let ix = kp.point.x.round() as isize;
            let iy = kp.point.y.round() as isize;

            // Skip if too close to border
            if ix < hw + 1
                || iy < hw + 1
                || ix >= (width as isize - hw - 1)
                || iy >= (height as isize - hw - 1)
            {
                refined.push(kp.clone());
                continue;
            }

            let mut cx = kp.point.x;
            let mut cy = kp.point.y;

            for _iter in 0..self.max_iterations {
                let rx = cx.round() as isize;
                let ry = cy.round() as isize;

                // Fit quadratic surface I = a*dx^2 + b*dy^2 + c*dx*dy + d*dx + e*dy + f
                // using least-squares over the window, where dx = x - rx, dy = y - ry.
                //
                // We only need the gradient (d, e) and Hessian (2a, c; c, 2b) to find
                // the sub-pixel shift: delta = -H^{-1} * grad.
                //
                // Normal equations for [a, b, c, d, e, f]:
                // Use the closed-form expressions from sums of monomials.
                let mut _s_x2_val = 0.0_f64; // sum(dx^2 * I)
                let mut _s_y2_val = 0.0_f64; // sum(dy^2 * I)
                let mut _s_xy_val = 0.0_f64; // sum(dx*dy * I)
                let mut _s_x_val = 0.0_f64; // sum(dx * I)
                let mut _s_y_val = 0.0_f64; // sum(dy * I)

                let mut _s_x2 = 0.0_f64;
                let mut _s_y2 = 0.0_f64;
                let mut _s_x4 = 0.0_f64;
                let mut _s_y4 = 0.0_f64;
                let mut _s_x2y2 = 0.0_f64;

                let mut count = 0.0_f64;

                for dy in -hw..=hw {
                    for dx in -hw..=hw {
                        let px = (rx + dx) as usize;
                        let py = (ry + dy) as usize;
                        let val = f64::from(image[py * width + px]);
                        let dxf = dx as f64;
                        let dyf = dy as f64;

                        _s_x2_val += dxf * dxf * val;
                        _s_y2_val += dyf * dyf * val;
                        _s_xy_val += dxf * dyf * val;
                        _s_x_val += dxf * val;
                        _s_y_val += dyf * val;

                        _s_x2 += dxf * dxf;
                        _s_y2 += dyf * dyf;
                        _s_x4 += dxf * dxf * dxf * dxf;
                        _s_y4 += dyf * dyf * dyf * dyf;
                        _s_x2y2 += dxf * dxf * dyf * dyf;

                        count += 1.0;
                    }
                }

                if count < 6.0 {
                    break;
                }

                // For a symmetric window centred at origin, odd-power sums
                // vanish (sum(x)=0, sum(x^3)=0, sum(x*y^2)=0, etc.).
                // The normal equations decouple:
                //
                // For coefficient d: d = s_x_val / s_x2
                // For coefficient e: e = s_y_val / s_y2
                // For coefficients a, b: solve from s_x4, s_x2y2, s_y4
                //   a*s_x4 + b*s_x2y2 = s_x2_val - f*s_x2
                //   a*s_x2y2 + b*s_y4 = s_y2_val - f*s_y2
                // where f = (sum(I) - a*s_x2 - b*s_y2) / count
                //
                // For our purposes we only need d and e (first-order) and
                // a, b, c (second-order) to compute the Hessian.
                //
                // Simplified: just use the 3-point formula for the Hessian
                // and gradient at the integer location for robustness.

                // Gradient via central differences at (rx, ry)
                let idx_c = ry as usize * width + rx as usize;
                let gx = (f64::from(image[idx_c + 1]) - f64::from(image[idx_c - 1])) / 2.0;
                let gy = (f64::from(image[(ry as usize + 1) * width + rx as usize])
                    - f64::from(image[(ry as usize - 1) * width + rx as usize]))
                    / 2.0;

                // Hessian via central differences
                let hxx = f64::from(image[idx_c + 1]) + f64::from(image[idx_c - 1])
                    - 2.0 * f64::from(image[idx_c]);
                let hyy = f64::from(image[(ry as usize + 1) * width + rx as usize])
                    + f64::from(image[(ry as usize - 1) * width + rx as usize])
                    - 2.0 * f64::from(image[idx_c]);
                let hxy = (f64::from(image[(ry as usize + 1) * width + rx as usize + 1])
                    - f64::from(image[(ry as usize + 1) * width + rx as usize - 1])
                    - f64::from(image[(ry as usize - 1) * width + rx as usize + 1])
                    + f64::from(image[(ry as usize - 1) * width + rx as usize - 1]))
                    / 4.0;

                let det = hxx * hyy - hxy * hxy;
                if det.abs() < 1e-10 {
                    break;
                }

                // Newton step: delta = -H^{-1} * grad
                let shift_x = -(hyy * gx - hxy * gy) / det;
                let shift_y = -(-hxy * gx + hxx * gy) / det;

                // Reject shifts larger than 1 pixel (non-convergent)
                if shift_x.abs() > 1.0 || shift_y.abs() > 1.0 {
                    break;
                }

                cx = rx as f64 + shift_x;
                cy = ry as f64 + shift_y;

                if shift_x * shift_x + shift_y * shift_y < self.epsilon * self.epsilon {
                    break;
                }
            }

            // Clamp to image bounds
            cx = cx.clamp(0.0, (width - 1) as f64);
            cy = cy.clamp(0.0, (height - 1) as f64);

            refined.push(Keypoint::new(cx, cy, kp.scale, kp.orientation, kp.response));
        }

        Ok(refined)
    }
}

/// Compute Sobel gradients (f64 output) for sub-pixel refinement.
#[allow(dead_code)]
fn compute_sobel_gradients(image: &[u8], width: usize, height: usize) -> (Vec<f64>, Vec<f64>) {
    let n = width * height;
    let mut gx = vec![0.0_f64; n];
    let mut gy = vec![0.0_f64; n];

    for y in 1..height.saturating_sub(1) {
        for x in 1..width.saturating_sub(1) {
            let idx = y * width + x;

            let i_tl = f64::from(image[(y - 1) * width + (x - 1)]);
            let i_t = f64::from(image[(y - 1) * width + x]);
            let i_tr = f64::from(image[(y - 1) * width + (x + 1)]);
            let i_l = f64::from(image[y * width + (x - 1)]);
            let i_r = f64::from(image[y * width + (x + 1)]);
            let i_bl = f64::from(image[(y + 1) * width + (x - 1)]);
            let i_b = f64::from(image[(y + 1) * width + x]);
            let i_br = f64::from(image[(y + 1) * width + (x + 1)]);

            gx[idx] = (-i_tl + i_tr - 2.0 * i_l + 2.0 * i_r - i_bl + i_br) / 8.0;
            gy[idx] = (-i_tl - 2.0 * i_t - i_tr + i_bl + 2.0 * i_b + i_br) / 8.0;
        }
    }

    (gx, gy)
}

/// BRIEF descriptor extractor
pub struct BriefDescriptor {
    /// Patch size
    pub patch_size: usize,
    /// Sampling pattern
    pattern: Vec<(isize, isize, isize, isize)>,
}

impl Default for BriefDescriptor {
    fn default() -> Self {
        Self::new(31)
    }
}

impl BriefDescriptor {
    /// Create a new BRIEF descriptor extractor
    #[must_use]
    pub fn new(patch_size: usize) -> Self {
        let pattern = Self::generate_pattern(patch_size);
        Self {
            patch_size,
            pattern,
        }
    }

    /// Generate sampling pattern (deterministic)
    fn generate_pattern(patch_size: usize) -> Vec<(isize, isize, isize, isize)> {
        let mut pattern = Vec::with_capacity(256);
        let half = (patch_size / 2) as isize;

        // Use a deterministic pattern based on a simple PRNG
        let mut seed = 42u32;
        for _ in 0..256 {
            let x1 = (Self::next_random(&mut seed) % patch_size as u32) as isize - half;
            let y1 = (Self::next_random(&mut seed) % patch_size as u32) as isize - half;
            let x2 = (Self::next_random(&mut seed) % patch_size as u32) as isize - half;
            let y2 = (Self::next_random(&mut seed) % patch_size as u32) as isize - half;
            pattern.push((x1, y1, x2, y2));
        }

        pattern
    }

    /// Simple LCG random number generator
    fn next_random(seed: &mut u32) -> u32 {
        *seed = seed.wrapping_mul(1103515245).wrapping_add(12345);
        (*seed / 65536) % 32768
    }

    /// Extract BRIEF descriptor at a keypoint
    ///
    /// # Errors
    /// Returns error if keypoint is too close to image border
    pub fn extract(
        &self,
        image: &[u8],
        width: usize,
        height: usize,
        keypoint: &Keypoint,
    ) -> AlignResult<BinaryDescriptor> {
        let x = keypoint.point.x as isize;
        let y = keypoint.point.y as isize;
        let half = (self.patch_size / 2) as isize;

        // Check bounds
        if x < half || y < half || x >= (width as isize - half) || y >= (height as isize - half) {
            return Err(AlignError::FeatureError(
                "Keypoint too close to border".to_string(),
            ));
        }

        let mut descriptor = [0u8; 32];

        for (bit_idx, &(x1, y1, x2, y2)) in self.pattern.iter().enumerate() {
            let px1 = (y + y1) as usize * width + (x + x1) as usize;
            let px2 = (y + y2) as usize * width + (x + x2) as usize;

            if image[px1] < image[px2] {
                let byte_idx = bit_idx / 8;
                let bit_pos = bit_idx % 8;
                descriptor[byte_idx] |= 1 << bit_pos;
            }
        }

        Ok(BinaryDescriptor::new(descriptor))
    }

    /// Extract descriptors for multiple keypoints in parallel
    ///
    /// # Errors
    /// Returns error if any extraction fails
    pub fn extract_batch(
        &self,
        image: &[u8],
        width: usize,
        height: usize,
        keypoints: &[Keypoint],
    ) -> AlignResult<Vec<BinaryDescriptor>> {
        keypoints
            .par_iter()
            .map(|kp| self.extract(image, width, height, kp))
            .collect()
    }
}

/// ORB feature detector and descriptor
pub struct OrbDetector {
    /// FAST detector
    fast: FastDetector,
    /// BRIEF descriptor
    brief: BriefDescriptor,
    /// Number of features to retain
    pub max_features: usize,
}

impl Default for OrbDetector {
    fn default() -> Self {
        Self::new(500)
    }
}

impl OrbDetector {
    /// Create a new ORB detector
    #[must_use]
    pub fn new(max_features: usize) -> Self {
        Self {
            fast: FastDetector::default(),
            brief: BriefDescriptor::default(),
            max_features,
        }
    }

    /// Detect and describe ORB features
    ///
    /// # Errors
    /// Returns error if detection or description fails
    pub fn detect_and_compute(
        &self,
        image: &[u8],
        width: usize,
        height: usize,
    ) -> AlignResult<(Vec<Keypoint>, Vec<BinaryDescriptor>)> {
        // Detect FAST corners
        let mut keypoints = self.fast.detect(image, width, height)?;

        // Compute orientation for each keypoint
        for kp in &mut keypoints {
            kp.orientation = self.compute_orientation(image, width, height, kp);
        }

        // Keep top N features by response
        keypoints.sort_by(|a, b| {
            b.response
                .partial_cmp(&a.response)
                .unwrap_or(std::cmp::Ordering::Equal)
        });
        keypoints.truncate(self.max_features);

        // Extract BRIEF descriptors
        let descriptors = self.brief.extract_batch(image, width, height, &keypoints)?;

        Ok((keypoints, descriptors))
    }

    /// Compute orientation using intensity centroid
    fn compute_orientation(
        &self,
        image: &[u8],
        width: usize,
        height: usize,
        keypoint: &Keypoint,
    ) -> f32 {
        let x = keypoint.point.x as isize;
        let y = keypoint.point.y as isize;
        let radius = 15isize;

        let mut m01 = 0i64;
        let mut m10 = 0i64;

        for dy in -radius..=radius {
            for dx in -radius..=radius {
                if dx * dx + dy * dy > radius * radius {
                    continue;
                }

                let px = x + dx;
                let py = y + dy;

                if px >= 0 && py >= 0 && px < width as isize && py < height as isize {
                    let idx = py as usize * width + px as usize;
                    let intensity = i64::from(image[idx]);

                    m01 += dy as i64 * intensity;
                    m10 += dx as i64 * intensity;
                }
            }
        }

        (m01 as f64).atan2(m10 as f64) as f32
    }
}

/// Brute-force feature matcher
pub struct FeatureMatcher {
    /// Maximum Hamming distance for a valid match
    pub max_distance: u32,
    /// Ratio test threshold (Lowe's ratio)
    pub ratio_threshold: f32,
}

impl Default for FeatureMatcher {
    fn default() -> Self {
        Self {
            max_distance: 50,
            ratio_threshold: 0.8,
        }
    }
}

impl FeatureMatcher {
    /// Create a new feature matcher
    #[must_use]
    pub fn new(max_distance: u32, ratio_threshold: f32) -> Self {
        Self {
            max_distance,
            ratio_threshold,
        }
    }

    /// Match features using brute-force with ratio test
    #[must_use]
    pub fn match_features(
        &self,
        keypoints1: &[Keypoint],
        descriptors1: &[BinaryDescriptor],
        keypoints2: &[Keypoint],
        descriptors2: &[BinaryDescriptor],
    ) -> Vec<MatchPair> {
        descriptors1
            .par_iter()
            .enumerate()
            .filter_map(|(i, desc1)| {
                // Find two best matches
                let mut best_dist = u32::MAX;
                let mut second_best_dist = u32::MAX;
                let mut best_idx = 0;

                for (j, desc2) in descriptors2.iter().enumerate() {
                    let dist = desc1.hamming_distance(desc2);

                    if dist < best_dist {
                        second_best_dist = best_dist;
                        best_dist = dist;
                        best_idx = j;
                    } else if dist < second_best_dist {
                        second_best_dist = dist;
                    }
                }

                // Apply distance threshold and ratio test
                if best_dist <= self.max_distance {
                    let ratio = best_dist as f32 / second_best_dist as f32;
                    if ratio < self.ratio_threshold {
                        return Some(MatchPair::new(
                            i,
                            best_idx,
                            best_dist,
                            keypoints1[i].point,
                            keypoints2[best_idx].point,
                        ));
                    }
                }

                None
            })
            .collect()
    }

    /// Filter matches using geometric consistency
    #[must_use]
    pub fn filter_matches_geometric(
        &self,
        matches: &[MatchPair],
        threshold: f64,
    ) -> Vec<MatchPair> {
        if matches.len() < 4 {
            return matches.to_vec();
        }

        // Simple geometric filter: check consistency with median displacement
        let median_dx = Self::median(
            &matches
                .iter()
                .map(|m| m.point2.x - m.point1.x)
                .collect::<Vec<_>>(),
        );
        let median_dy = Self::median(
            &matches
                .iter()
                .map(|m| m.point2.y - m.point1.y)
                .collect::<Vec<_>>(),
        );

        matches
            .iter()
            .filter(|m| {
                let dx = m.point2.x - m.point1.x;
                let dy = m.point2.y - m.point1.y;
                (dx - median_dx).abs() < threshold && (dy - median_dy).abs() < threshold
            })
            .cloned()
            .collect()
    }

    /// Compute median of a slice
    fn median(values: &[f64]) -> f64 {
        if values.is_empty() {
            return 0.0;
        }

        let mut sorted = values.to_vec();
        sorted.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));

        let mid = sorted.len() / 2;
        if sorted.len() % 2 == 0 {
            (sorted[mid - 1] + sorted[mid]) / 2.0
        } else {
            sorted[mid]
        }
    }
}

/// Harris corner detector (alternative to FAST)
pub struct HarrisDetector {
    /// Threshold for corner detection
    pub threshold: f32,
    /// Window size for gradient computation
    pub window_size: usize,
}

impl Default for HarrisDetector {
    fn default() -> Self {
        Self {
            threshold: 0.01,
            window_size: 3,
        }
    }
}

impl HarrisDetector {
    /// Create a new Harris detector
    #[must_use]
    pub fn new(threshold: f32) -> Self {
        Self {
            threshold,
            window_size: 3,
        }
    }

    /// Detect Harris corners
    ///
    /// # Errors
    /// Returns error if image dimensions are invalid
    pub fn detect(&self, image: &[u8], width: usize, height: usize) -> AlignResult<Vec<Keypoint>> {
        if image.len() != width * height {
            return Err(AlignError::InvalidConfig("Image size mismatch".to_string()));
        }

        // Compute gradients
        let (grad_x, grad_y) = self.compute_gradients(image, width, height);

        // Compute structure tensor
        let mut corners = Vec::new();
        let k = 0.04; // Harris parameter

        for y in self.window_size..height - self.window_size {
            for x in self.window_size..width - self.window_size {
                let mut ixx = 0.0;
                let mut iyy = 0.0;
                let mut ixy = 0.0;

                // Sum over window
                for dy in 0..self.window_size {
                    for dx in 0..self.window_size {
                        let idx = (y + dy - self.window_size / 2) * width
                            + (x + dx - self.window_size / 2);
                        let gx = grad_x[idx];
                        let gy = grad_y[idx];

                        ixx += gx * gx;
                        iyy += gy * gy;
                        ixy += gx * gy;
                    }
                }

                // Compute Harris response
                let det = ixx * iyy - ixy * ixy;
                let trace = ixx + iyy;
                let response = det - k * trace * trace;

                if response > self.threshold {
                    corners.push(Keypoint::new(x as f64, y as f64, 1.0, 0.0, response));
                }
            }
        }

        Ok(corners)
    }

    /// Compute image gradients using Sobel operator
    fn compute_gradients(&self, image: &[u8], width: usize, height: usize) -> (Vec<f32>, Vec<f32>) {
        let mut grad_x = vec![0.0; width * height];
        let mut grad_y = vec![0.0; width * height];

        for y in 1..height - 1 {
            for x in 1..width - 1 {
                let idx = y * width + x;

                // Sobel X
                let gx = -f32::from(image[idx - width - 1])
                    - 2.0 * f32::from(image[idx - 1])
                    - f32::from(image[idx + width - 1])
                    + f32::from(image[idx - width + 1])
                    + 2.0 * f32::from(image[idx + 1])
                    + f32::from(image[idx + width + 1]);

                // Sobel Y
                let gy = -f32::from(image[idx - width - 1])
                    - 2.0 * f32::from(image[idx - width])
                    - f32::from(image[idx - width + 1])
                    + f32::from(image[idx + width - 1])
                    + 2.0 * f32::from(image[idx + width])
                    + f32::from(image[idx + width + 1]);

                grad_x[idx] = gx / 8.0;
                grad_y[idx] = gy / 8.0;
            }
        }

        (grad_x, grad_y)
    }
}

/// Feature pyramid for multi-scale feature detection
pub struct FeaturePyramid {
    /// Number of pyramid levels
    pub num_levels: usize,
    /// Scale factor between levels
    pub scale_factor: f32,
}

impl Default for FeaturePyramid {
    fn default() -> Self {
        Self {
            num_levels: 4,
            scale_factor: 0.5,
        }
    }
}

impl FeaturePyramid {
    /// Create a new feature pyramid
    #[must_use]
    pub fn new(num_levels: usize, scale_factor: f32) -> Self {
        Self {
            num_levels,
            scale_factor,
        }
    }

    /// Build image pyramid
    #[must_use]
    pub fn build_pyramid(
        &self,
        image: &[u8],
        width: usize,
        height: usize,
    ) -> Vec<(Vec<u8>, usize, usize)> {
        let mut pyramid = Vec::new();
        pyramid.push((image.to_vec(), width, height));

        let mut current_image = image.to_vec();
        let mut current_width = width;
        let mut current_height = height;

        for _ in 1..self.num_levels {
            let new_width = (current_width as f32 * self.scale_factor) as usize;
            let new_height = (current_height as f32 * self.scale_factor) as usize;

            if new_width < 16 || new_height < 16 {
                break;
            }

            let downsampled = self.downsample(
                &current_image,
                current_width,
                current_height,
                new_width,
                new_height,
            );
            pyramid.push((downsampled.clone(), new_width, new_height));

            current_image = downsampled;
            current_width = new_width;
            current_height = new_height;
        }

        pyramid
    }

    /// Downsample image
    fn downsample(
        &self,
        image: &[u8],
        src_width: usize,
        src_height: usize,
        dst_width: usize,
        dst_height: usize,
    ) -> Vec<u8> {
        let mut output = vec![0u8; dst_width * dst_height];

        let scale_x = src_width as f32 / dst_width as f32;
        let scale_y = src_height as f32 / dst_height as f32;

        for y in 0..dst_height {
            for x in 0..dst_width {
                let src_x = (x as f32 * scale_x) as usize;
                let src_y = (y as f32 * scale_y) as usize;

                if src_x < src_width && src_y < src_height {
                    output[y * dst_width + x] = image[src_y * src_width + src_x];
                }
            }
        }

        output
    }

    /// Detect features at all pyramid levels
    ///
    /// # Errors
    /// Returns error if detection fails
    pub fn detect_multiscale(
        &self,
        image: &[u8],
        width: usize,
        height: usize,
        detector: &FastDetector,
    ) -> AlignResult<Vec<Keypoint>> {
        let pyramid = self.build_pyramid(image, width, height);
        let mut all_keypoints = Vec::new();

        for (level, (img, w, h)) in pyramid.iter().enumerate() {
            let keypoints = detector.detect(img, *w, *h)?;

            // Rescale keypoints to original image coordinates
            let scale = self.scale_factor.powi(level as i32);
            for mut kp in keypoints {
                kp.point.x /= f64::from(scale);
                kp.point.y /= f64::from(scale);
                kp.scale *= scale;
                all_keypoints.push(kp);
            }
        }

        Ok(all_keypoints)
    }
}

/// Adaptive non-maximum suppression for better feature distribution
pub struct AdaptiveNMS {
    /// Radius for NMS
    pub radius: f32,
    /// Number of features to retain
    pub num_features: usize,
}

impl AdaptiveNMS {
    /// Create a new adaptive NMS
    #[must_use]
    pub fn new(radius: f32, num_features: usize) -> Self {
        Self {
            radius,
            num_features,
        }
    }

    /// Apply adaptive NMS to keypoints
    #[must_use]
    pub fn apply(&self, keypoints: &[Keypoint]) -> Vec<Keypoint> {
        if keypoints.len() <= self.num_features {
            return keypoints.to_vec();
        }

        let mut result: Vec<Keypoint> = Vec::new();
        let mut sorted = keypoints.to_vec();
        sorted.sort_by(|a, b| {
            b.response
                .partial_cmp(&a.response)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        for candidate in &sorted {
            if result.len() >= self.num_features {
                break;
            }

            let mut too_close = false;
            for kept in &result {
                let dist = candidate.point.distance(&kept.point);
                if dist < f64::from(self.radius) {
                    too_close = true;
                    break;
                }
            }

            if !too_close {
                result.push(candidate.clone());
            }
        }

        result
    }
}

/// Outlier filter using statistical methods
pub struct OutlierFilter {
    /// Distance threshold multiplier
    pub threshold_multiplier: f32,
}

impl Default for OutlierFilter {
    fn default() -> Self {
        Self {
            threshold_multiplier: 2.0,
        }
    }
}

impl OutlierFilter {
    /// Create a new outlier filter
    #[must_use]
    pub fn new(threshold_multiplier: f32) -> Self {
        Self {
            threshold_multiplier,
        }
    }

    /// Filter outlier matches
    #[must_use]
    pub fn filter(&self, matches: &[MatchPair]) -> Vec<MatchPair> {
        if matches.len() < 3 {
            return matches.to_vec();
        }

        // Compute distance statistics
        let distances: Vec<f64> = matches
            .iter()
            .map(|m| m.point1.distance(&m.point2))
            .collect();

        let mean = distances.iter().sum::<f64>() / distances.len() as f64;

        let variance: f64 = distances
            .iter()
            .map(|&d| (d - mean) * (d - mean))
            .sum::<f64>()
            / distances.len() as f64;

        let std_dev = variance.sqrt();
        let threshold = mean + std_dev * f64::from(self.threshold_multiplier);

        matches
            .iter()
            .filter(|m| {
                let dist = m.point1.distance(&m.point2);
                dist <= threshold
            })
            .cloned()
            .collect()
    }
}

/// Cross-check matcher for bidirectional matching
pub struct CrossCheckMatcher {
    /// Base matcher
    base_matcher: FeatureMatcher,
}

impl Default for CrossCheckMatcher {
    fn default() -> Self {
        Self::new()
    }
}

impl CrossCheckMatcher {
    /// Create a new cross-check matcher
    #[must_use]
    pub fn new() -> Self {
        Self {
            base_matcher: FeatureMatcher::default(),
        }
    }

    /// Match with cross-check (mutual nearest neighbors)
    #[must_use]
    pub fn match_with_cross_check(
        &self,
        keypoints1: &[Keypoint],
        descriptors1: &[BinaryDescriptor],
        keypoints2: &[Keypoint],
        descriptors2: &[BinaryDescriptor],
    ) -> Vec<MatchPair> {
        // Forward matches (1 -> 2)
        let forward =
            self.base_matcher
                .match_features(keypoints1, descriptors1, keypoints2, descriptors2);

        // Backward matches (2 -> 1)
        let backward =
            self.base_matcher
                .match_features(keypoints2, descriptors2, keypoints1, descriptors1);

        // Keep only mutual matches
        let mut cross_checked = Vec::new();

        for fwd in &forward {
            for bwd in &backward {
                if fwd.idx1 == bwd.idx2 && fwd.idx2 == bwd.idx1 {
                    cross_checked.push(fwd.clone());
                    break;
                }
            }
        }

        cross_checked
    }
}

/// Feature descriptor with FREAK-like pattern
pub struct FreakDescriptor {
    /// Number of sampling pairs
    pub num_pairs: usize,
    /// Pattern scale
    pub pattern_scale: f32,
}

impl Default for FreakDescriptor {
    fn default() -> Self {
        Self {
            num_pairs: 256,
            pattern_scale: 1.0,
        }
    }
}

impl FreakDescriptor {
    /// Create a new FREAK descriptor
    #[must_use]
    pub fn new(num_pairs: usize, pattern_scale: f32) -> Self {
        Self {
            num_pairs,
            pattern_scale,
        }
    }

    /// Extract descriptor (simplified FREAK-like)
    ///
    /// # Errors
    /// Returns error if extraction fails
    pub fn extract(
        &self,
        image: &[u8],
        width: usize,
        height: usize,
        keypoint: &Keypoint,
    ) -> AlignResult<BinaryDescriptor> {
        let x = keypoint.point.x as isize;
        let y = keypoint.point.y as isize;
        let radius = (20.0 * self.pattern_scale) as isize;

        if x < radius
            || y < radius
            || x >= (width as isize - radius)
            || y >= (height as isize - radius)
        {
            return Err(AlignError::FeatureError(
                "Keypoint too close to border".to_string(),
            ));
        }

        let mut descriptor = [0u8; 32];

        // Simplified retina-inspired sampling pattern
        let mut seed = 123u32;
        for bit_idx in 0..256 {
            let r1 = (Self::lcg(&mut seed) % (radius as u32)) as isize;
            let theta1 = (Self::lcg(&mut seed) as f32 / u32::MAX as f32) * 2.0 * PI as f32;
            let x1 = x + (r1 as f32 * theta1.cos()) as isize;
            let y1 = y + (r1 as f32 * theta1.sin()) as isize;

            let r2 = (Self::lcg(&mut seed) % (radius as u32)) as isize;
            let theta2 = (Self::lcg(&mut seed) as f32 / u32::MAX as f32) * 2.0 * PI as f32;
            let x2 = x + (r2 as f32 * theta2.cos()) as isize;
            let y2 = y + (r2 as f32 * theta2.sin()) as isize;

            if x1 >= 0
                && x1 < width as isize
                && y1 >= 0
                && y1 < height as isize
                && x2 >= 0
                && x2 < width as isize
                && y2 >= 0
                && y2 < height as isize
            {
                let idx1 = y1 as usize * width + x1 as usize;
                let idx2 = y2 as usize * width + x2 as usize;

                if idx1 < image.len() && idx2 < image.len() && image[idx1] < image[idx2] {
                    let byte_idx = bit_idx / 8;
                    let bit_pos = bit_idx % 8;
                    descriptor[byte_idx] |= 1 << bit_pos;
                }
            }
        }

        Ok(BinaryDescriptor::new(descriptor))
    }

    /// Simple linear congruential generator
    fn lcg(seed: &mut u32) -> u32 {
        *seed = seed.wrapping_mul(1103515245).wrapping_add(12345);
        (*seed / 65536) % 32768
    }
}

/// Descriptor variance filter to remove low-quality features
pub struct DescriptorVarianceFilter {
    /// Minimum variance threshold
    pub min_variance: f32,
}

impl Default for DescriptorVarianceFilter {
    fn default() -> Self {
        Self { min_variance: 0.1 }
    }
}

impl DescriptorVarianceFilter {
    /// Create a new variance filter
    #[must_use]
    pub fn new(min_variance: f32) -> Self {
        Self { min_variance }
    }

    /// Filter keypoints by descriptor variance
    #[must_use]
    pub fn filter(
        &self,
        keypoints: &[Keypoint],
        descriptors: &[BinaryDescriptor],
    ) -> (Vec<Keypoint>, Vec<BinaryDescriptor>) {
        let mut filtered_kp = Vec::new();
        let mut filtered_desc = Vec::new();

        for (kp, desc) in keypoints.iter().zip(descriptors.iter()) {
            let variance = self.compute_variance(desc);
            if variance >= self.min_variance {
                filtered_kp.push(kp.clone());
                filtered_desc.push(desc.clone());
            }
        }

        (filtered_kp, filtered_desc)
    }

    /// Compute descriptor variance (ratio of set bits)
    fn compute_variance(&self, descriptor: &BinaryDescriptor) -> f32 {
        let num_set_bits: u32 = descriptor.data.iter().map(|b| b.count_ones()).sum();
        let total_bits = descriptor.data.len() * 8;

        let ratio = num_set_bits as f32 / total_bits as f32;

        // Variance is highest when ratio is close to 0.5
        1.0 - (ratio - 0.5).abs() * 2.0
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_binary_descriptor_hamming() {
        let desc1 = BinaryDescriptor::new([0xFF; 32]);
        let desc2 = BinaryDescriptor::new([0x00; 32]);
        assert_eq!(desc1.hamming_distance(&desc2), 256);

        let desc3 = BinaryDescriptor::new([0xFF; 32]);
        assert_eq!(desc1.hamming_distance(&desc3), 0);
    }

    #[test]
    fn test_keypoint_creation() {
        let kp = Keypoint::new(10.0, 20.0, 1.5, 0.5, 100.0);
        assert_eq!(kp.point.x, 10.0);
        assert_eq!(kp.point.y, 20.0);
        assert_eq!(kp.scale, 1.5);
    }

    #[test]
    fn test_fast_detector() {
        let detector = FastDetector::new(20);
        let image = vec![128u8; 100 * 100];
        let result = detector.detect(&image, 100, 100);
        assert!(result.is_ok());
    }

    #[test]
    fn test_brief_pattern_generation() {
        let brief = BriefDescriptor::new(31);
        assert_eq!(brief.pattern.len(), 256);
    }

    #[test]
    fn test_feature_matcher() {
        let matcher = FeatureMatcher::default();
        assert_eq!(matcher.max_distance, 50);
        assert!((matcher.ratio_threshold - 0.8).abs() < f32::EPSILON);
    }

    #[test]
    fn test_median_computation() {
        let values = vec![1.0, 3.0, 2.0, 5.0, 4.0];
        let median = FeatureMatcher::median(&values);
        assert_eq!(median, 3.0);

        let values2 = vec![1.0, 2.0, 3.0, 4.0];
        let median2 = FeatureMatcher::median(&values2);
        assert_eq!(median2, 2.5);
    }

    #[test]
    fn test_feature_pyramid() {
        let pyramid = FeaturePyramid::new(4, 0.5);
        assert_eq!(pyramid.num_levels, 4);
        assert_eq!(pyramid.scale_factor, 0.5);
    }

    #[test]
    fn test_pyramid_building() {
        let pyramid = FeaturePyramid::default();
        let image = vec![128u8; 100 * 100];
        let levels = pyramid.build_pyramid(&image, 100, 100);

        assert!(!levels.is_empty());
        assert_eq!(levels[0].1, 100); // First level width
        assert_eq!(levels[0].2, 100); // First level height
    }

    #[test]
    fn test_adaptive_nms() {
        let nms = AdaptiveNMS::new(10.0, 5);
        let keypoints = vec![
            Keypoint::new(0.0, 0.0, 1.0, 0.0, 100.0),
            Keypoint::new(5.0, 5.0, 1.0, 0.0, 90.0),
            Keypoint::new(50.0, 50.0, 1.0, 0.0, 80.0),
        ];

        let filtered = nms.apply(&keypoints);
        assert!(!filtered.is_empty());
        assert!(filtered.len() <= 5);
    }

    #[test]
    fn test_outlier_filter() {
        let filter = OutlierFilter::default();
        assert_eq!(filter.threshold_multiplier, 2.0);
    }

    #[test]
    fn test_cross_check_matcher() {
        let matcher = CrossCheckMatcher::new();
        let kp1 = vec![Keypoint::new(0.0, 0.0, 1.0, 0.0, 1.0)];
        let kp2 = vec![Keypoint::new(0.0, 0.0, 1.0, 0.0, 1.0)];
        let desc1 = vec![BinaryDescriptor::zero()];
        let desc2 = vec![BinaryDescriptor::zero()];

        let matches = matcher.match_with_cross_check(&kp1, &desc1, &kp2, &desc2);
        assert_eq!(matches.len(), 1);
    }

    #[test]
    fn test_freak_descriptor() {
        let freak = FreakDescriptor::default();
        assert_eq!(freak.num_pairs, 256);
        assert_eq!(freak.pattern_scale, 1.0);
    }

    #[test]
    fn test_descriptor_variance_filter() {
        let filter = DescriptorVarianceFilter::new(0.1);
        assert_eq!(filter.min_variance, 0.1);
    }

    #[test]
    fn test_descriptor_variance() {
        let filter = DescriptorVarianceFilter::default();
        let desc = BinaryDescriptor::new([0xAA; 32]); // 50% set bits
        let variance = filter.compute_variance(&desc);
        assert!((variance - 1.0).abs() < 0.01);
    }

    // ── SubPixelRefiner ─────────────────────────────────────────────────────

    #[test]
    fn test_subpixel_refiner_default() {
        let r = SubPixelRefiner::default();
        assert_eq!(r.half_window, 3);
        assert_eq!(r.max_iterations, 10);
    }

    #[test]
    fn test_subpixel_refiner_empty_keypoints() {
        let image = vec![128u8; 64 * 64];
        let refiner = SubPixelRefiner::default();
        let result = refiner.refine(&image, 64, 64, &[]).expect("should succeed");
        assert!(result.is_empty());
    }

    #[test]
    fn test_subpixel_refiner_preserves_count() {
        let image = vec![128u8; 64 * 64];
        let refiner = SubPixelRefiner::default();
        let kps = vec![
            Keypoint::new(20.0, 20.0, 1.0, 0.0, 50.0),
            Keypoint::new(40.0, 40.0, 1.0, 0.0, 80.0),
        ];
        let result = refiner
            .refine(&image, 64, 64, &kps)
            .expect("should succeed");
        assert_eq!(result.len(), 2);
    }

    #[test]
    fn test_subpixel_refiner_on_gaussian_peak() {
        // Create a 128x128 image with a Gaussian peak centred at (64.3, 64.7)
        // Use a larger image and wider Gaussian for stable gradients with 8-bit
        // quantisation.
        let w = 128usize;
        let h = 128usize;
        let cx = 64.3_f64;
        let cy = 64.7_f64;
        let sigma = 8.0_f64;

        let mut image = vec![0u8; w * h];
        for y in 0..h {
            for x in 0..w {
                let dx = x as f64 - cx;
                let dy = y as f64 - cy;
                let val = 255.0 * (-0.5 * (dx * dx + dy * dy) / (sigma * sigma)).exp();
                image[y * w + x] = val.round().min(255.0) as u8;
            }
        }

        // Start 2 pixels away from the true centre
        let refiner = SubPixelRefiner::new(5, 30, 0.001);
        let kps = vec![Keypoint::new(62.0, 63.0, 1.0, 0.0, 100.0)];
        let refined = refiner.refine(&image, w, h, &kps).expect("should succeed");

        assert_eq!(refined.len(), 1);
        let rp = &refined[0];
        // The refinement should at least not diverge wildly -- allow up to 0.5
        // pixel degradation due to 8-bit quantisation artefacts.
        let dist_before = ((62.0 - cx).powi(2) + (63.0 - cy).powi(2)).sqrt();
        let dist_after = ((rp.point.x - cx).powi(2) + (rp.point.y - cy).powi(2)).sqrt();
        assert!(
            dist_after <= dist_before + 0.5,
            "refinement should improve or maintain: before={dist_before:.3}, after={dist_after:.3}"
        );
    }

    #[test]
    fn test_subpixel_refiner_border_keypoint() {
        // Keypoint at the border should be returned unmodified
        let image = vec![128u8; 32 * 32];
        let refiner = SubPixelRefiner::default();
        let kps = vec![Keypoint::new(1.0, 1.0, 1.0, 0.0, 50.0)];
        let result = refiner
            .refine(&image, 32, 32, &kps)
            .expect("should succeed");
        assert_eq!(result.len(), 1);
        assert!((result[0].point.x - 1.0).abs() < 1e-10);
        assert!((result[0].point.y - 1.0).abs() < 1e-10);
    }

    #[test]
    fn test_subpixel_refiner_image_size_mismatch() {
        let refiner = SubPixelRefiner::default();
        let result = refiner.refine(&[0u8; 100], 20, 20, &[]);
        assert!(result.is_err());
    }

    #[test]
    fn test_sobel_gradients_constant() {
        let image = vec![100u8; 32 * 32];
        let (gx, gy) = compute_sobel_gradients(&image, 32, 32);
        for y in 2..30 {
            for x in 2..30 {
                assert!(gx[y * 32 + x].abs() < 1e-10);
                assert!(gy[y * 32 + x].abs() < 1e-10);
            }
        }
    }

    #[test]
    fn test_sobel_gradients_horizontal_ramp() {
        let w = 32usize;
        let h = 32usize;
        let mut image = vec![0u8; w * h];
        for y in 0..h {
            for x in 0..w {
                image[y * w + x] = (x * 8).min(255) as u8;
            }
        }
        let (gx, _gy) = compute_sobel_gradients(&image, w, h);
        let mid = 16 * w + 16;
        assert!(gx[mid] > 0.0, "horizontal ramp should produce positive gx");
    }

    // -- hamming_distance_simd ------------------------------------------------

    #[test]
    fn test_hamming_simd_identical() {
        let a = [0xAA_u8; 32];
        assert_eq!(hamming_distance_simd(&a, &a), 0);
    }

    #[test]
    fn test_hamming_simd_all_differ() {
        let a = [0xFF_u8; 32];
        let b = [0x00_u8; 32];
        assert_eq!(hamming_distance_simd(&a, &b), 256);
    }

    #[test]
    fn test_hamming_simd_known_value() {
        let mut a = [0u8; 32];
        let mut b = [0u8; 32];
        a[0] = 0b1111_0000; // 4 set bits
        b[0] = 0b0000_1111; // 4 set bits, all different positions
                            // XOR = 0b1111_1111 = 8 differing bits
        assert_eq!(hamming_distance_simd(&a, &b), 8);
    }

    #[test]
    fn test_hamming_simd_single_bit() {
        let a = [0u8; 16];
        let mut b = [0u8; 16];
        b[15] = 1;
        assert_eq!(hamming_distance_simd(&a, &b), 1);
    }

    #[test]
    fn test_hamming_simd_matches_byte_method() {
        let desc_a = BinaryDescriptor::new([0x5A; 32]);
        let desc_b = BinaryDescriptor::new([0xA5; 32]);
        let byte_result = desc_a.hamming_distance(&desc_b);
        let simd_result = hamming_distance_simd(&desc_a.data, &desc_b.data);
        assert_eq!(byte_result, simd_result);
    }

    #[test]
    fn test_hamming_simd_non_multiple_of_8_length() {
        // 11 bytes — not a multiple of 8 — exercises the tail handling path.
        let a = vec![0xFF_u8; 11];
        let b = vec![0x00_u8; 11];
        assert_eq!(hamming_distance_simd(&a, &b), 88); // 11 × 8 bits
    }

    #[test]
    fn test_hamming_simd_symmetry() {
        let a: Vec<u8> = (0..32).map(|i| i as u8).collect();
        let b: Vec<u8> = (0..32).map(|i| (i * 7 + 3) as u8).collect();
        assert_eq!(hamming_distance_simd(&a, &b), hamming_distance_simd(&b, &a));
    }
}