Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- # repositorios
- # opencv-python
- # numpy
- # scikit-image
- # scikit-learn
- # pandas
- # matplotlib
- import cv2
- import numpy as np
- import os
- import shutil
- from skimage.feature import local_binary_pattern
- # ---- CONFIGURACIÓN ----
- radius = 3
- n_points = 8 * radius
- def calcular_histograma(img_path):
- img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)
- if img is None:
- return None
- lbp = local_binary_pattern(img, n_points, radius, method="uniform")
- hist, _ = np.histogram(lbp.ravel(),
- bins=np.arange(0, n_points + 3),
- range=(0, n_points + 2))
- hist = hist.astype("float")
- hist /= (hist.sum() + 1e-6)
- return hist
- def chi2_distance(histA, histB, eps=1e-10):
- return 0.5 * np.sum([((a - b) ** 2) / (a + b + eps)
- for (a, b) in zip(histA, histB)])
- def filtrar_texturas(dir_origen, dir_destino, ref_img, umbral=0.3):
- if not os.path.exists(dir_destino):
- os.makedirs(dir_destino)
- # Histograma de la imagen de referencia (la tela)
- ref_hist = calcular_histograma(ref_img)
- for archivo in os.listdir(dir_origen):
- path_img = os.path.join(dir_origen, archivo)
- if not os.path.isfile(path_img):
- continue
- hist = calcular_histograma(path_img)
- if hist is None:
- continue
- distancia = chi2_distance(ref_hist, hist)
- if distancia < umbral: # mientras más chico, más parecido
- print(f"[OK] {archivo} -> MOVIDO (distancia={distancia:.4f})")
- shutil.move(path_img, os.path.join(dir_destino, archivo))
- else:
- print(f"[NO] {archivo} -> no coincide (distancia={distancia:.4f})")
- # ---- MAIN ----
- if __name__ == "__main__":
- dir_origen = input("📂 Ingrese carpeta ORIGEN: ")
- dir_destino = input("📂 Ingrese carpeta DESTINO: ")
- ref_img = input("📸 Ingrese ruta de la imagen de REFERENCIA (tela): ")
- filtrar_texturas(dir_origen, dir_destino, ref_img)
Advertisement
Add Comment
Please, Sign In to add comment