nerdemma

pyfabric

Sep 25th, 2025
170
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.01 KB | Source Code | 0 0
  1. # repositorios
  2. # opencv-python
  3. # numpy
  4. # scikit-image
  5. # scikit-learn
  6. # pandas
  7. # matplotlib
  8.  
  9. import cv2
  10. import numpy as np
  11. import os
  12. import shutil
  13. from skimage.feature import local_binary_pattern
  14.  
  15. # ---- CONFIGURACIÓN ----
  16. radius = 3
  17. n_points = 8 * radius
  18.  
  19. def calcular_histograma(img_path):
  20.     img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
  21.     if img is None:
  22.         return None
  23.     lbp = local_binary_pattern(img, n_points, radius, method="uniform")
  24.     hist, _ = np.histogram(lbp.ravel(),
  25.                            bins=np.arange(0, n_points + 3),
  26.                            range=(0, n_points + 2))
  27.     hist = hist.astype("float")
  28.     hist /= (hist.sum() + 1e-6)
  29.     return hist
  30.  
  31. def chi2_distance(histA, histB, eps=1e-10):
  32.     return 0.5 * np.sum([((a - b) ** 2) / (a + b + eps)
  33.                          for (a, b) in zip(histA, histB)])
  34.  
  35. def filtrar_texturas(dir_origen, dir_destino, ref_img, umbral=0.3):
  36.     if not os.path.exists(dir_destino):
  37.         os.makedirs(dir_destino)
  38.  
  39.     # Histograma de la imagen de referencia (la tela)
  40.     ref_hist = calcular_histograma(ref_img)
  41.  
  42.     for archivo in os.listdir(dir_origen):
  43.         path_img = os.path.join(dir_origen, archivo)
  44.  
  45.         if not os.path.isfile(path_img):
  46.             continue
  47.  
  48.         hist = calcular_histograma(path_img)
  49.         if hist is None:
  50.             continue
  51.  
  52.         distancia = chi2_distance(ref_hist, hist)
  53.  
  54.         if distancia < umbral:  # mientras más chico, más parecido
  55.             print(f"[OK] {archivo} -> MOVIDO (distancia={distancia:.4f})")
  56.             shutil.move(path_img, os.path.join(dir_destino, archivo))
  57.         else:
  58.             print(f"[NO] {archivo} -> no coincide (distancia={distancia:.4f})")
  59.  
  60. # ---- MAIN ----
  61. if __name__ == "__main__":
  62.     dir_origen = input("📂 Ingrese carpeta ORIGEN: ")
  63.     dir_destino = input("📂 Ingrese carpeta DESTINO: ")
  64.     ref_img = input("📸 Ingrese ruta de la imagen de REFERENCIA (tela): ")
  65.  
  66.     filtrar_texturas(dir_origen, dir_destino, ref_img)
  67.  
Advertisement
Add Comment
Please, Sign In to add comment