Skip to content

Instantly share code, notes, and snippets.

@me-suzy
Created December 29, 2025 11:09
Show Gist options
  • Select an option

  • Save me-suzy/8bddd3436e9d4b48e3aefe45b3eac46c to your computer and use it in GitHub Desktop.

Select an option

Save me-suzy/8bddd3436e9d4b48e3aefe45b3eac46c to your computer and use it in GitHub Desktop.
890-99.py
import cv2
import numpy as np
from pathlib import Path
def process_v1_frequency_zones(img_gray, low=150, high=220):
"""Elimină pixelii din zonele cu frecvență mică"""
result = img_gray.copy()
mask = (img_gray >= low) & (img_gray < high)
result[mask] = 245
return result
def process_v2_local_median(img_gray, low=150, high=220, kernel_size=5):
"""Înlocuiește cu mediana locală doar pixelii suspecți"""
result = img_gray.copy()
mask = (img_gray >= low) & (img_gray < high)
local_median = cv2.medianBlur(img_gray, kernel_size)
replace_mask = mask & (local_median > img_gray + 10)
result[replace_mask] = local_median[replace_mask]
return result
def process_v3_horizontal_context(img_gray, low=150, high=220):
"""Elimină doar dacă formează linie orizontală"""
result = img_gray.copy()
suspect = (img_gray >= low) & (img_gray < high)
h_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (20, 1))
horizontal_suspect = cv2.morphologyEx(suspect.astype(np.uint8) * 255, cv2.MORPH_OPEN, h_kernel)
dilate = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
horizontal_suspect = cv2.dilate(horizontal_suspect, dilate, iterations=1)
result[horizontal_suspect > 0] = 245
return result
def process_v4_quartile_based(img_gray):
"""Folosim quartilele pentru a defini zonele"""
q1 = np.percentile(img_gray, 25)
q3 = np.percentile(img_gray, 75)
result = img_gray.copy()
low_bound = max(q1, 140)
high_bound = min(q3, 230)
suspect = (img_gray >= low_bound) & (img_gray < high_bound)
h_kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (25, 1))
horizontal = cv2.morphologyEx(suspect.astype(np.uint8) * 255, cv2.MORPH_OPEN, h_kernel)
result[horizontal > 0] = 245
return result
def process_v5_adaptive_threshold(img_gray):
"""Două praguri adaptive - păstrăm textul, eliminăm subliniere"""
_, binary_text = cv2.threshold(img_gray, 0, 255, cv2.THRESH_BINARY + cv2.THRESH_OTSU)
adaptive = cv2.adaptiveThreshold(img_gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 25, 12)
result = adaptive.copy()
text_mask = binary_text == 0
result[text_mask] = 0
return result
def process_v6_horizontal_narrow(img_gray, low=160, high=210):
"""Linii orizontale cu interval mai strâns"""
return process_v3_horizontal_context(img_gray, low, high)
def generate_variants(image_path, output_dir):
"""Generează 6 variante folosind abordare statistică"""
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")
gamma_corrected = cv2.LUT(gray, table)
# Configurații
configs = [
("v1_freq_150_220", lambda g: process_v1_frequency_zones(g, 150, 220)),
("v2_local_med_150_220", lambda g: process_v2_local_median(g, 150, 220)),
("v3_horiz_150_220", lambda g: process_v3_horizontal_context(g, 150, 220)),
("v4_quartile", lambda g: process_v4_quartile_based(g)),
("v5_dual_thresh", lambda g: process_v5_adaptive_threshold(g)),
("v6_horiz_160_210", lambda g: process_v6_horizontal_narrow(g, 160, 210)),
]
for name, func in configs:
processed = func(gamma_corrected)
# Binarizare finală
blur = cv2.GaussianBlur(processed, (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}")
# ========== CONFIGURARE ==========
IMAGE_PATH = r"d:\imagine_noua.jpg"
OUTPUT_DIR = r"d:\output"
# =================================
if __name__ == "__main__":
print("Eliminare sublinieri - Abordare Statistică")
print(f"Input: {IMAGE_PATH}")
print(f"Output: {OUTPUT_DIR}")
print("-" * 50)
generate_variants(IMAGE_PATH, OUTPUT_DIR)
print("-" * 50)
print("Gata!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment