A comprehensive Python pipeline for automated cell counting and co-localization analysis in fluorescence microscopy images. This toolkit processes multi-channel confocal microscopy data (.czi files) to quantify cell populations and analyze marker co-expression patterns.
This pipeline consists of two main components:
- Image Preprocessing (
01_image_preprocessing.ipynb
): Extracts individual channels from multi-channel microscopy files and performs automated cell segmentation - Cell Counting Analysis (
02_cell_counting_analysis.ipynb
): Counts cells in each channel and analyzes co-localization patterns between fluorescence markers
- Multi-channel extraction: Splits .czi files into individual channel/Z-plane images
- Automated segmentation: Uses Cellpose (cpsam model) for accurate cell boundary detection
- Parameter optimization: Tools to find optimal segmentation parameters for your dataset
- Batch processing: Handle multiple files efficiently
- Quality control: Progress reporting and error handling
- Multi-marker analysis: Supports 3-channel analysis (GFP, c-fos, NeuN)
- ROI-based counting: Process user-defined regions of interest
- Co-localization detection: Identifies cells expressing multiple markers
- 3D analysis: Handles multiple Z-planes independently
- Export capabilities: Generates CSV reports and ImageJ-compatible ROI files
- Boundary exclusion: Removes edge artifacts for accurate counting
- Python 3.8+
- CUDA-compatible GPU (optional, for faster Cellpose segmentation)
- Sufficient RAM (8GB+ recommended for large images)
- Clone the repository:
git clone https://github.com/yourusername/cell-segmentation-pipeline.git
cd cell-segmentation-pipeline
- Create a virtual environment (recommended):
python -m venv cell_analysis_env
source cell_analysis_env/bin/activate # On Windows: cell_analysis_env\Scripts\activate
- Install dependencies:
pip install -r requirements.txt
- GPU Setup (optional but recommended):
- For CUDA 11.x: Uncomment
cupy-cuda11x>=9.0.0
in requirements.txt - For CUDA 12.x: Uncomment
cupy-cuda12x>=12.0.0
in requirements.txt - Then run:
pip install -r requirements.txt
- For CUDA 11.x: Uncomment
- Open
01_image_preprocessing.ipynb
in Jupyter Lab/Notebook - Update file paths in Cell 1 to point to your .czi files
- Run all cells to:
- Extract individual channels
- Perform automated segmentation
- Generate training data for analysis
Optional: Parameter Optimization
- Uncomment and run Cell 4 to test different Cellpose parameters
- Choose optimal settings based on your cell types and imaging conditions
- Open
02_cell_counting_analysis.ipynb
- Update the sample configuration in Cell 2:
sample_list = [ ("path/to/your/file.czi", "path/to/roi_file.zip", "path/to/cellpose_output/"), # Add more samples as needed ]
- Run all cells to:
- Process all ROIs and Z-planes
- Count cells in each fluorescence channel
- Analyze co-localization patterns
- Generate comprehensive reports
filename_C{channel}_Z{z-plane}.ome.tif
: Individual channel imagesfilename_C{channel}_Z{z-plane}_mask.tiff
: Segmentation masksfilename_C{channel}_Z{z-plane}.zip
: ROI coordinates for detected cells
cell_counting_results.csv
: Comprehensive cell count tablefilename_ROI{X}_final.zip
: Co-localized cell coordinates for ImageJ
cell-segmentation-pipeline/
│
├── 01_image_preprocessing.ipynb # Step 1: Channel extraction & segmentation
├── 02_cell_counting_analysis.ipynb # Step 2: Cell counting & co-localization
├── requirements.txt # Python dependencies
├── README.md # This file
│
└── example_data/ # (Optional) Example datasets
├── sample.czi # Example microscopy file
└── sample_rois.zip # Example ROI file
By default, the pipeline expects:
- Channel 0 (C0): GFP - Specific cell population marker
- Channel 1 (C1): c-fos - Neuronal activation marker
- Channel 3 (C3): NeuN - General neuronal marker
Modify the channel assignments in read_channels_z()
function if your setup differs.
Key Cellpose parameters you can adjust:
diameter
: Expected cell diameter in pixels (None = auto-detect)flow_threshold
: Quality threshold for flow (0.4 default, range 0-1)cellprob_threshold
: Cell probability threshold (0.0 default, range -6 to 6)
threshold
: IoU overlap threshold for co-localization (0.8 default)- Adjust in
count_overlaps()
function calls
-
Prepare your data:
- Multi-channel .czi files from confocal microscopy
- Manually defined ROIs saved as .zip files (using ImageJ/FIJI)
-
Run preprocessing:
jupyter lab 01_image_preprocessing.ipynb
-
Analyze cell counts:
jupyter lab 02_cell_counting_analysis.ipynb
-
Review results:
- Check
cell_counting_results.csv
for quantitative data - Import ROI files into ImageJ for visual validation
- Check
The analysis generates a CSV table with columns:
Z
: Z-plane numberROI
: Region of interest identifierGFP
: Count of GFP+ cellsc-fos
: Count of c-fos+ cellsNeuN
: Count of NeuN+ cellsGFP+ NEUN+
: Count of cells expressing both GFP and NeuNFOS+ NEUN+
: Count of cells expressing both c-fos and NeuNGFP+ FOS+ NEUN+
: Count of cells expressing all three markers
Contributions are welcome! Please feel free to submit a Pull Request. For major changes, please open an issue first to discuss what you would like to change.
This project is licensed under the MIT License - see the LICENSE file for details.
- Built with Cellpose for cell segmentation
- Uses aicspylibczi for .czi file handling
- Inspired by the need for reproducible microscopy analysis workflows
If you encounter any problems or have questions:
- Check the Issues page
- Create a new issue with detailed description of the problem
- Include sample data and error messages when possible
For questions or collaborations, please contact: your.email@institution.edu
This pipeline was developed to standardize and automate fluorescence microscopy analysis workflows, making quantitative cell biology more accessible and reproducible.