from __future__ import annotations from typing import Iterable, Tuple import cv2 import numpy as np CNN_INPUT_SIZE = 32 def compute_dark_ink_channel(bgr: np.ndarray) -> np.ndarray: """ Produce a color-suppressed dark-ink grayscale image where colored printed form lines & A/B/C/D letters (red/maroon/orange/blue/green) are suppressed while student pen/pencil ink covering a letter stays intensely dark. """ b, g, r = cv2.split(bgr) sat_red = float(np.mean(r.astype(np.float32) - b.astype(np.float32))) if sat_red > 6.5: return r gray = cv2.cvtColor(bgr, cv2.COLOR_BGR2GRAY) max_ch = np.maximum(np.maximum(b, g), r) return cv2.addWeighted(gray, 0.55, max_ch, 0.45, 0) def extract_4ch_bubble_tensor( bgr_sheet: np.ndarray, cx: float, cy: float, radius: float, ink_sheet: np.ndarray | None = None, ) -> np.ndarray: """ Extract a normalized 4-channel (4, 32, 32) float32 tensor centered on a single bubble: - Ch 0: Red channel / 255.0 (suppresses red printed A/B/C/D letters!) - Ch 1: Green channel / 255.0 - Ch 2: Blue channel / 255.0 - Ch 3: Local background-subtracted dark-ink occlusion map in [0, 1] (0 = clean paper / unoccluded letter, 1 = heavy student ink blinding the letter) """ h, w = bgr_sheet.shape[:2] r = max(6.0, float(radius)) half = int(round(r * 1.30)) ix, iy = int(round(cx)), int(round(cy)) # Pad safely if near border x0, x1 = ix - half, ix + half + 1 y0, y1 = iy - half, iy + half + 1 pad_l = max(0, -x0) pad_t = max(0, -y0) pad_r = max(0, x1 - w) pad_b = max(0, y1 - h) cx0, cx1 = max(0, x0), min(w, x1) cy0, cy1 = max(0, y0), min(h, y1) crop_bgr = bgr_sheet[cy0:cy1, cx0:cx1] if crop_bgr.size == 0: return np.zeros((4, CNN_INPUT_SIZE, CNN_INPUT_SIZE), dtype=np.float32) if pad_l or pad_t or pad_r or pad_b: crop_bgr = cv2.copyMakeBorder( crop_bgr, pad_t, pad_b, pad_l, pad_r, cv2.BORDER_REPLICATE ) crop_rgb = cv2.cvtColor(crop_bgr, cv2.COLOR_BGR2RGB) rgb_32 = cv2.resize(crop_rgb, (CNN_INPUT_SIZE, CNN_INPUT_SIZE), interpolation=cv2.INTER_AREA) rgb_norm = rgb_32.astype(np.float32) / 255.0 # (32, 32, 3) if ink_sheet is not None: crop_ink = ink_sheet[cy0:cy1, cx0:cx1] if pad_l or pad_t or pad_r or pad_b: crop_ink = cv2.copyMakeBorder( crop_ink, pad_t, pad_b, pad_l, pad_r, cv2.BORDER_REPLICATE ) else: crop_ink = compute_dark_ink_channel(crop_bgr) ink_32 = cv2.resize(crop_ink, (CNN_INPUT_SIZE, CNN_INPUT_SIZE), interpolation=cv2.INTER_AREA).astype(np.float32) # Estimate local paper brightness from outer border pixels of the 32x32 crop border = np.concatenate([ink_32[0, :], ink_32[-1, :], ink_32[:, 0], ink_32[:, -1]]) local_paper = max(130.0, float(np.percentile(border, 80))) # Ink occlusion map: high where student ink darkens below local paper level occ = np.clip((local_paper - ink_32) / local_paper, 0.0, 1.0) tensor = np.empty((4, CNN_INPUT_SIZE, CNN_INPUT_SIZE), dtype=np.float32) tensor[0] = rgb_norm[:, :, 0] tensor[1] = rgb_norm[:, :, 1] tensor[2] = rgb_norm[:, :, 2] tensor[3] = occ return tensor def extract_4ch_batch_from_sheet( bgr_sheet: np.ndarray, bubbles: Iterable[Tuple[float, float, float]], ) -> np.ndarray: """Extract a (N, 4, 32, 32) float32 tensor batch for all bubbles on a sheet.""" ink_sheet = compute_dark_ink_channel(bgr_sheet) tensors = [ extract_4ch_bubble_tensor(bgr_sheet, cx, cy, rad, ink_sheet=ink_sheet) for cx, cy, rad in bubbles ] if not tensors: return np.empty((0, 4, CNN_INPUT_SIZE, CNN_INPUT_SIZE), dtype=np.float32) return np.stack(tensors, axis=0).astype(np.float32)