ocrodeg

repository·master·Indexed 41 days ago

https://github.com/nvlabs/ocrodeg

A Python library for document image degradation used to augment datasets for OCR and handwriting recognition. It provides tools to simulate geometric distortions, noise, blur, and printing artifacts, including functions for random transformations, warping, binary blur, and the generation of multiscale, fibrous, or blob-like noise.

Tokens
1.2K
Snippets
7
Records
8
Agent score
1%

What's inside ocrodeg

  1. Overview of ocrodeg

    master
    The ocrodeg package is a Python library designed for document image degradation. It is primarily used for data augmentation in handwriting recognition and Optical Character Recognition (OCR) applications by simulating various real-world imaging artifacts and distortions.
  2. Simulate printing styles (Multiscale and Fibrous)

    master

    You can simulate how text appears when printed on different surfaces using:

    • ocrodeg.printlike_multiscale(patch): Simulates printing with multiscale noise effects.
    • ocrodeg.printlike_fibrous(patch): Simulates printing with fibrous texture effects.
    # Simulate multiscale printing
    printed_multiscale = ocrodeg.printlike_multiscale(patch)
    
    # Simulate fibrous printing
    printed_fibrous = ocrodeg.printlike_fibrous(patch)
  3. Generate random blobs and blotches

    master

    To simulate stains or blotches on a document:

    • ocrodeg.random_blobs(shape, density, size): Generates a random blob noise map.
    • ocrodeg.random_blotches(patch, density, scale): Applies random blotches directly to an image patch.
    # Generate a blob noise map
    blobs = ocrodeg.random_blobs(patch.shape, 3e-4, 20)
    
    # Apply blotches to an image
    blotched_image = ocrodeg.random_blotches(patch, 3e-4, 1e-4)
  4. Apply random distortions (warping)

    master

    To model paper curl or ink spread, you can apply random distortions to an image.

    1. Small/Noisy distortions: Use ocrodeg.bounded_gaussian_noise to create a noise map, then apply it using ocrodeg.distort_with_noise.
    2. Large 1D distortions: Use ocrodeg.noise_distort1d to create a 1D noise map for modeling paper curl, then apply it with ocrodeg.distort_with_noise.
    # Modeling small distortions (e.g., ink spread)
    noise = ocrodeg.bounded_gaussian_noise(image.shape, sigma=2.0, scale=5.0)
    distorted = ocrodeg.distort_with_noise(image, noise)
    
    # Modeling large 1D distortions (e.g., paper curl)
    noise_1d = ocrodeg.noise_distort1d(image.shape, magnitude=100.0)
    distorted_1d = ocrodeg.distort_with_noise(image, noise_1d)
  5. Generate multiscale noise

    master

    Use ocrodeg.make_multiscale_noise_uniform(shape) to generate a uniform multiscale noise map of a given shape (e.g., (512, 512)).

    noisy_map = ocrodeg.make_multiscale_noise_uniform((512, 512))
  6. Generate fibrous noise

    master

    Use ocrodeg.make_fibrous_image(shape, param1, param2, param3) to create a fibrous noise image, which can simulate paper texture or fiber-like artifacts.

    fibrous_img = ocrodeg.make_fibrous_image((256, 256), 700, 300, 0.01)
  7. Apply random geometric transformations

    master

    You can apply random geometric transformations (rotation, anisotropy, scale, and translation) to an image using ocrodeg.transform_image.

    To generate a set of random parameters that are suitable for document degradation, use ocrodeg.random_transform(). You can pass these parameters as keyword arguments to transform_image or override specific ranges by providing your own values.

    # Generate random parameters
    params = ocrodeg.random_transform()
    # Apply the transformation
    degraded_image = ocrodeg.transform_image(image, **params)
    
    # Alternatively, apply specific parameters directly
    degraded_image = ocrodeg.transform_image(image, angle=0.05, aniso=1.2, scale=0.9, translation=(0.01, 0.02))
  8. Apply blur and thresholding

    master

    The library provides utilities for modeling imaging artifacts like blurring and thresholding.

    • ocrodeg.binary_blur(patch, s, noise=s): Applies a binary blur to a patch. The parameter s controls the blur strength, and noise can be used to add noise during the process.
    # Apply binary blur with a specific strength and noise level
    blurred = ocrodeg.binary_blur(patch, s=2.0, noise=0.1)