Automated detection, tracking, and quantitative analysis of sperm motility using motion-aware algorithms
Click here to see β‘οΈ Detailed parameter description β¬ οΈ
This pipeline is recommended for Linux (including WSL), and is also supported on Windows and macOS, provided Python β₯ 3.12 is available.
You may use any terminal:
- Windows: PowerShell
- Linux: Terminal
- macOS: Terminal
Ensure Python 3 is installed, then install uv, which is used for environment management and execution (this step is required only once):
pip install uvClone the repository and set up the environment:
# Change to your preferred software directory
cd /home/elemeng/software
# Clone the repository
git clone https://github.com/elemeng/simple-sperm-analyzer
cd simple-sperm-analyzer
# Install dependencies
uv sync
# Activate the virtual environment
source .venv/bin/activate # On Windows: .venv\Scripts\activateβ The pipeline is now ready to run.
# Show help message
uv run main.py --help- Parameters can be provided via command-line flags or a JSON configuration file
- Using a JSON file is strongly recommended for batch processing and reproducibility
π See Detailed parameter description
Ensure movies contain white sperm heads on a black background.
Recommended preprocessing steps:
- Invert (if needed):
Edit β Invert - Background subtraction:
Process β Subtract Background - Contrast enhancement:
Image β Adjust β Brightness/ContrastTune to emphasize sperm heads while suppressing background noise.
uv run main.py example/movies \
-o example/results \
--params-file example/zebrafish_sperm_params.json \
--viz-dir example/movies \
--cut-input-suffix="-enhanced" \
--input-glob "*-enhanced.avi" \
--viz-glob "Stream-?.avi" \
--recursiveexample/results/
dmso_60min/
βββ Stream-2-enhanced/
βββ pipeline.log
βββ Stream-2-enhanced_ana_motility.csv
βββ Stream-2-enhanced_ana_report.txt
βββ Stream-2-enhanced_det_coords.csv
βββ Stream-2-enhanced_det_overlay.avi
βββ Stream-2-enhanced_pipeline_summary.json
βββ Stream-2-enhanced_trk_overlay.avi
βββ Stream-2-enhanced_trk_overview.png
βββ Stream-2-enhanced_trk_tracks.csv
-
π₯ Visualization example:
example/results/dmso_60min/Stream-2-enhanced/Stream-2-enhanced_trk_overlay.avi
uv run main.py [OPTIONS] input_pathIn most cases, you do not need to tune all parameters.
If detection is wrong, everything downstream fails.
Tune in the following order:
-
--det-thresholdControls foreground/background separation.- Too low β noise
- Too high β missed sperm
-
--det-min-area,--det-max-areaMatch sperm head size in pixels. -
--det-min-solidityRemoves irregular debris and merged objects. -
--det-min-aspect,--det-max-aspectSpecies-dependent (round vs. elongated heads).
π― Goal: Every visible sperm head is detected once per frame.
After detection is stable, tune motion constraints.
Tune in this order:
-
--trk-max-distanceMaximum movement allowed between frames (pixels). -
--trk-angle-hard-cutPrevents non-biological sharp turns. -
--trk-max-ageAllows short occlusions without breaking tracks.
π― Goal: Tracks follow sperm smoothly without jumping.
These parameters refine which detection belongs to which track.
Tune only if you observe:
- ID switches
- Track hopping
- Broken trajectories
Key parameters:
--trk-weight-direction(increase first)--trk-weight-distance--trk-sigma-angle(smaller = stricter)--trk-sigma-distance
Tune only if:
- You care about immotile sperm, or
- Motile tracking is confused by static objects
Tune in this order:
-
--imm-search-radiusTolerance for drift. -
--imm-max-stdSeparates true immotile from slow movers. -
--imm-min-pointsPrevents false immotile tracks.
Tracking must already be correct.
Critical parameters:
--pixel-sizeβ οΈ (most important)--fps- Motility thresholds (
--ana-motility-*)
Tune based on:
- Species
- Temperature
- Experimental protocol
π§ Rule of Thumb
Fix detection first β then tracking geometry β then matching behavior β then analysis.
- Decrease
--det-threshold(try5β15) - Adjust
--det-min-area/--det-max-areafor magnification - Increase
--det-blur-radiusfor noisy images
- Increase
--trk-weight-direction(try0.5β0.7) - Decrease
--trk-sigma-angle(try30β40Β°) - Use
--trk-assignment-mode hungarianfor global optimization
- Decrease
--trk-min-hits(minimum2) - Increase
--trk-max-age(try7β10) - Decrease
--trk-min-track-length
-
AlquΓ©zar-Baeta, C., Gimeno-Martos, S., Miguel-JimΓ©nez, S., Santolaria, P., YΓ‘niz, J., PalacΓn, I., Casao, A., CebriΓ‘n-PΓ©rez, J.Γ., MuiΓ±o-Blanco, T., and PΓ©rez-PΓ©, R. (2019). OpenCASA: A new open-source and scalable tool for sperm quality analysis. Plos Comput. Biol. 15, e1006691. https://doi.org/10.1371/journal.pcbi.1006691.
-
Yamaguchi, H., Morikawa, M., and Kikkawa, M. (2023). Calaxin stabilizes the docking of outer arm dyneins onto ciliary doublet microtubule in vertebrates. eLife 12, e84860. https://doi.org/10.7554/eLife.84860.
-
Xin Zhang, Jiang Sun, Yonggang Lu, Jintao Zhang, Keisuke Shimada, Taichi Noda, Shuqin Zhao, Takayuki Koyano, Makoto Matsuyama, Shushu Zhou, et al. (2021). LRRC23 is a conserved component of the radial spoke that is necessary for sperm motility and male fertility in mice. Journal of Cell Science 134, jcs259381. https://doi.org/10.1242/jcs.259381.
Issues, feature requests, and pull requests are welcome.
This project is licensed under the MIT License. See the LICENSE file for details.

