Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added comparacion/test_espaciado.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
49 changes: 0 additions & 49 deletions generar.py

This file was deleted.

Binary file removed imagenes_generadas/texto_arco_abajo.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_arco_arriba.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_arco_rotacion.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_arco_ruido.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_base.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_onda_compleja.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_onda_moderada.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_onda_rapida.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_onda_ruido_bajo.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_onda_ruido_medio.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_onda_suave.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_perspectiva_fuerte.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_perspectiva_suave.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_rectificado2.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_rectificado5.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_rotacion_fuerte.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_rotacion_moderada.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_rotacion_suave.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_sintetico.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_sintetico2.png
Binary file not shown.
Binary file removed imagenes_generadas/texto_sintetico3.png
Binary file not shown.
Binary file added images/synth_texto_arco_abajo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_arco_arriba.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_arco_rotacion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_base.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_curva_perspectiva.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_onda_compleja.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_onda_moderada.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_onda_rapida.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_onda_rotacion.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_onda_suave.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_perspectiva_fuerte.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_perspectiva_suave.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_rotacion_fuerte.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_rotacion_moderada.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/synth_texto_rotacion_suave.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions src/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
"""
Paquete principal del proyecto de rectificación de texto.
"""
Binary file added src/__pycache__/__init__.cpython-313.pyc
Binary file not shown.
5 changes: 5 additions & 0 deletions src/text_generation/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
"""
Módulo para generación de imágenes sintéticas de texto deformado.
"""

from .generar import *
95 changes: 95 additions & 0 deletions src/text_generation/generar.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import cv2 as cv
import numpy as np

# ================================
# CONFIGURACIÓN
# ================================
ANCHO = 1200
ALTO = 400
TEXTO = "TEXT PROOFING"
FONT = cv.FONT_HERSHEY_SIMPLEX
FONT_SCALE = 3
COLOR = (0, 0, 0) # Negro
GROSOR = 8

# Parámetros de deformación
ANGULO = 15 # grados de inclinación global (0 = sin inclinación)
CURVATURA = 0.00008 # Factor de curvatura (0 = sin curvatura, valores pequeños como 0.0001)
AMPLITUD_ONDA = 0 # Amplitud de ondulación vertical en píxeles (0 = sin ondulación)
FRECUENCIA_ONDA = 0.01 # Frecuencia de la ondulación

NOMBRE_SALIDA = "./images/texto_sintetico_curved.png"

# ================================
# 1) Crear imagen grande para evitar recortes
# ================================
margen = 200
ancho_aux = ANCHO + 2 * margen
alto_aux = ALTO + 2 * margen

# ================================
# 2) Renderizar texto en alta resolución (sin deformación)
# ================================
aux = np.full((alto_aux, ancho_aux, 3), 255, dtype=np.uint8)

(tw, th), baseline = cv.getTextSize(TEXTO, FONT, FONT_SCALE, GROSOR)
x = (ancho_aux - tw) // 2
y = (alto_aux + th) // 2

cv.putText(aux, TEXTO, (x, y), FONT, FONT_SCALE, COLOR, GROSOR, cv.LINE_AA)

# ================================
# 3) Aplicar transformación de curvatura e inclinación
# ================================
img_final = np.full((ALTO, ANCHO, 3), 255, dtype=np.uint8)

# Centro de la imagen para la curvatura
cx = ancho_aux / 2
cy = alto_aux / 2

# Crear mapas de coordenadas para remapeo
map_x = np.zeros((ALTO, ANCHO), dtype=np.float32)
map_y = np.zeros((ALTO, ANCHO), dtype=np.float32)

for i in range(ALTO):
for j in range(ANCHO):
# Coordenadas en la imagen de salida
x_dst = j
y_dst = i

# Aplicar offset para centrar
x_src = x_dst + margen
y_src = y_dst + margen

# Aplicar curvatura (efecto de perspectiva curva)
if CURVATURA != 0:
dx = x_src - cx
dy = y_src - cy
# Curvatura cuadrática horizontal
x_src += CURVATURA * dx * dx
# Pequeña curvatura vertical para efecto realista
y_src += CURVATURA * 0.3 * dx * dx

# Aplicar ondulación vertical (opcional)
if AMPLITUD_ONDA != 0:
y_src += AMPLITUD_ONDA * np.sin(FRECUENCIA_ONDA * x_src)

# Aplicar inclinación (shear)
if ANGULO != 0:
angulo_rad = np.radians(ANGULO)
shear = np.tan(angulo_rad)
y_src += shear * (x_src - cx)

map_x[i, j] = x_src
map_y[i, j] = y_src

# Aplicar el remapeo con interpolación de alta calidad
img_final = cv.remap(aux, map_x, map_y, cv.INTER_CUBIC,
borderMode=cv.BORDER_CONSTANT, borderValue=(255, 255, 255))

# ================================
# 4) Guardar resultado
# ================================
cv.imwrite(NOMBRE_SALIDA, img_final)
print(f"Imagen sintética generada: {NOMBRE_SALIDA}")
print(f"Parámetros: Ángulo={ANGULO}°, Curvatura={CURVATURA}, Ondulación={AMPLITUD_ONDA}px")
202 changes: 202 additions & 0 deletions src/text_generation/generar_batch.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,202 @@
import cv2 as cv
import numpy as np
import os

# ================================
# CONFIGURACIÓN BASE
# ================================
ANCHO = 1200
ALTO = 400
TEXTO = "TEXT PROOFING"
FONT = cv.FONT_HERSHEY_SIMPLEX
FONT_SCALE = 3
COLOR = (0, 0, 0) # Negro
GROSOR = 8
OUTPUT_DIR = "./images"

# Crear directorio si no existe
os.makedirs(OUTPUT_DIR, exist_ok=True)


def generar_imagen_sintetica(nombre, angulo=0, curvatura=0, amplitud_onda=0, frecuencia_onda=0.01,
curvatura_vertical=False, perspectiva=0):
"""
Genera una imagen sintética con texto deformado
Parámetros:
nombre: nombre del archivo de salida
angulo: inclinación en grados
curvatura: factor de curvatura horizontal (valores pequeños como 0.0001)
amplitud_onda: amplitud de ondulación en píxeles
frecuencia_onda: frecuencia de la ondulación
curvatura_vertical: si True, aplica curvatura vertical (arco)
perspectiva: factor de perspectiva (efecto de profundidad)
"""
# ================================
# 1) Crear imagen grande para evitar recortes
# ================================
margen = 200
ancho_aux = ANCHO + 2 * margen
alto_aux = ALTO + 2 * margen

# ================================
# 2) Renderizar texto en alta resolución (sin deformación)
# ================================
aux = np.full((alto_aux, ancho_aux, 3), 255, dtype=np.uint8)

(tw, th), baseline = cv.getTextSize(TEXTO, FONT, FONT_SCALE, GROSOR)
x = (ancho_aux - tw) // 2
y = (alto_aux + th) // 2

cv.putText(aux, TEXTO, (x, y), FONT, FONT_SCALE, COLOR, GROSOR, cv.LINE_AA)

# ================================
# 3) Aplicar transformación
# ================================
img_final = np.full((ALTO, ANCHO, 3), 255, dtype=np.uint8)

# Centro de la imagen
cx = ancho_aux / 2
cy = alto_aux / 2

# Crear mapas de coordenadas para remapeo
map_x = np.zeros((ALTO, ANCHO), dtype=np.float32)
map_y = np.zeros((ALTO, ANCHO), dtype=np.float32)

for i in range(ALTO):
for j in range(ANCHO):
# Coordenadas en la imagen de salida
x_dst = j
y_dst = i

# Aplicar offset para centrar
x_src = x_dst + margen
y_src = y_dst + margen

# Normalizar coordenadas para efectos [-1, 1]
x_norm = (x_src - cx) / cx
y_norm = (y_src - cy) / cy

# Aplicar curvatura horizontal (arco)
if curvatura != 0:
dx = x_src - cx
x_src += curvatura * dx * dx
if not curvatura_vertical:
# Pequeña curvatura vertical para efecto realista
y_src += curvatura * 0.3 * dx * dx

# Aplicar curvatura vertical (arco arriba/abajo)
if curvatura_vertical and curvatura != 0:
dy = y_src - cy
y_src += curvatura * x_norm * x_norm * 50000

# Aplicar ondulación vertical
if amplitud_onda != 0:
y_src += amplitud_onda * np.sin(frecuencia_onda * x_src)

# Aplicar perspectiva
if perspectiva != 0:
# Efecto de profundidad
scale = 1.0 + perspectiva * x_norm
y_offset = (y_src - cy) * scale
y_src = cy + y_offset

# Aplicar inclinación (shear)
if angulo != 0:
angulo_rad = np.radians(angulo)
shear = np.tan(angulo_rad)
y_src += shear * (x_src - cx)

map_x[i, j] = x_src
map_y[i, j] = y_src

# Aplicar el remapeo con interpolación de alta calidad
img_final = cv.remap(aux, map_x, map_y, cv.INTER_CUBIC,
borderMode=cv.BORDER_CONSTANT, borderValue=(255, 255, 255))

# ================================
# 4) Guardar resultado
# ================================
ruta_salida = os.path.join(OUTPUT_DIR, nombre)
cv.imwrite(ruta_salida, img_final)
print(f"✓ Generada: {nombre}")


# ================================
# GENERAR TODAS LAS CASUÍSTICAS
# ================================

print("Generando imágenes sintéticas...")
print("=" * 50)

# 1. Texto base (sin deformación)
generar_imagen_sintetica("synth_texto_base.png")

# 2. Arco hacia arriba (curvatura negativa vertical)
generar_imagen_sintetica("synth_texto_arco_arriba.png",
curvatura=-0.000015,
curvatura_vertical=True)

# 3. Arco hacia abajo (curvatura positiva vertical)
generar_imagen_sintetica("synth_texto_arco_abajo.png",
curvatura=0.000015,
curvatura_vertical=True)

# 4. Arco con rotación
generar_imagen_sintetica("synth_texto_arco_rotacion.png",
angulo=20,
curvatura=0.00005)

# 5. Onda suave
generar_imagen_sintetica("synth_texto_onda_suave.png",
amplitud_onda=15,
frecuencia_onda=0.008)

# 6. Onda moderada
generar_imagen_sintetica("synth_texto_onda_moderada.png",
amplitud_onda=25,
frecuencia_onda=0.012)

# 7. Onda rápida
generar_imagen_sintetica("synth_texto_onda_rapida.png",
amplitud_onda=20,
frecuencia_onda=0.025)

# 8. Onda compleja (múltiples frecuencias simuladas con amplitud variable)
generar_imagen_sintetica("synth_texto_onda_compleja.png",
amplitud_onda=30,
frecuencia_onda=0.015)

# 9. Perspectiva suave
generar_imagen_sintetica("synth_texto_perspectiva_suave.png",
perspectiva=0.15)

# 10. Perspectiva fuerte
generar_imagen_sintetica("synth_texto_perspectiva_fuerte.png",
perspectiva=0.35)

# 11. Rotación suave
generar_imagen_sintetica("synth_texto_rotacion_suave.png",
angulo=8)

# 12. Rotación moderada
generar_imagen_sintetica("synth_texto_rotacion_moderada.png",
angulo=15)

# 13. Rotación fuerte
generar_imagen_sintetica("synth_texto_rotacion_fuerte.png",
angulo=25)

# 14. Combinación: onda + rotación
generar_imagen_sintetica("synth_texto_onda_rotacion.png",
angulo=12,
amplitud_onda=18,
frecuencia_onda=0.01)

# 15. Combinación: curvatura + perspectiva
generar_imagen_sintetica("synth_texto_curva_perspectiva.png",
curvatura=0.00008,
perspectiva=0.2)

print("=" * 50)
print(f"¡Completado! Se generaron 15 imágenes sintéticas en '{OUTPUT_DIR}/'")
Loading