File size: 6,199 Bytes
be1cb53
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from __future__ import annotations

from dataclasses import dataclass
from pathlib import Path

import cv2
import numpy as np


@dataclass(frozen=True)
class FrameRegion:
    id: str
    row: int
    col: int
    x: int
    y: int
    width: int
    height: int


@dataclass(frozen=True)
class DetectionResult:
    regions: list[FrameRegion]
    rows: int
    columns: int
    width: int
    height: int


@dataclass
class Band:
    start: int
    end: int


@dataclass(frozen=True)
class Segment:
    start: int
    end: int


def read_image(path: str | Path) -> np.ndarray:
    image = cv2.imdecode(np.fromfile(Path(path), dtype=np.uint8), cv2.IMREAD_COLOR)
    if image is None:
        raise ValueError(f"Could not read image: {path}")
    return image


def detect_grid(
    image: np.ndarray,
    min_frame_size: int = 80,
    sensitivity: int = 58,
    separator_mode: str = "auto",
) -> DetectionResult:
    height, width = image.shape[:2]
    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

    vertical_bands = find_separator_bands(
        gray=gray,
        axis="vertical",
        min_frame_size=min_frame_size,
        sensitivity=sensitivity,
        separator_mode=separator_mode,
    )
    horizontal_bands = find_separator_bands(
        gray=gray,
        axis="horizontal",
        min_frame_size=min_frame_size,
        sensitivity=sensitivity,
        separator_mode=separator_mode,
    )

    x_segments = bands_to_segments(vertical_bands, width, min_frame_size)
    y_segments = bands_to_segments(horizontal_bands, height, min_frame_size)
    regions: list[FrameRegion] = []

    for row, y_segment in enumerate(y_segments):
        for col, x_segment in enumerate(x_segments):
            regions.append(
                FrameRegion(
                    id=f"{row + 1}-{col + 1}",
                    row=row,
                    col=col,
                    x=x_segment.start,
                    y=y_segment.start,
                    width=x_segment.end - x_segment.start,
                    height=y_segment.end - y_segment.start,
                )
            )

    return DetectionResult(
        regions=regions,
        rows=len(y_segments),
        columns=len(x_segments),
        width=width,
        height=height,
    )


def extract_frames(image: np.ndarray, regions: list[FrameRegion]) -> list[np.ndarray]:
    frames = []
    for region in regions:
        frames.append(
            image[
                region.y : region.y + region.height,
                region.x : region.x + region.width,
            ].copy()
        )
    return frames


def annotate_regions(image: np.ndarray, regions: list[FrameRegion]) -> np.ndarray:
    annotated = image.copy()
    color = (190, 255, 55)

    for region in regions:
        cv2.rectangle(
            annotated,
            (region.x, region.y),
            (region.x + region.width, region.y + region.height),
            color,
            max(1, round(max(image.shape[:2]) / 700)),
        )

    return annotated


def find_separator_bands(
    gray: np.ndarray,
    axis: str,
    min_frame_size: int,
    sensitivity: int,
    separator_mode: str,
) -> list[Band]:
    sensitivity = max(0, min(100, sensitivity))
    dark_threshold = 10 + round(sensitivity * 0.24)
    light_threshold = 246 - round(sensitivity * 0.16)
    line_ratio_threshold = 0.972 - sensitivity * 0.00035

    if separator_mode == "dark":
        separator_mask = gray <= dark_threshold
    elif separator_mode == "light":
        separator_mask = gray >= light_threshold
    else:
        separator_mask = (gray <= dark_threshold) | (gray >= light_threshold)

    if axis == "vertical":
        profile = separator_mask.mean(axis=0)
        axis_size = gray.shape[1]
    else:
        profile = separator_mask.mean(axis=1)
        axis_size = gray.shape[0]

    candidate_indexes = np.flatnonzero(profile >= line_ratio_threshold)
    bands = group_indexes(candidate_indexes)
    max_thickness = max(2, min(round(axis_size * 0.018), round(min_frame_size * 0.35)))
    bands = [band for band in bands if band.end - band.start + 1 <= max_thickness]

    return merge_close_bands(bands, min_gap=max(2, round(min_frame_size * 0.08)))


def group_indexes(indexes: np.ndarray) -> list[Band]:
    if indexes.size == 0:
        return []

    bands: list[Band] = []
    start = int(indexes[0])
    previous = int(indexes[0])

    for raw_index in indexes[1:]:
        index = int(raw_index)
        if index <= previous + 2:
            previous = index
            continue

        bands.append(Band(start=start, end=previous))
        start = index
        previous = index

    bands.append(Band(start=start, end=previous))
    return bands


def merge_close_bands(bands: list[Band], min_gap: int) -> list[Band]:
    if not bands:
        return []

    merged = [bands[0]]
    for band in bands[1:]:
        current = merged[-1]
        if band.start - current.end <= min_gap:
            current.end = band.end
        else:
            merged.append(band)

    return merged


def bands_to_segments(bands: list[Band], axis_size: int, min_frame_size: int) -> list[Segment]:
    if not bands:
        return [Segment(start=0, end=axis_size)]

    edge_tolerance = max(3, round(axis_size * 0.008))
    first_pixel = 0
    last_pixel = axis_size
    internal_bands: list[Band] = []

    for band in bands:
        if band.start <= edge_tolerance:
            first_pixel = max(first_pixel, band.end + 1)
        elif band.end >= axis_size - edge_tolerance - 1:
            last_pixel = min(last_pixel, band.start)
        else:
            internal_bands.append(band)

    segments: list[Segment] = []
    segment_start = first_pixel

    for band in internal_bands:
        push_segment(segments, segment_start, band.start, min_frame_size)
        segment_start = band.end + 1

    push_segment(segments, segment_start, last_pixel, min_frame_size)
    return segments


def push_segment(segments: list[Segment], start: int, end: int, min_frame_size: int) -> None:
    start = max(0, int(round(start)))
    end = max(start, int(round(end)))

    if end - start >= min_frame_size:
        segments.append(Segment(start=start, end=end))