2D Micrograph Segmentation Quickstart
This quickstart guide shows you how to use SABER's API to segment 2D micrographs programmatically. You'll learn the core classes and functions needed to process EM images, SEM data, or any 2D microscopy data.
🎯 What You'll Learn
- Load and preprocess micrograph data
- Initialize SAM2-based segmenters
- Apply domain expert classifiers
🚀 Basic Segmentation
Step 1: Load Your Micrograph and Import Modules
Before starting, ensure you have SABER installed and import the necessary modules: SABER supports various file formats commonly used in microscopy:
from saber.segmenters.micro import cryoMicroSegmenter
from saber.classifier.models import common
from saber.visualization import classifier as viz
from saber import io
import numpy as np
import torch
# Load a micrograph file (supports .mrc, .tif, .png, etc.)
image, pixel_size = io.read_micrograph("path/to/your/micrograph.mrc")
print(f"Image shape: {image.shape}, Pixel size: {pixel_size} Å")
Step 2: Initialize the Segmenter and Classifier
The cryoMicroSegmenter
class provides SAM2-based segmentation optimized for cryo-EM data. We can either pool model sizes ranging from small to large, all of which are available through meta. We can also filter segmentations that can be too small with the min_mask_area
input.
# Create a segmenter with SAM2
segmenter = cryoMicroSegmenter(
sam2_cfg="large", # SAM2 model size: tiny, base, large
deviceID=0, # GPU device ID
min_mask_area=50, # Minimum mask area to keep
)
# Optional: If a trained classifier is available
classifier = common.get_predictor(
model_weights="path/to/model.pth",
model_config="path/to/config.yaml"
)
# Create segmenter with classifier
segmenter = cryoMicroSegmenter(
sam2_cfg="large",
classifier=classifier,
target_class=1, # Class ID for your target organelle
min_mask_area=50
)
Step 3: Run Segmentation
Execute the segmentation process with a single function call.
# Segment the image
masks = segmenter.segment(
image0=image,
display_image=True, # Show results
)
print(f"Found {len(masks)} segments")
🔧 Advanced Configuration
Resolution Control
As a pre-processing step, we can Fourier crop (downsample or images) to a resolution that is suitable for the available GPU. In cases where the memory requirement is too large, either reduce the model size of SAM2 or reduce the image resolution to below 2048.
# Downsample to target resolution
from saber.process.downsample import FourierRescale2D
scale = 2
image = FourierRescale2D.run(image, scale)
(Experimental) Sliding Window Segmentation
In cases where high-resolution is essential, we can use a sliding window to segment the images. This guarantees we can use the large base SAM2 model and process the full image resolution. We can vary both the window size (in pixels) and overlap ratio for the sliding window.
# Fine-tune SAM2 behavior
segmenter = cryoMicroSegmenter(
sam2_cfg="large",
min_mask_area=100, # Larger minimum area
window_size=512, # Window Size (Pixels)
overlap_ratio=0.5 # More overlap
)
# Use sliding window for images larger than 1536x1536
masks = segmenter.segment(
image0=large_image,
use_sliding_window=True, # Enable sliding window
display_image=True
)
📚 Next Steps
Now that you've mastered basic 2D segmentation:
- 3D Quickstart - Learn 3D tomogram segmentation
- API Overview - Explore advanced features and customization