ocr 0.1.2

A pure Rust CLI OCR tool for printed text extraction
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
//! Region detection operations
//!
//! Provides a `TextDetector` trait with multiple implementations:
//! - `CclDetector`: Connected component labeling (baseline)
//! - `CnnDetector`: Lightweight CNN-based text detection (stub)

use crate::core::layout::*;
use crate::core::text::BoundingBox;
use crate::image::thresholder::{ImageThresholder, ThresholdMethod};
use crate::utils::Result;
use image::Luma;
use imageproc::distance_transform::Norm;
use imageproc::morphology::dilate;
use imageproc::region_labelling::{connected_components, Connectivity};

/// Trait for text region detection algorithms
pub trait TextDetector: Send + Sync {
    /// Detect text regions in an image
    fn detect(&self, image: &crate::core::image::OcrImage) -> Result<Vec<TextRegion>>;
    /// Detector name
    fn name(&self) -> &'static str;
}

/// CCL-based text detector using connected component labeling
pub struct CclDetector;

impl TextDetector for CclDetector {
    fn detect(&self, image: &crate::core::image::OcrImage) -> Result<Vec<TextRegion>> {
        TextRegionDetector::detect_text_regions(image)
    }

    fn name(&self) -> &'static str {
        "CCL"
    }
}

/// CNN-based text detector using a lightweight detection CNN
pub struct CnnDetector {
    cnn: crate::layout::detection_cnn::TextDetectionCNN,
}

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

impl CnnDetector {
    /// Create a new CNN detector with heuristic initial weights
    pub fn new() -> Self {
        Self {
            cnn: crate::layout::detection_cnn::TextDetectionCNN::new(),
        }
    }
}

impl TextDetector for CnnDetector {
    fn detect(&self, image: &crate::core::image::OcrImage) -> Result<Vec<TextRegion>> {
        let gray = image.to_grayscale();
        let luma = gray.data.to_luma8();
        let (w, h) = (luma.width() as usize, luma.height() as usize);

        // Convert to normalized ndarray [0,1]
        let mut arr = ndarray::Array2::zeros((h, w));
        for y in 0..h {
            for x in 0..w {
                arr[(y, x)] = luma.get_pixel(x as u32, y as u32)[0] as f32 / 255.0;
            }
        }

        // Run CNN
        let heatmap = self.cnn.forward(&arr);

        // Post-process: threshold, connected components, bounding boxes
        let boxes = self.cnn.post_process(&heatmap, 0.5, 20);

        let mut regions = Vec::new();
        for (min_x, max_x, min_y, max_y) in boxes {
            let bbox = BoundingBox::new(min_x as u32, min_y as u32, (max_x + 1) as u32, (max_y + 1) as u32);
            regions.push(TextRegion::new(
                format!("cnn_{}_{}", min_x, min_y),
                bbox,
                String::new(),
            ));
        }

        // Fallback to CCL if CNN finds nothing (safety net)
        if regions.is_empty() {
            return TextRegionDetector::detect_text_regions(image);
        }

        Ok(regions)
    }

    fn name(&self) -> &'static str {
        "CNN"
    }
}

/// Multi-angle CCL detector: finds text that is not axis-aligned.
///
/// Sweeps candidate rotations, runs CCL on each, maps boxes back to the
/// original image, and keeps the highest-scoring non-overlapping set.
pub struct OrientedCclDetector {
    /// Angle step in degrees (e.g. 15 → tries -45,-30,…,45)
    pub angle_step_deg: f32,
    /// Maximum absolute angle to search
    pub max_angle_deg: f32,
}

impl Default for OrientedCclDetector {
    fn default() -> Self {
        Self {
            angle_step_deg: 15.0,
            max_angle_deg: 45.0,
        }
    }
}

impl OrientedCclDetector {
    pub fn new(angle_step_deg: f32, max_angle_deg: f32) -> Self {
        Self {
            angle_step_deg: angle_step_deg.max(1.0),
            max_angle_deg: max_angle_deg.max(0.0),
        }
    }

    fn candidate_angles(&self) -> Vec<f32> {
        let mut angles = Vec::new();
        let mut a = -self.max_angle_deg;
        while a <= self.max_angle_deg + 1e-3 {
            angles.push(a);
            a += self.angle_step_deg;
        }
        if !angles.iter().any(|x| x.abs() < 1e-3) {
            angles.push(0.0);
        }
        angles.sort_by(|a, b| a.partial_cmp(b).unwrap_or(std::cmp::Ordering::Equal));
        angles.dedup_by(|a, b| (*a - *b).abs() < 1e-3);
        angles
    }
}

impl TextDetector for OrientedCclDetector {
    fn detect(&self, image: &crate::core::image::OcrImage) -> Result<Vec<TextRegion>> {
        let (ow, oh) = (image.width as f32, image.height as f32);
        let cx = ow / 2.0;
        let cy = oh / 2.0;

        let mut candidates: Vec<(TextRegion, f32)> = Vec::new();

        for angle_deg in self.candidate_angles() {
            let angle_rad = angle_deg.to_radians();
            let rotated = if angle_deg.abs() < 1e-3 {
                image.clone()
            } else {
                image.rotate(angle_rad)?
            };

            let regions = TextRegionDetector::detect_text_regions(&rotated)?;
            let (rw, rh) = (rotated.width as f32, rotated.height as f32);
            let rcx = rw / 2.0;
            let rcy = rh / 2.0;

            for mut region in regions {
                // Map axis-aligned box corners from rotated → original
                let bbox = &region.bounding_box;
                let corners = [
                    (bbox.left as f32, bbox.top as f32),
                    (bbox.right as f32, bbox.top as f32),
                    (bbox.right as f32, bbox.bottom as f32),
                    (bbox.left as f32, bbox.bottom as f32),
                ];
                let inv = -angle_rad;
                let mapped: Vec<(f32, f32)> = corners
                    .iter()
                    .map(|&(x, y)| {
                        let dx = x - rcx;
                        let dy = y - rcy;
                        let cos = inv.cos();
                        let sin = inv.sin();
                        (cx + dx * cos - dy * sin, cy + dx * sin + dy * cos)
                    })
                    .collect();

                let min_x = mapped
                    .iter()
                    .map(|p| p.0)
                    .fold(f32::INFINITY, f32::min)
                    .clamp(0.0, ow - 1.0);
                let max_x = mapped
                    .iter()
                    .map(|p| p.0)
                    .fold(f32::NEG_INFINITY, f32::max)
                    .clamp(0.0, ow - 1.0);
                let min_y = mapped
                    .iter()
                    .map(|p| p.1)
                    .fold(f32::INFINITY, f32::min)
                    .clamp(0.0, oh - 1.0);
                let max_y = mapped
                    .iter()
                    .map(|p| p.1)
                    .fold(f32::NEG_INFINITY, f32::max)
                    .clamp(0.0, oh - 1.0);

                if max_x <= min_x || max_y <= min_y {
                    continue;
                }

                let mapped_bbox = BoundingBox::new(
                    min_x as u32,
                    min_y as u32,
                    max_x as u32 + 1,
                    max_y as u32 + 1,
                );
                region.bounding_box = mapped_bbox;
                region.properties.rotation_deg = angle_deg;
                region.id = format!("orient_{}_{}", angle_deg as i32, region.id);

                // Prefer wider text-like regions; penalize near-square blobs slightly
                let w = region.bounding_box.width() as f32;
                let h = region.bounding_box.height().max(1) as f32;
                let aspect = w / h;
                let score = (w * h) * aspect.clamp(0.5, 8.0);
                candidates.push((region, score));
            }
        }

        if candidates.is_empty() {
            return TextRegionDetector::detect_text_regions(image);
        }

        // Sort by score descending and greedy NMS
        candidates.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(std::cmp::Ordering::Equal));
        let mut kept: Vec<TextRegion> = Vec::new();
        for (region, _) in candidates {
            let overlaps = kept.iter().any(|k| {
                bbox_iou(&k.bounding_box, &region.bounding_box) > 0.3
            });
            if !overlaps {
                kept.push(region);
            }
        }

        Ok(kept)
    }

    fn name(&self) -> &'static str {
        "OrientedCCL"
    }
}

fn bbox_iou(a: &BoundingBox, b: &BoundingBox) -> f32 {
    let x1 = a.left.max(b.left);
    let y1 = a.top.max(b.top);
    let x2 = a.right.min(b.right);
    let y2 = a.bottom.min(b.bottom);
    if x2 <= x1 || y2 <= y1 {
        return 0.0;
    }
    let inter = ((x2 - x1) * (y2 - y1)) as f32;
    let area_a = a.area() as f32;
    let area_b = b.area() as f32;
    let union = area_a + area_b - inter;
    if union <= 0.0 {
        0.0
    } else {
        inter / union
    }
}

/// Text region detector
pub struct TextRegionDetector;

impl TextRegionDetector {
    /// Detect text regions
    pub fn detect_text_regions(img: &crate::core::image::OcrImage) -> Result<Vec<TextRegion>> {
        // 1. Binarize image
        let mut thresholder = ImageThresholder::new();
        thresholder.set_image(img.clone())?;
        let binary = thresholder.threshold(ThresholdMethod::Otsu)?;

        // 2. Invert if needed (we want white text on black background for morphological ops usually,
        // or just consider foreground as non-zero).
        // Imageproc assumes non-zero is foreground.
        // If binary is standard (text=0, bg=255), we need to invert.
        // Let's check typical output of thresholding. usually text is black (0).
        // So we invert to make text white (255).
        let mut gray = binary.data.to_luma8();
        image::imageops::invert(&mut gray);

        // 3. Dilate to merge characters into words/lines
        // Kernel size depends on resolution. Assuming 300 DPI.
        // 3x3 or 5x5 kernel.
        let dilated = dilate(&gray, Norm::LInf, 2); // k=2 means 5x5 kernel (2*k+1)

        // 4. Connected components
        let labeled = connected_components(&dilated, Connectivity::Eight, Luma([0u8]));

        // 5. Extract bounding boxes
        let mut components = std::collections::HashMap::new();

        for (x, y, pixel) in labeled.enumerate_pixels() {
            let label = pixel[0];
            if label > 0 {
                components
                    .entry(label)
                    .or_insert_with(Vec::new)
                    .push((x, y));
            }
        }

        let mut regions = Vec::new();

        for (label, pixels) in components {
            if pixels.is_empty() {
                continue;
            }

            let min_x = pixels.iter().map(|p| p.0).min().unwrap();
            let max_x = pixels.iter().map(|p| p.0).max().unwrap();
            let min_y = pixels.iter().map(|p| p.1).min().unwrap();
            let max_y = pixels.iter().map(|p| p.1).max().unwrap();

            let width = max_x - min_x + 1;
            let height = max_y - min_y + 1;

            // Filter noise
            if width < 5 || height < 5 {
                continue;
            }

            let bbox = BoundingBox::new(
                min_x as u32,
                min_y as u32,
                (max_x + 1) as u32,
                (max_y + 1) as u32,
            );

            regions.push(TextRegion::new(
                format!("region_{}", label),
                bbox,
                String::new(), // Content not yet recognized
            ));
        }

        Ok(regions)
    }

    /// Isolate characters in a text region
    pub fn isolate_characters(
        img: &crate::core::image::OcrImage,
        region: &TextRegion,
    ) -> Result<Vec<BoundingBox>> {
        // Extract region image
        let region_img = img.crop(
            region.bounding_box.left,
            region.bounding_box.top,
            region.bounding_box.width(),
            region.bounding_box.height(),
        )?;
        let gray = region_img.to_grayscale();

        // Binarize locally (simple threshold for now)
        let binary = gray.threshold(128);

        // Connected components to find characters
        // Characters are usually connected components
        // But for touching characters, we might need projection profile.
        // For now, use connected components as a baseline.

        // We need to invert if text is black (0).
        let mut luma = binary.data.to_luma8();
        image::imageops::invert(&mut luma);

        let labeled = connected_components(&luma, Connectivity::Eight, Luma([0u8]));

        let mut components = std::collections::HashMap::new();
        for (x, y, pixel) in labeled.enumerate_pixels() {
            let label = pixel[0];
            if label > 0 {
                components
                    .entry(label)
                    .or_insert_with(Vec::new)
                    .push((x, y));
            }
        }

        let mut char_boxes = Vec::new();
        for (_, pixels) in components {
            if pixels.is_empty() {
                continue;
            }

            let min_x = pixels.iter().map(|p| p.0).min().unwrap();
            let max_x = pixels.iter().map(|p| p.0).max().unwrap();
            let min_y = pixels.iter().map(|p| p.1).min().unwrap();
            let max_y = pixels.iter().map(|p| p.1).max().unwrap();

            // Filter noise (too small)
            if max_x - min_x < 2 || max_y - min_y < 2 {
                continue;
            }

            let char_bbox = BoundingBox::new(
                region.bounding_box.left + min_x as u32,
                region.bounding_box.top + min_y as u32,
                region.bounding_box.left + max_x as u32 + 1,
                region.bounding_box.top + max_y as u32 + 1,
            );

            char_boxes.push(char_bbox);
        }

        // Sort left to right
        char_boxes.sort_by_key(|b| b.left);

        Ok(char_boxes)
    }

    /// Check if a text region contains handwritten text
    pub fn is_handwritten(img: &crate::core::image::OcrImage, region: &TextRegion) -> bool {
        // Heuristic: Handwritten text has high variance in character height and vertical position
        // 1. Isolate components (characters)
        if let Ok(chars) = Self::isolate_characters(img, region) {
            if chars.len() < 3 {
                return false;
            } // Too few characters to judge

            // Calculate heights
            let heights: Vec<f32> = chars.iter().map(|b| b.height() as f32).collect();
            let count = heights.len() as f32;
            let mean_height: f32 = heights.iter().sum::<f32>() / count;

            // Calculate variance
            let variance: f32 = heights
                .iter()
                .map(|h| (h - mean_height).powi(2))
                .sum::<f32>()
                / count;
            let std_dev = variance.sqrt();
            let cv = std_dev / mean_height; // Coefficient of variation

            // Calculate vertical scatter (baseline irregularity)
            // Use bottom coordinate as baseline proxy
            let bottoms: Vec<f32> = chars.iter().map(|b| b.bottom as f32).collect();
            let mean_bottom: f32 = bottoms.iter().sum::<f32>() / count;
            let bottom_variance: f32 = bottoms
                .iter()
                .map(|b| (b - mean_bottom).powi(2))
                .sum::<f32>()
                / count;
            let bottom_std_dev = bottom_variance.sqrt();

            // Thresholds (tuned empirically)
            // High height variation (> 0.25) OR high baseline variation (> 5.0 pixels)
            // Printed text usually has CV < 0.1 and baseline std dev < 2.0

            if cv > 0.25 || bottom_std_dev > 5.0 {
                return true;
            }
        }
        false
    }
}

/// Image region detector
pub struct ImageRegionDetector;

impl ImageRegionDetector {
    /// Detect image regions
    pub fn detect_image_regions(img: &crate::core::image::OcrImage) -> Result<Vec<ImageRegion>> {
        // 1. Binarize
        let mut thresholder = ImageThresholder::new();
        thresholder.set_image(img.clone())?;
        let binary = thresholder.threshold(ThresholdMethod::Otsu)?;

        let mut gray = binary.data.to_luma8();
        image::imageops::invert(&mut gray);

        // 2. Connected components without dilation (to find solid image blocks)
        // or with small dilation to merge lines in line drawings
        let dilated = dilate(&gray, Norm::LInf, 1);
        let labeled = connected_components(&dilated, Connectivity::Eight, Luma([0u8]));

        // 3. Extract and filter
        let mut components = std::collections::HashMap::new();
        for (x, y, pixel) in labeled.enumerate_pixels() {
            let label = pixel[0];
            if label > 0 {
                components
                    .entry(label)
                    .or_insert_with(Vec::new)
                    .push((x, y));
            }
        }

        let mut regions = Vec::new();
        for (label, pixels) in components {
            if pixels.is_empty() {
                continue;
            }

            let min_x = pixels.iter().map(|p| p.0).min().unwrap();
            let max_x = pixels.iter().map(|p| p.0).max().unwrap();
            let min_y = pixels.iter().map(|p| p.1).min().unwrap();
            let max_y = pixels.iter().map(|p| p.1).max().unwrap();

            let width = max_x - min_x + 1;
            let height = max_y - min_y + 1;

            // Heuristic: Images are large
            if width > 100 && height > 100 {
                let bbox = BoundingBox::new(
                    min_x as u32,
                    min_y as u32,
                    (max_x + 1) as u32,
                    (max_y + 1) as u32,
                );

                regions.push(ImageRegion::new(format!("img_{}", label), bbox));
            }
        }

        Ok(regions)
    }
}

/// Table detector
pub struct TableDetector;

impl TableDetector {
    /// Detect tables
    pub fn detect_tables(img: &crate::core::image::OcrImage) -> Result<Vec<Table>> {
        // 1. Binarize
        let mut thresholder = ImageThresholder::new();
        thresholder.set_image(img.clone())?;
        let binary = thresholder.threshold(ThresholdMethod::Otsu)?;

        let gray = binary.data.to_luma8();
        let width = gray.width();
        let height = gray.height();

        // 2. Detect horizontal lines
        let h_line_threshold = width / 5; // Must be at least 20% of page width
        let mut h_lines = Vec::new();

        for y in 0..height {
            let mut run_start = None;
            for x in 0..width {
                let pixel = gray.get_pixel(x, y)[0];
                if pixel < 128 {
                    // Dark pixel
                    if run_start.is_none() {
                        run_start = Some(x);
                    }
                } else {
                    if let Some(start) = run_start {
                        let len = x - start;
                        if len > h_line_threshold {
                            h_lines.push(BoundingBox::new(start, y, x, y + 1));
                        }
                        run_start = None;
                    }
                }
            }
            if let Some(start) = run_start {
                let len = width - start;
                if len > h_line_threshold {
                    h_lines.push(BoundingBox::new(start, y, width, y + 1));
                }
            }
        }

        // 3. Detect vertical lines
        let v_line_threshold = height / 10; // Must be at least 10% of page height
        let mut v_lines = Vec::new();

        for x in 0..width {
            let mut run_start = None;
            for y in 0..height {
                let pixel = gray.get_pixel(x, y)[0];
                if pixel < 128 {
                    // Dark pixel
                    if run_start.is_none() {
                        run_start = Some(y);
                    }
                } else {
                    if let Some(start) = run_start {
                        let len = y - start;
                        if len > v_line_threshold {
                            v_lines.push(BoundingBox::new(x, start, x + 1, y));
                        }
                        run_start = None;
                    }
                }
            }
            if let Some(start) = run_start {
                let len = height - start;
                if len > v_line_threshold {
                    v_lines.push(BoundingBox::new(x, start, x + 1, height));
                }
            }
        }

        // 4. Find intersections and group into tables
        let mut tables = Vec::new();

        if !h_lines.is_empty() && !v_lines.is_empty() {
            // Find bounding box of all lines
            let min_x = h_lines
                .iter()
                .map(|b| b.left)
                .min()
                .unwrap_or(0)
                .min(v_lines.iter().map(|b| b.left).min().unwrap_or(0));
            let max_x = h_lines
                .iter()
                .map(|b| b.right)
                .max()
                .unwrap_or(0)
                .max(v_lines.iter().map(|b| b.right).max().unwrap_or(0));
            let min_y = h_lines
                .iter()
                .map(|b| b.top)
                .min()
                .unwrap_or(0)
                .min(v_lines.iter().map(|b| b.top).min().unwrap_or(0));
            let max_y = h_lines
                .iter()
                .map(|b| b.bottom)
                .max()
                .unwrap_or(0)
                .max(v_lines.iter().map(|b| b.bottom).max().unwrap_or(0));

            // Check if we have enough lines to call it a table (e.g. at least 2 H and 2 V)
            if h_lines.len() >= 2 && v_lines.len() >= 2 {
                let bbox = BoundingBox::new(min_x, min_y, max_x, max_y);
                let mut table = Table::new("table_1".to_string(), bbox);

                // 5. Structure analysis (Cells)
                // Find intersections
                let mut row_coords = Vec::new();
                let mut col_coords = Vec::new();

                for h in &h_lines {
                    row_coords.push(h.top); // Y coordinate
                }
                for v in &v_lines {
                    col_coords.push(v.left); // X coordinate
                }

                // Sort and dedup with tolerance
                row_coords.sort();
                col_coords.sort();

                let mut unique_rows = Vec::new();
                if !row_coords.is_empty() {
                    unique_rows.push(row_coords[0]);
                    for &r in &row_coords[1..] {
                        if r > unique_rows.last().unwrap() + 10 {
                            // 10px tolerance
                            unique_rows.push(r);
                        }
                    }
                }

                let mut unique_cols = Vec::new();
                if !col_coords.is_empty() {
                    unique_cols.push(col_coords[0]);
                    for &c in &col_coords[1..] {
                        if c > unique_cols.last().unwrap() + 10 {
                            unique_cols.push(c);
                        }
                    }
                }

                // Create cells
                let rows = if unique_rows.len() > 1 {
                    unique_rows.len() - 1
                } else {
                    0
                };
                let cols = if unique_cols.len() > 1 {
                    unique_cols.len() - 1
                } else {
                    0
                };

                if rows > 0 && cols > 0 {
                    table.structure.rows = rows;
                    table.structure.columns = cols;

                    let mut cells = Vec::new();
                    for r in 0..rows {
                        let mut row_cells = Vec::new();
                        for c in 0..cols {
                            let cell_bbox = BoundingBox::new(
                                unique_cols[c],
                                unique_rows[r],
                                unique_cols[c + 1],
                                unique_rows[r + 1],
                            );

                            let mut cell = TableCell {
                                content: String::new(),
                                bounding_box: cell_bbox,
                                row_span: 1,
                                column_span: 1,
                                properties: CellProperties::default(),
                            };

                            // Basic border detection
                            cell.properties.borders.top.width = 1;
                            cell.properties.borders.bottom.width = 1;
                            cell.properties.borders.left.width = 1;
                            cell.properties.borders.right.width = 1;

                            row_cells.push(cell);
                        }
                        cells.push(row_cells);
                    }

                    // 6. Span inference: check which internal grid lines are actually present
                    Self::infer_spans(&mut cells, &unique_rows, &unique_cols, &gray);

                    table.structure.cells = cells;
                }

                tables.push(table);
            }
        }

        Ok(tables)
    }

    /// Infer row and column spans by checking which internal grid lines are actually present.
    fn infer_spans(
        cells: &mut [Vec<TableCell>],
        unique_rows: &[u32],
        unique_cols: &[u32],
        gray: &image::GrayImage,
    ) {
        if cells.is_empty() || cells[0].is_empty() {
            return;
        }
        let rows = cells.len();
        let cols = cells[0].len();

        // Check horizontal line presence between each pair of rows
        // h_present[r][c] = true if line between row r and r+1 exists at column c
        let mut h_present = vec![vec![false; cols]; rows.saturating_sub(1)];
        for r in 0..rows.saturating_sub(1) {
            let y = unique_rows[r + 1];
            if y >= gray.height() {
                continue;
            }
            for c in 0..cols {
                let x_start = unique_cols[c];
                let x_end = unique_cols[(c + 1).min(unique_cols.len() - 1)];
                let dark_count = Self::count_dark_pixels_horizontal(gray, y, x_start, x_end);
                let length = (x_end - x_start).max(1);
                // Line is "present" if > 30% of pixels are dark
                h_present[r][c] = (dark_count as f32 / length as f32) > 0.30;
            }
        }

        // Check vertical line presence between each pair of columns
        // v_present[r][c] = true if line between col c and c+1 exists at row r
        let mut v_present = vec![vec![false; cols.saturating_sub(1)]; rows];
        for r in 0..rows {
            let y_start = unique_rows[r];
            let y_end = unique_rows[(r + 1).min(unique_rows.len() - 1)];
            for c in 0..cols.saturating_sub(1) {
                let x = unique_cols[c + 1];
                if x >= gray.width() {
                    continue;
                }
                let dark_count = Self::count_dark_pixels_vertical(gray, x, y_start, y_end);
                let length = (y_end - y_start).max(1);
                v_present[r][c] = (dark_count as f32 / length as f32) > 0.30;
            }
        }

        // Merge column spans: for each row, find consecutive cells without vertical lines between them
        for r in 0..rows {
            let mut c = 0;
            while c < cols {
                let mut span = 1;
                while c + span < cols && !v_present[r][c + span - 1] {
                    span += 1;
                }
                for k in 0..span {
                    cells[r][c + k].column_span = span - k;
                }
                // Widen bbox for the first cell in the span
                cells[r][c].bounding_box.right = cells[r][c + span - 1].bounding_box.right;
                c += span;
            }
        }

        // Merge row spans: for each col, find consecutive cells without horizontal lines between them
        for c in 0..cols {
            let mut r = 0;
            while r < rows {
                let mut span = 1;
                while r + span < rows && !h_present[r + span - 1][c] {
                    span += 1;
                }
                for k in 0..span {
                    cells[r + k][c].row_span = span - k;
                }
                // Widen bbox for the first cell in the span
                cells[r][c].bounding_box.bottom = cells[r + span - 1][c].bounding_box.bottom;
                r += span;
            }
        }
    }

    fn count_dark_pixels_horizontal(
        gray: &image::GrayImage,
        y: u32,
        x_start: u32,
        x_end: u32,
    ) -> u32 {
        let mut count = 0;
        for x in x_start..x_end.min(gray.width()) {
            if gray.get_pixel(x, y)[0] < 128 {
                count += 1;
            }
        }
        count
    }

    fn count_dark_pixels_vertical(
        gray: &image::GrayImage,
        x: u32,
        y_start: u32,
        y_end: u32,
    ) -> u32 {
        let mut count = 0;
        for y in y_start..y_end.min(gray.height()) {
            if gray.get_pixel(x, y)[0] < 128 {
                count += 1;
            }
        }
        count
    }
}

#[cfg(test)]
mod oriented_tests {
    use super::*;
    use crate::core::image::OcrImage;
    use image::{DynamicImage, GrayImage, Luma};

    fn make_horizontal_bar() -> OcrImage {
        let mut img = GrayImage::from_pixel(80, 80, Luma([255]));
        for x in 10..70 {
            for y in 38..42 {
                img.put_pixel(x, y, Luma([0]));
            }
        }
        OcrImage::new(DynamicImage::ImageLuma8(img), 72)
    }

    #[test]
    fn test_oriented_detector_finds_upright_text() {
        let img = make_horizontal_bar();
        let det = OrientedCclDetector::new(15.0, 30.0);
        let regions = det.detect(&img).unwrap();
        assert!(!regions.is_empty(), "should find at least one region");
    }

    #[test]
    fn test_oriented_detector_on_rotated_image() {
        let img = make_horizontal_bar();
        let rotated = img.rotate(30.0_f32.to_radians()).unwrap();
        let det = OrientedCclDetector::new(15.0, 45.0);
        let regions = det.detect(&rotated).unwrap();
        assert!(!regions.is_empty());
        assert!(regions.iter().any(|r| r.id.contains("orient_")));
    }

    #[test]
    fn test_bbox_iou_identical() {
        let a = BoundingBox::new(0, 0, 10, 10);
        assert!((bbox_iou(&a, &a) - 1.0).abs() < 1e-5);
    }

    #[test]
    fn test_candidate_angles_include_zero() {
        let det = OrientedCclDetector::new(15.0, 45.0);
        let angles = det.candidate_angles();
        assert!(angles.iter().any(|a| a.abs() < 1e-3));
        assert!(*angles.first().unwrap() <= -45.0);
        assert!(*angles.last().unwrap() >= 45.0);
    }
}