Author: Mohamad Dweik, Date: 25/02/2026
This project implements two different approaches for the semantic segmentation of dendrites in Scanning Electron Microscope (SEM) images: a Classic Computer Vision pipeline and a Deep Learning approach using YOLO.
Install the required dependencies using pip:
pip install -r requirements.txt
This deterministic pipeline processes images using classic techniques (Pre-processing -> Sauvola -> Morphology -> Skeletonization).
Open SEMDendriteSegmenter.py and scroll to the bottom of the file to choose how you want to run it:
Option A: Single Image Analysis (Debugging Mode) To see the step-by-step transformation of a single image:
- Uncomment the lines loading a single image and calling
segmenter.run_stages(img)andsegmenter.show_stages(...). - Tip: Inside the
run_stages()function, most of the intermediate steps are commented out by default to save memory. Just uncomment the specificstages["name"] = ...lines you want to visualize. - Run the script:
python classic_cv.py
Option B: Batch Folder Analysis To process an entire folder and display a grid of the final results:
- Comment out the single image lines at the bottom of the file.
- Uncomment the
segmenter.plot_all_overlays(...)line and make sure thefolder_pathpoints to your target images. - Run the script:
python SEMDendriteSegmenter.py
To improve segmentation of thin structures, the training images are split into overlapping tiles. Run this to generate the tiled dataset:
python make_tiled_dataset.py
To train the YOLO segmentation model (yolo26n-seg.pt):
python train_yolo_dendrite_seg.py
Results and the best model weights (best.pt) will be saved automatically in the runs/segment/ directory.
For high-resolution images, we use a tiled inference approach with a voting mechanism to merge the masks.
Important: Before running, open predict_tile.py and update the following variables with your local paths:
weights: Path to your trained.ptmodel.source_folder: Path to the folder containing the images you want to predict on.out_dir: Path where you want the masks and overlays saved.
Once updated, run:
python predict_tile.py
If your images are smaller and don't require tiling, use the standard prediction script. Like the tiled script, make sure to update your model and image paths inside the file first:
python predict.py
If you prefer using the Ultralytics CLI directly on a single image:
yolo segment predict model="runs/segment/yolo26_dendrite_tiled_final/weights/best.pt" source="path/to/new_image.png" imgsz=896 conf=0.15 iou=0.80 device=cpu save=True