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
//! Image enhancement operations

use crate::core::image::OcrImage;
use crate::utils::{OcrError, Result, SimdImageOps};
use image::{imageops, DynamicImage, GrayImage, Luma};

/// Image enhancement operations
pub struct ImageEnhancer;

impl ImageEnhancer {
    /// Enhance image contrast
    pub fn enhance_contrast(img: &OcrImage, factor: f32) -> Result<OcrImage> {
        let gray = img.data.to_luma8();
        let pixels: Vec<u8> = gray.pixels().map(|p| p[0]).collect();
        let adjusted = SimdImageOps::contrast_adjust(&pixels, factor);
        let mut result = GrayImage::new(gray.width(), gray.height());
        for (i, pixel) in adjusted.iter().enumerate() {
            let x = (i as u32) % gray.width();
            let y = (i as u32) / gray.width();
            result.put_pixel(x, y, Luma([*pixel]));
        }
        Ok(OcrImage::new(DynamicImage::ImageLuma8(result), img.dpi))
    }

    /// Reduce image noise (using Gaussian blur)
    pub fn reduce_noise(img: &OcrImage) -> Result<OcrImage> {
        let blurred = imageops::blur(&img.data, 0.5);
        Ok(OcrImage::new(DynamicImage::ImageRgba8(blurred), img.dpi))
    }

    /// Sharpen image
    pub fn sharpen(img: &OcrImage) -> Result<OcrImage> {
        let mut sharpened = img.data.clone();
        imageops::unsharpen(&mut sharpened, 1.0, 1);
        Ok(OcrImage::new(sharpened, img.dpi))
    }

    /// Deskew image by searching ±15° (handles typical scanner skew including ~10–15°).
    ///
    /// Coarse search at 1° steps, then refine ±1° at 0.1° around the best.
    /// Larger in-page rotations are handled by `OrientedCclDetector`.
    pub fn deskew(img: &OcrImage) -> Result<OcrImage> {
        let gray = img.to_grayscale();
        let binary = gray.threshold(200);

        let mut best_angle = 0.0f32;
        let mut max_variance = 0.0f64;

        for i in -15..=15 {
            let angle_rad = (i as f32).to_radians();
            let rotated = binary.rotate(angle_rad)?;
            let variance = Self::calculate_projection_variance(&rotated);
            if variance > max_variance {
                max_variance = variance;
                best_angle = angle_rad;
            }
        }

        let best_deg = best_angle.to_degrees();
        for i in -10..=10 {
            let angle_deg = best_deg + i as f32 * 0.1;
            let angle_rad = angle_deg.to_radians();
            let rotated = binary.rotate(angle_rad)?;
            let variance = Self::calculate_projection_variance(&rotated);
            if variance > max_variance {
                max_variance = variance;
                best_angle = angle_rad;
            }
        }

        if best_angle.abs() > 0.001 {
            img.rotate(best_angle).map_err(OcrError::from)
        } else {
            Ok(img.clone())
        }
    }

    /// Estimate DPI from image content by analyzing stroke widths
    ///
    /// Returns an estimated DPI. Falls back to the image's stored DPI if
    /// estimation is not possible. Typical printed text has strokes ~1pt wide,
    /// which at 300 DPI is ~4px.
    pub fn estimate_dpi(img: &OcrImage) -> u32 {
        let gray = img.data.to_luma8();
        let (width, height) = gray.dimensions();

        if width < 50 || height < 50 {
            return img.dpi.max(72);
        }

        let mid_y = height / 2;
        let mut run_lengths = Vec::new();
        let mut current_run = 0u32;
        let mut in_dark = gray.get_pixel(0, mid_y)[0] < 128;

        for x in 0..width {
            let dark = gray.get_pixel(x, mid_y)[0] < 128;
            if dark == in_dark {
                current_run += 1;
            } else {
                if in_dark && current_run > 0 && current_run < width / 4 {
                    run_lengths.push(current_run);
                }
                current_run = 1;
                in_dark = dark;
            }
        }

        if run_lengths.len() < 3 {
            return img.dpi.max(72);
        }

        run_lengths.sort_unstable();
        let median_stroke = run_lengths[run_lengths.len() / 2];

        let estimated = (median_stroke as f32 * 72.0) as u32;
        estimated.clamp(72, 1200).max(img.dpi)
    }

    /// Detect image orientation (0, 90, 180, 270 degrees)
    ///
    /// Uses horizontal projection variance: upright text has higher
    /// variance because text lines create distinct dark bands separated
    /// by white space.
    pub fn detect_orientation(img: &OcrImage) -> u32 {
        let gray = img.data.to_luma8();
        let (width, height) = gray.dimensions();

        if width < 50 || height < 50 {
            return 0;
        }

        let binary = Self::simple_threshold(&gray);

        let score_0 = Self::orientation_score(&binary, width, height);
        let score_90 = Self::orientation_score(&Self::rotate_90_cw(&binary), height, width);
        let score_180 = Self::orientation_score(&Self::rotate_180(&binary), width, height);
        let score_270 = Self::orientation_score(&Self::rotate_90_ccw(&binary), height, width);

        let scores = [
            (0u32, score_0),
            (90, score_90),
            (180, score_180),
            (270, score_270),
        ];
        let best = scores
            .iter()
            .max_by(|a, b| a.1.partial_cmp(&b.1).unwrap())
            .unwrap();

        best.0
    }

    /// Rotate image to correct orientation based on detected text direction
    pub fn correct_orientation(img: &OcrImage) -> Result<OcrImage> {
        let orientation = Self::detect_orientation(img);

        match orientation {
            0 => Ok(img.clone()),
            90 => img
                .rotate(std::f32::consts::FRAC_PI_2)
                .map_err(OcrError::from),
            180 => img.rotate(std::f32::consts::PI).map_err(OcrError::from),
            270 => img
                .rotate(-std::f32::consts::FRAC_PI_2)
                .map_err(OcrError::from),
            _ => Ok(img.clone()),
        }
    }

    /// Remove document borders/margins
    ///
    /// Scans inward from each edge to find the first row/column containing
    /// dark pixels, then crops with a small margin.
    pub fn remove_borders(img: &OcrImage) -> Result<OcrImage> {
        let gray = img.data.to_luma8();
        let (width, height) = gray.dimensions();

        if width < 20 || height < 20 {
            return Ok(img.clone());
        }

        let threshold = 200u8;
        let margin = 5u32;

        let mut left = margin;
        let mut right = width - margin - 1;
        let mut top = margin;
        let mut bottom = height - margin - 1;

        for x in margin..(width - margin) {
            let mut has_content = false;
            for y in margin..(height - margin) {
                if gray.get_pixel(x, y)[0] < threshold {
                    has_content = true;
                    break;
                }
            }
            if has_content {
                left = x.saturating_sub(margin);
                break;
            }
        }

        for x in (margin..(width - margin)).rev() {
            let mut has_content = false;
            for y in margin..(height - margin) {
                if gray.get_pixel(x, y)[0] < threshold {
                    has_content = true;
                    break;
                }
            }
            if has_content {
                right = (x + margin).min(width);
                break;
            }
        }

        for y in margin..(height - margin) {
            let mut has_content = false;
            for x in margin..(width - margin) {
                if gray.get_pixel(x, y)[0] < threshold {
                    has_content = true;
                    break;
                }
            }
            if has_content {
                top = y.saturating_sub(margin);
                break;
            }
        }

        for y in (margin..(height - margin)).rev() {
            let mut has_content = false;
            for x in margin..(width - margin) {
                if gray.get_pixel(x, y)[0] < threshold {
                    has_content = true;
                    break;
                }
            }
            if has_content {
                bottom = (y + margin).min(height);
                break;
            }
        }

        let crop_width = right.saturating_sub(left);
        let crop_height = bottom.saturating_sub(top);

        if crop_width < 20 || crop_height < 20 || crop_width >= width || crop_height >= height {
            return Ok(img.clone());
        }

        img.crop(left, top, crop_width, crop_height)
            .map_err(OcrError::from)
    }

    /// Remove speckle noise (small connected components of dark pixels)
    ///
    /// Finds connected components of dark pixels using two-pass labeling
    /// and removes those whose area is smaller than `max_speckle_area`.
    pub fn remove_speckle(img: &OcrImage, max_speckle_area: u32) -> Result<OcrImage> {
        let gray = img.data.to_luma8();
        let (width, height) = gray.dimensions();

        if width == 0 || height == 0 {
            return Ok(img.clone());
        }

        let total_pixels = (width * height) as usize;
        let mut labels = vec![0u32; total_pixels];
        let mut next_label = 1u32;
        let mut areas = vec![0u32; total_pixels + 1];

        for y in 0..height {
            for x in 0..width {
                let idx = (y * width + x) as usize;
                if gray.get_pixel(x, y)[0] >= 128 {
                    continue;
                }

                let up = if y > 0 {
                    labels[((y - 1) * width + x) as usize]
                } else {
                    0
                };
                let left_label = if x > 0 {
                    labels[(y * width + x - 1) as usize]
                } else {
                    0
                };

                if up == 0 && left_label == 0 {
                    labels[idx] = next_label;
                    next_label += 1;
                } else if up > 0 && left_label == 0 {
                    labels[idx] = up;
                } else if left_label > 0 && up == 0 {
                    labels[idx] = left_label;
                } else {
                    labels[idx] = up.min(left_label);
                }
            }
        }

        for &label in &labels {
            if label > 0 && (label as usize) < areas.len() {
                areas[label as usize] += 1;
            }
        }

        let mut result = gray.clone();
        for y in 0..height {
            for x in 0..width {
                let idx = (y * width + x) as usize;
                let label = labels[idx];
                if label > 0
                    && (label as usize) < areas.len()
                    && areas[label as usize] < max_speckle_area
                {
                    result.put_pixel(x, y, Luma([255u8]));
                }
            }
        }

        Ok(OcrImage::new(DynamicImage::ImageLuma8(result), img.dpi))
    }

    fn simple_threshold(gray: &GrayImage) -> GrayImage {
        let (width, height) = gray.dimensions();

        let mut histogram = [0u32; 256];
        for pixel in gray.pixels() {
            histogram[pixel[0] as usize] += 1;
        }

        let total = (width * height) as u32;
        let mut sum = 0u32;
        for i in 0..256 {
            sum += histogram[i] * (i as u32);
        }

        let mut sum_b = 0u32;
        let mut w_b = 0u32;
        let mut max_variance = 0f64;
        let mut threshold = 128u8;

        for i in 0..256 {
            w_b += histogram[i];
            if w_b == 0 {
                continue;
            }
            let w_f = total - w_b;
            if w_f == 0 {
                break;
            }
            sum_b += (i as u32) * histogram[i];
            let m_b = sum_b as f64 / w_b as f64;
            let m_f = (sum - sum_b) as f64 / w_f as f64;
            let v = (w_b as f64) * (w_f as f64) * (m_b - m_f) * (m_b - m_f);
            if v > max_variance {
                max_variance = v;
                threshold = i as u8;
            }
        }

        let mut result = GrayImage::new(width, height);
        for (x, y, pixel) in gray.enumerate_pixels() {
            let value = if pixel[0] > threshold { 255u8 } else { 0u8 };
            result.put_pixel(x, y, Luma([value]));
        }
        result
    }

    fn orientation_score(binary: &GrayImage, width: u32, height: u32) -> f64 {
        let mut row_sums = Vec::with_capacity(height as usize);
        for y in 0..height {
            let mut sum = 0u64;
            for x in 0..width {
                if binary.get_pixel(x, y)[0] < 128 {
                    sum += 1;
                }
            }
            row_sums.push(sum as f64);
        }

        if row_sums.is_empty() {
            return 0.0;
        }

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

        variance
    }

    fn rotate_90_cw(img: &GrayImage) -> GrayImage {
        let (width, height) = img.dimensions();
        let mut result = GrayImage::new(height, width);
        for y in 0..height {
            for x in 0..width {
                let src_pixel = img.get_pixel(x, y);
                result.put_pixel(height - 1 - y, x, *src_pixel);
            }
        }
        result
    }

    fn rotate_90_ccw(img: &GrayImage) -> GrayImage {
        let (width, height) = img.dimensions();
        let mut result = GrayImage::new(height, width);
        for y in 0..height {
            for x in 0..width {
                let src_pixel = img.get_pixel(x, y);
                result.put_pixel(y, width - 1 - x, *src_pixel);
            }
        }
        result
    }

    fn rotate_180(img: &GrayImage) -> GrayImage {
        let (width, height) = img.dimensions();
        let mut result = GrayImage::new(width, height);
        for y in 0..height {
            for x in 0..width {
                let src_pixel = img.get_pixel(x, y);
                result.put_pixel(width - 1 - x, height - 1 - y, *src_pixel);
            }
        }
        result
    }

    fn calculate_projection_variance(img: &OcrImage) -> f64 {
        let (width, height) = img.dimensions();
        let mut row_sums = Vec::with_capacity(height as usize);

        if let Some(buf) = img.data.as_luma8() {
            for y in 0..height {
                let mut sum = 0u64;
                for x in 0..width {
                    sum += buf.get_pixel(x, y)[0] as u64;
                }
                row_sums.push(sum as f64);
            }
        } else {
            return 0.0;
        }

        if row_sums.is_empty() {
            return 0.0;
        }

        let mean = row_sums.iter().sum::<f64>() / row_sums.len() as f64;
        let variance = row_sums
            .iter()
            .map(|s| {
                let diff = s - mean;
                diff * diff
            })
            .sum::<f64>()
            / row_sums.len() as f64;

        variance
    }
}