Created
December 28, 2025 10:57
-
-
Save me-suzy/89b39a85dc6a95662f36c9eecb3a224f to your computer and use it in GitHub Desktop.
57uh.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 remove_thin_horizontal_lines(binary_img, line_thickness=5, kernel_length=40): | |
| """ | |
| Elimină liniile orizontale subțiri din imaginea binarizată. | |
| """ | |
| inv = cv2.bitwise_not(binary_img) | |
| # Detectăm liniile orizontale | |
| h_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (kernel_length, 1)) | |
| horizontal_lines = cv2.morphologyEx(inv, cv2.MORPH_OPEN, h_kernel) | |
| # Detectăm și linii ușor înclinate | |
| for angle in [-8, -5, -3, 3, 5, 8]: | |
| length = kernel_length | |
| rad = np.deg2rad(angle) | |
| dx = int(length * np.cos(rad)) | |
| dy = int(length * np.sin(rad)) | |
| kernel_size = max(abs(dx), abs(dy)) + 1 | |
| kernel = np.zeros((kernel_size * 2 + 1, kernel_size * 2 + 1), dtype=np.uint8) | |
| cv2.line(kernel, (kernel_size - dx, kernel_size - dy), | |
| (kernel_size + dx, kernel_size + dy), 1, 1) | |
| lines_angle = cv2.morphologyEx(inv, cv2.MORPH_OPEN, kernel) | |
| horizontal_lines = cv2.bitwise_or(horizontal_lines, lines_angle) | |
| # Dilatăm pentru a acoperi complet liniile | |
| dilate_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, line_thickness)) | |
| horizontal_lines = cv2.dilate(horizontal_lines, dilate_kernel, iterations=1) | |
| # Eliminăm liniile (le facem albe) | |
| result = binary_img.copy() | |
| result[horizontal_lines > 0] = 255 | |
| return result | |
| def process_image_with_settings(img, gamma, line_thickness, kernel_length): | |
| """ | |
| Procesează imaginea cu setările specificate. | |
| """ | |
| # Pasul 1: 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) | |
| # Pasul 2: Gamma correction | |
| inv_gamma = 1.0 / gamma | |
| table = np.array([((i / 255.0) ** inv_gamma) * 255 for i in range(256)]).astype("uint8") | |
| gamma_corrected = cv2.LUT(gray, table) | |
| # Pasul 3: Binarizare adaptivă | |
| gray_blur = cv2.GaussianBlur(gamma_corrected, (3, 3), 0) | |
| binary = cv2.adaptiveThreshold(gray_blur, 255, | |
| cv2.ADAPTIVE_THRESH_GAUSSIAN_C, | |
| cv2.THRESH_BINARY, 25, 12) | |
| # Pasul 4: Eliminare linii orizontale subțiri | |
| result = remove_thin_horizontal_lines(binary, line_thickness, kernel_length) | |
| return result | |
| def generate_variants(image_path, output_dir): | |
| """ | |
| Generează 6 variante cu diferite setări pentru testare. | |
| """ | |
| 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) | |
| # Configurații de testat: (nume, gamma, line_thickness, kernel_length) | |
| configs = [ | |
| ("g3_t4_k40", 3.0, 4, 40), | |
| ("g3_t5_k40", 3.0, 5, 40), | |
| ("g3_t6_k40", 3.0, 6, 40), | |
| ("g3_t5_k30", 3.0, 5, 30), | |
| ("g35_t5_k40", 3.5, 5, 40), | |
| ("g35_t6_k30", 3.5, 6, 30), | |
| ] | |
| for name, gamma, thickness, kernel in configs: | |
| result = process_image_with_settings(img, gamma, thickness, kernel) | |
| out_path = output_dir / f"{p.stem}_{name}{p.suffix}" | |
| cv2.imwrite(str(out_path), result) | |
| print(f"Salvat: {out_path} (gamma={gamma}, thick={thickness}, kernel={kernel})") | |
| # ========== CONFIGURARE ========== | |
| IMAGE_PATH = r"d:\imagine_noua.jpg" | |
| OUTPUT_DIR = r"d:\output" | |
| # ================================= | |
| if __name__ == "__main__": | |
| print(f"Procesez: {IMAGE_PATH}") | |
| print(f"Output în: {OUTPUT_DIR}") | |
| print("-" * 50) | |
| generate_variants(IMAGE_PATH, OUTPUT_DIR) | |
| print("-" * 50) | |
| print("Gata! Verifică cele 6 variante și alege cea mai bună.") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment