Batch Crop Anything is an interactive napari plugin for intelligent image cropping and object extraction. It combines SAM2 (Segment Anything Model 2) for automatic object detection with an intuitive interface for selecting and cropping specific objects from microscopy images.
This plugin enables:
- Interactive object segmentation using AI-powered SAM2 model
- 2D and 3D processing for single images and image stacks
- Multi-frame propagation for temporal datasets
- Batch cropping of selected objects across multiple images
- GPU acceleration (CUDA, Apple Silicon, CPU fallback)
Batch Crop Anything will automatically create a dedicated SAM2 environment when first used.
SAM2 is automatically downloaded and installed in an isolated environment on first use. However, you must manually install ffmpeg:
# Linux (usually pre-installed)
sudo apt-get install ffmpeg
# macOS
brew install ffmpeg
# Or via conda
mamba install -c conda-forge ffmpegIf you already have SAM2 installed (from a previous version), you can use it by setting the SAM2_PATH environment variable:
export SAM2_PATH=/path/to/your/sam2The plugin will automatically detect and use this installation instead of creating a new isolated environment. To switch back to the isolated environment, simply unset the variable:
unset SAM2_PATH- Open napari and navigate to Plugins → napari-tmidas → Batch Crop Anything
- Select your image folder containing
.tifor.zarrfiles - Choose between 2D Mode (single images) or 3D Mode (stacks/time-series)
- Click on objects in the image to segment them with SAM2
- Use the interactive table to select which objects to crop
- Save cropped regions to disk
Perfect for single 2D images or when you want to segment individual layers.
Interactive Workflow:
- Click on image → Creates positive point prompt
- Shift+click → Creates negative point prompt (refine boundaries)
- SAM2 segments the object at that point
- Click existing objects → Select for cropping
Controls:
- Sensitivity slider: Adjust detection confidence (0-100)
- Higher values → More aggressive segmentation
- Lower values → Conservative segmentation
- Next/Previous buttons: Navigate through image collection
For volumetric data (Z-stacks) or time-series datasets (time-lapse videos).
Data Format Recognition: The plugin automatically detects your data format:
- TZYX: Time-series with Z-stacks (e.g., time-lapse confocal)
- TYX: Time-series without Z dimension (e.g., 2D time-lapse)
- ZYX: Single Z-stack without time dimension
Interactive Workflow:
- Navigate to the first slice where your object appears (using the dimension slider)
- Click on the object in 2D view (not 3D view)
- SAM2 segments the object at that frame
- Automatic propagation: Segmentation is propagated through all frames using video tracking
Important: Always click on the first frame containing your object. SAM2's video propagation then extends the segmentation forward through time.
Controls:
- Use dimension sliders to navigate frames/slices
- Sensitivity slider: Control propagation aggressiveness
- Objects persist across frames automatically
SAM2 supports two ways to specify objects:
Click on image to add point prompts. Best for complex boundaries or small objects.
| Action | Effect |
|---|---|
| Click | Add positive point (include this region) |
| Shift+Click | Add negative point (exclude this region) |
Draw rectangles around objects. Best for quick segmentation of simple objects.
- Select Box Mode from the UI
- Draw rectangle around object
- SAM2 segments the region inside the box
- Can add more rectangles for multiple objects
- Shift+draw to refine/subtract from existing box
When to use each:
- Points: Fine details, intricate boundaries, removing noise
- Box: Quick segmentation, well-defined rectangular regions, speed
| Action | Effect |
|---|---|
| Left/Right Click | Navigate to adjacent frames (3D mode) |
| Dimension Slider | Jump to specific frame/slice (3D mode) |
The label table displays all detected objects:
- Checkbox: Select objects to crop
- Object ID: Unique identifier in segmentation
- Area: Size in pixels
- Statistics: Min/max intensity values
Adjust SAM2's detection confidence:
- Range: 0-100
- Default: 50
- Effect on 2D: Higher values segment larger regions
- Effect on 3D: Higher values allow more aggressive frame-to-frame propagation
When you save cropped objects, the plugin creates:
output_folder/
├── image1_object_1.tif
├── image1_object_2.tif
├── image2_object_1.tif
└── ...
Each cropped region is:
- Extracted as a minimal bounding box
- Saved as a separate TIFF file
- Named with original image + object ID
For 3D/4D processing, the plugin converts image stacks to MP4 format:
- Automatic: Conversion happens on first load
- Cached: MP4 files are reused if they exist
- 4D Handling: TZYX data is projected to TYX using maximum intensity projection
Device selection is automatic:
- NVIDIA GPU: CUDA (if available)
- Apple Silicon: MPS (Metal Performance Shaders)
- CPU: Fallback for all systems
Check console output to see which device is active.
If SAM2 initialization fails:
- Images still load without automatic segmentation
- You can still use manual annotation tools
- Check console for detailed error messages
- Verify SAM2_PATH environment variable if needed
Solution: SAM2 will auto-install on first use. If this fails, check console for errors.
Possible causes:
- SAM2 model not initialized (check console)
- Image format incompatible (must be
.tif,.tiff, or.zarr) - GPU out of memory (switch to CPU)
Solutions:
- Check console output for error messages
- Try reducing image size or resolution
- Enable CPU mode:
torch.device('cpu')
Solutions:
- Reduce image dimensions
- Switch to CPU mode
- Close other GPU-intensive applications
- Clear GPU cache:
torch.cuda.empty_cache()
Causes:
- Large 4D volumes
- Limited GPU memory
- Network latency (SAM2 checkpoint download)
Solutions:
- Use 2D mode for individual slices
- Reduce image dimensions
- Pre-process images to smaller regions
| Format | Dimensions | Status |
|---|---|---|
.tif / .tiff |
2D, 3D, 4D | ✓ Fully supported |
.zarr |
2D, 3D, 4D | ✓ Fully supported |
.png |
2D | ✗ Not supported |
.jpg |
2D | ✗ Not supported |
- Pre-process large images: Downscale to < 2 megapixels for interactive use
- Use 2D mode: For single large images, segment individual slices
- GPU selection: CUDA > MPS > CPU (in terms of speed)
- Batch processing: Process multiple small images faster than one large image
- Sensitivity tuning: Start at 50, adjust based on results
confocal_images/
├── sample1.tif (3D Z-stack)
├── sample2.tif (3D Z-stack)
└── ...
→ Use 3D Mode, select appropriate crop regions
timelapse/
├── embryo_t001.tif (2D)
├── embryo_t002.tif (2D)
├── embryo_t003.tif (2D)
└── ...
→ Process each timepoint with 2D Mode, or stack into TYX format for 3D mode
multi_channel/
├── raw_ch1_ch2.tif (4D: TZYX)
├── raw_ch2_ch2.tif (4D: TZYX)
└── ...
→ Use 3D Mode, plugin auto-detects dimensions
- Basic Processing: Image preprocessing and filtering
- Cellpose Segmentation: Alternative segmentation method
- Grid View Overlay: Visualize multiple processed images
- Label Inspection: Interactive label verification and editing
- Model: SAM2.1 Hiera Large
- Input: RGB images (0-1 range)
- Output: Binary mask for each object
- Inference: Single-pass prompting + optional propagation in videos
macOS:
- Check for Apple Silicon (MPS) → Use MPS
- Otherwise → Use CPU
Linux/Windows:
- Check for CUDA → Use CUDA
- Otherwise → Use CPU
If you use Batch Crop Anything in your research, please cite:
@software{napari_tmidas_2024,
title = {napari-tmidas: Batch Image Processing for Microscopy},
author = {Mercader Lab},
year = {2024},
url = {https://github.com/MercaderLabAnatomy/napari-tmidas}
}