Tree Crown Delineation with detectree2¶
This notebook demonstrates how to use the detectree2 module for automatic tree crown delineation in aerial RGB imagery using Mask R-CNN.
Overview¶
detectree2 is a Python package for automatic tree crown delineation based on the Detectron2 implementation of Mask R-CNN. It has been designed to delineate trees in challenging dense tropical forests and has been validated across various forest types.
Reference: Ball, J.G.C., et al. (2023). Accurate delineation of individual tree crowns in tropical forests from aerial RGB imagery using Mask R-CNN. Remote Sens Ecol Conserv. 9(5):641-655. https://doi.org/10.1002/rse2.332
Installation¶
First, install the required packages. detectree2 requires PyTorch and Detectron2 to be installed first.
1 2 3 4 5 6 7 8 9 10 11 | |
Import Libraries¶
1 2 3 4 5 | |
List Available Pre-trained Models¶
detectree2 provides several pre-trained models from different forest types:
1 2 3 | |
Download Sample Image¶
Download a sample tree image for testing:
1 2 3 4 | |
Visualize the Sample Image¶
1 2 3 | |
Initialize the Tree Crown Delineator¶
Initialize the TreeCrownDelineator with a pre-trained model. The model will be automatically downloaded on first use.
1 2 3 4 5 | |
Predict Tree Crowns¶
Run the prediction on the sample image:
1 2 3 4 5 6 7 8 9 10 11 12 | |
Examine the Results¶
1 2 | |
Visualize Results on Map¶
Display the detected tree crowns overlaid on the original image:
1 2 3 4 5 6 7 8 | |
Using a Custom Model¶
If you have trained your own model, you can use it instead of the pre-trained models:
1 2 3 4 5 | |
Tiling Orthomosaics¶
For large orthomosaics, you may want to tile them first before running predictions:
1 2 3 4 5 6 7 8 9 | |
Preparing Training Data¶
If you want to train a custom model, you'll need to prepare your training data with manually delineated crown polygons:
1 2 3 4 5 6 7 8 9 10 11 | |
Stitching Predictions¶
After running predictions on tiles, you can stitch them together:
1 2 3 4 5 6 7 8 | |
Tips for Best Results¶
-
Image Resolution: detectree2 works best with high-resolution imagery (10cm or better).
-
Tile Size: The default tile size of 40x40 meters works well for most applications. Adjust based on your tree sizes.
-
Buffer Size: A buffer of 30 meters helps handle trees at tile edges.
-
Confidence Threshold: Start with 0.5 and adjust based on your precision/recall needs.
-
Training Data: If training custom models, ensure your manual crowns are:
- Densely clustered (not scattered)
- Comprehensively labeled (no missing trees in labeled areas)
-
Accurately delineated
-
GPU: For faster predictions, use a CUDA-enabled GPU.