Class: Naiso::ImageSplitter

Inherits:
Object
  • Object
show all
Defined in:
lib/naiso/image_splitter.rb

Overview

이미지 분할기

Instance Method Summary collapse

Constructor Details

#initialize(config = nil) ⇒ ImageSplitter

Returns a new instance of ImageSplitter.



9
10
11
# File 'lib/naiso/image_splitter.rb', line 9

def initialize(config = nil)
  @config = config || SplitConfig.new
end

Instance Method Details

#split(image_path, output_dir: nil, verbose: true) ⇒ Object



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
# File 'lib/naiso/image_splitter.rb', line 13

def split(image_path, output_dir: nil, verbose: true)
  result = SplitResult.new

  # 이미지 로드
  image = Vips::Image.new_from_file(image_path)

  # 설정값 계산
  min_height = @config.min_section_height || (image.width * 2 / 3)
  max_height = @config.max_section_height || (image.width * 1.5).to_i

  if verbose
    puts "이미지 크기: #{image.width} x #{image.height}"
    puts "최소 섹션 높이: #{min_height}px"
    puts "최대 섹션 높이: #{max_height}px"
  end

  # 분석기 및 감지기 초기화
  analyzer = RowAnalyzer.new(image)
  detector = SplitPointDetector.new(analyzer, @config)

  # 분할점 수집
  result.uniform_regions = detector.find_uniform_regions
  result.divider_lines = detector.find_divider_lines
  result.background_transitions = detector.find_background_transitions

  print_detection_results(result) if verbose

  # 분할점 병합
  split_points = merge_split_points(result, image.height, min_height)

  # 최대 높이 초과 섹션 분할
  if max_height > 0
    split_points, complexity_splits = apply_max_height_splits(
      split_points, max_height, min_height, detector, verbose
    )
    result.complexity_splits = complexity_splits
  end

  result.split_points = split_points

  if verbose
    puts "\n분할 위치: #{split_points}"
    puts "생성될 섹션 수: #{split_points.size - 1}개"
  end

  # 이미지 분할 및 저장
  if split_points.nil? || split_points.size < 2
    puts '분할할 영역을 찾지 못했습니다.' if verbose
    return result
  end

  output_dir = prepare_output_dir(image_path, output_dir)
  result.output_files = save_sections(image, split_points, output_dir, image_path, verbose)

  result
end