Created
December 29, 2025 16:13
-
-
Save me-suzy/2bd29593d704c73fb9f6e064a7964954 to your computer and use it in GitHub Desktop.
678ghjkjh.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import cv2 | |
| import numpy as np | |
| from pathlib import Path | |
| def detect_underline_by_vertical_context(img, low=150, high=220, bg_thresh=220, check_dist=3): | |
| """ | |
| Detectează subliniere bazat pe context vertical. | |
| Un pixel e subliniere dacă: | |
| 1. E în zona gri (low-high) | |
| 2. Pixelii de DEASUPRA sunt fundal (>bg_thresh) | |
| 3. Pixelii de DEDESUBT sunt fundal (>bg_thresh) | |
| Textul NU îndeplinește condițiile 2 și 3 (are alte litere în jur). | |
| """ | |
| h, w = img.shape | |
| # Zona gri | |
| in_gray_zone = (img >= low) & (img < high) | |
| # Media pixelilor de deasupra | |
| above_mean = np.zeros_like(img, dtype=np.float32) | |
| for d in range(1, check_dist + 1): | |
| shifted = np.roll(img, d, axis=0) | |
| shifted[:d, :] = 255 | |
| above_mean += shifted | |
| above_mean /= check_dist | |
| # Media pixelilor de dedesubt | |
| below_mean = np.zeros_like(img, dtype=np.float32) | |
| for d in range(1, check_dist + 1): | |
| shifted = np.roll(img, -d, axis=0) | |
| shifted[-d:, :] = 255 | |
| below_mean += shifted | |
| below_mean /= check_dist | |
| # Condiții | |
| above_is_bg = above_mean > bg_thresh | |
| below_is_bg = below_mean > bg_thresh | |
| # Subliniere = în zona gri + fundal sus + fundal jos | |
| underline_mask = in_gray_zone & above_is_bg & below_is_bg | |
| return underline_mask.astype(np.uint8) * 255 | |
| def generate_variants(image_path, output_dir): | |
| """Generează variante cu detectare bazată pe context vertical""" | |
| img = cv2.imread(str(image_path)) | |
| if img is None: | |
| raise ValueError(f"Nu s-a putut încărca: {image_path}") | |
| p = Path(image_path) | |
| output_dir = Path(output_dir) | |
| output_dir.mkdir(parents=True, exist_ok=True) | |
| # Desaturare completă | |
| hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV) | |
| h, s, v = cv2.split(hsv) | |
| s_zero = np.zeros_like(s) | |
| hsv_desat = cv2.merge([h, s_zero, v]) | |
| img_gray = cv2.cvtColor(hsv_desat, cv2.COLOR_HSV2BGR) | |
| gray = cv2.cvtColor(img_gray, cv2.COLOR_BGR2GRAY) | |
| # Gamma correction | |
| gamma = 3.0 | |
| inv_gamma = 1.0 / gamma | |
| table = np.array([((i / 255.0) ** inv_gamma) * 255 for i in range(256)]).astype("uint8") | |
| gc = cv2.LUT(gray, table) | |
| # Configurații: (nume, low, high, bg_thresh, check_dist) | |
| configs = [ | |
| ("vc1_150_220_bg220_d3", 150, 220, 220, 3), | |
| ("vc2_150_220_bg215_d3", 150, 220, 215, 3), | |
| ("vc3_155_215_bg220_d2", 155, 215, 220, 2), | |
| ("vc4_160_210_bg220_d4", 160, 210, 220, 4), | |
| ("vc5_150_220_bg210_d5", 150, 220, 210, 5), | |
| ("vc6_145_225_bg215_d3", 145, 225, 215, 3), | |
| ] | |
| for name, low, high, bg_thresh, dist in configs: | |
| mask = detect_underline_by_vertical_context(gc, low, high, bg_thresh, dist) | |
| # Aplicăm masca | |
| result = gc.copy() | |
| result[mask > 0] = 245 | |
| # Binarizare | |
| blur = cv2.GaussianBlur(result, (3, 3), 0) | |
| binary = cv2.adaptiveThreshold(blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, | |
| cv2.THRESH_BINARY, 25, 12) | |
| out_path = output_dir / f"{p.stem}_{name}{p.suffix}" | |
| cv2.imwrite(str(out_path), binary) | |
| print(f"Salvat: {out_path} (low={low}, high={high}, bg={bg_thresh}, dist={dist})") | |
| # ========== CONFIGURARE ========== | |
| IMAGE_PATH = r"d:\imagine_noua.jpg" | |
| OUTPUT_DIR = r"d:\output" | |
| # ================================= | |
| if __name__ == "__main__": | |
| print("Eliminare sublinieri - Context Vertical") | |
| print("(Detectează pixeli cu fundal DEASUPRA și DEDESUBT)") | |
| print(f"Input: {IMAGE_PATH}") | |
| print(f"Output: {OUTPUT_DIR}") | |
| print("-" * 60) | |
| generate_variants(IMAGE_PATH, OUTPUT_DIR) | |
| print("-" * 60) | |
| print("Gata!") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment