Class: Kreuzberg::Config::ImagePreprocessing

Inherits:
Object
  • Object
show all
Defined in:
lib/kreuzberg/config.rb

Overview

Image preprocessing configuration for OCR

Examples:

Basic preprocessing

preprocessing = ImagePreprocessing.new(
  binarization_method: "otsu",
  denoise: true
)

Advanced preprocessing

preprocessing = ImagePreprocessing.new(
  target_dpi: 600,
  auto_rotate: true,
  deskew: true,
  denoise: true,
  contrast_enhance: true,
  binarization_method: "sauvola",
  invert_colors: false
)

Constant Summary collapse

VALID_BINARIZATION_METHODS =
%w[otsu sauvola niblack wolf bradley adaptive].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_dpi: 300, auto_rotate: true, deskew: true, denoise: false, contrast_enhance: true, binarization_method: 'otsu', invert_colors: false) ⇒ ImagePreprocessing

Returns a new instance of ImagePreprocessing.

Raises:

  • (ArgumentError)


535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
# File 'lib/kreuzberg/config.rb', line 535

def initialize(
  target_dpi: 300,
  auto_rotate: true,
  deskew: true,
  denoise: false,
  contrast_enhance: true,
  binarization_method: 'otsu',
  invert_colors: false
)
  @target_dpi = target_dpi.to_i
  @auto_rotate = auto_rotate ? true : false
  @deskew = deskew ? true : false
  @denoise = denoise ? true : false
  @contrast_enhance = contrast_enhance ? true : false
  @binarization_method = binarization_method.to_s
  @invert_colors = invert_colors ? true : false

  # Validate binarization method
  return if VALID_BINARIZATION_METHODS.include?(@binarization_method)

  valid_methods = VALID_BINARIZATION_METHODS.join(', ')
  raise ArgumentError,
        "Invalid binarization_method: #{@binarization_method}. Valid methods are: #{valid_methods}"
end

Instance Attribute Details

#auto_rotateObject (readonly)

Returns the value of attribute auto_rotate.



530
531
532
# File 'lib/kreuzberg/config.rb', line 530

def auto_rotate
  @auto_rotate
end

#binarization_methodObject (readonly)

Returns the value of attribute binarization_method.



530
531
532
# File 'lib/kreuzberg/config.rb', line 530

def binarization_method
  @binarization_method
end

#contrast_enhanceObject (readonly)

Returns the value of attribute contrast_enhance.



530
531
532
# File 'lib/kreuzberg/config.rb', line 530

def contrast_enhance
  @contrast_enhance
end

#denoiseObject (readonly)

Returns the value of attribute denoise.



530
531
532
# File 'lib/kreuzberg/config.rb', line 530

def denoise
  @denoise
end

#deskewObject (readonly)

Returns the value of attribute deskew.



530
531
532
# File 'lib/kreuzberg/config.rb', line 530

def deskew
  @deskew
end

#invert_colorsObject (readonly)

Returns the value of attribute invert_colors.



530
531
532
# File 'lib/kreuzberg/config.rb', line 530

def invert_colors
  @invert_colors
end

#target_dpiObject (readonly)

Returns the value of attribute target_dpi.



530
531
532
# File 'lib/kreuzberg/config.rb', line 530

def target_dpi
  @target_dpi
end

Instance Method Details

#to_hObject



560
561
562
563
564
565
566
567
568
569
570
# File 'lib/kreuzberg/config.rb', line 560

def to_h
  {
    target_dpi: @target_dpi,
    auto_rotate: @auto_rotate,
    deskew: @deskew,
    denoise: @denoise,
    contrast_enhance: @contrast_enhance,
    binarization_method: @binarization_method,
    invert_colors: @invert_colors
  }
end