Skip to main content
. 2025 Sep 9;11(9):309. doi: 10.3390/jimaging11090309
Algorithm 2. Unsupervised clustering of RBC latent features using k-means
RBC_Clustering_Encoder(samples, encoder_models, num_clusters_list)
  for each sample_id in samples do
    initialize paths for model, images, and outputs
    create output folders if not exist
    load pre-trained encoder model for current sample_id
    load and preprocess RBC images   → x_test
    normalize pixel values (0–1)
    encode images using encoder     → encoded_imgs
    remove existing clustering score CSV if exists
    for each num_clusters in num_clusters_list do
      apply KMeans clustering (num_clusters)
      compute clustering labels     → labels
      calculate Silhouette Score     → sil_score
      calculate Davies–Bouldin Index  → dbi_score
      save scores to CSV log
      create cluster folders and copy images based on labels
      plot silhouette visualization per cluster
      plot metrics comparison (Silhouette vs. DBI)
      save plots
      apply UMAP to reduce encoded_imgs to 2D
      plot and save UMAP scatter plot with cluster coloring
      save plots
    end for
    compile per-sample clustering report summarizing metrics and plots
    append results to global clustering summary (multi-sample CSV)
  end for
  compute total execution time and display summary
  return clustering_results_summary