Computer vision (detection, segmentation)
How object detection and image segmentation models locate and outline objects in images, with the main architectures, metrics such as IoU, mAP and Dice, and steps for building a reliable pipeline.
What it is
Object detection and image segmentation are computer-vision tasks that go beyond asking “what is in this image?” to ask “where is it, and what exactly does it cover?” Object detection outputs a bounding box and a class label for each object of interest, for example cells in a micrograph, cracks in a pavement or vehicles in satellite imagery. Semantic segmentation assigns a class to every pixel (tumor versus healthy tissue, water versus land), instance segmentation additionally separates individual objects of the same class (each cell outlined separately), and panoptic segmentation combines both. These methods are built on deep convolutional and transformer networks (see CNNs and transformers) and are used in medical imaging, microscopy, remote sensing, manufacturing inspection, robotics, autonomous vehicles and agriculture.
It answers questions such as: How many cells or particles are in this field of view, and how large is each? Where is the tumor boundary? Which pixels are crop and which are weeds? Where are the defects on this surface? What is the area of a flooded region? The result is spatial and quantitative, so errors in boundaries translate directly into errors in measurements.
Fundamentals
Detection architectures and post-processing
Two-stage detectors (R-CNN, Fast R-CNN, Faster R-CNN) first propose candidate regions (with a region proposal network) and then classify and refine each; they are accurate, especially for small objects. One-stage detectors (YOLO, SSD, RetinaNet) predict boxes and classes in a single pass over a grid or set of anchors, and are fast enough for real-time video; RetinaNet’s focal loss down-weights the many easy background examples. Transformer-based detectors (DETR and successors) treat detection as set prediction without anchors. Many boxes can describe the same object, so non-maximum suppression (NMS) keeps the highest-scoring box and removes others whose overlap with it exceeds a threshold. Overlap is measured by intersection over union, IoU = |A ∩ B|/|A ∪ B|, which equals 1 for a perfect match and 0 for none; a detection typically counts as correct if its IoU with the ground-truth box is at least 0.5.
Segmentation architectures
Fully convolutional networks output a label map the size of the image. The U-Net (Ronneberger et al., 2015), designed for biomedical images, uses an encoder that downsamples to capture context, a decoder that upsamples back to full resolution, and skip connections that pass fine detail from encoder to decoder; it works well with small training sets and strong augmentation, and remains the standard in microscopy and medical imaging, with nnU-Net automating its configuration. DeepLab uses atrous (dilated) convolutions to enlarge the field of view without losing resolution. Mask R-CNN adds a mask branch to Faster R-CNN for instance segmentation. Cell-specific tools (Cellpose, StarDist) separate touching cells. Foundation models such as the Segment Anything Model (SAM) segment objects from prompts like a click or a box without task-specific training, which speeds annotation, although accuracy on specialized scientific images should be verified, and fine-tuned variants exist for medical and microscopy data.
Metrics and losses
Detection is evaluated with precision (fraction of detections that are correct) and recall (fraction of true objects found) over confidence thresholds. The average precision (AP) is the area under the precision–recall curve for a class, and mAP averages over classes; the COCO benchmark reports mAP averaged over IoU thresholds from 0.5 to 0.95. Segmentation uses IoU (Jaccard index) and the Dice coefficient, Dice = 2|A ∩ B|/(|A| + |B|), averaged over classes, plus boundary-based measures (Hausdorff distance) when contours matter. Training losses include pixel-wise cross-entropy, Dice loss (which handles class imbalance, such as small lesions in large images), focal loss and combinations. Quantities derived from segmentations, such as areas, counts and sizes, should be validated against manual measurements, since pixel metrics may hide systematic bias in size.
Data, annotation and training
Quality labels decide the outcome. Boxes and polygons or masks are drawn in annotation tools (CVAT, Label Studio, labelme) and stored in standard formats (COCO JSON, Pascal VOC, YOLO text files, mask images). Pixel-level annotation is slow, so approaches that reduce effort include pre-labeling with a model or SAM and correcting, active learning (see active learning), and weak or semi-supervised learning. Multiple annotators and an agreement measure (inter-rater Dice) show how consistent the “ground truth” is, which caps the achievable accuracy. Training uses transfer learning from pretrained backbones (see transfer learning), heavy augmentation (flips, rotations, scaling, color and noise changes, elastic deformations, mosaic), and tiling of very large images (whole-slide pathology, satellite scenes) into patches. Split data by independent unit (patient, slide, scene) to avoid leakage, and handle class imbalance by weighting or sampling. Small objects, dense overlapping objects, occlusion and changes in imaging conditions (a new microscope, season or sensor) are the usual sources of failure.
Typical applications and limits
Applications: counting and measuring cells, detecting lesions, segmenting organs for radiotherapy planning, finding surface defects and cracks, mapping land cover and flooding from satellite images, and monitoring crops. Frameworks: PyTorch with torchvision and Detectron2, Ultralytics YOLO, MMDetection, MONAI (medical), segmentation_models_pytorch. Limits: annotation cost, sensitivity to domain shift, difficulty with tiny or crowded objects, and overconfident errors that call for review.
Basic operation
- Define the task. Choose detection, semantic or instance segmentation and the required accuracy.
- Collect and annotate images. Label carefully with clear guidelines, and measure annotator agreement.
- Split properly. Separate training, validation and test sets by patient, slide or scene.
- Choose a model. Start from a pretrained detector or a U-Net-type network.
- Train with augmentation. Monitor validation metrics and loss.
- Evaluate. Compute mAP, IoU or Dice on held-out data, and inspect failures visually.
- Post-process and measure. Apply NMS or morphology, then compute counts, areas or sizes.
- Collect the output. You obtain boxes, masks and measurements; annotation strategy and model selection are covered in the video.
Good practice
- Look at the predictions. Metrics hide systematic errors.
- Validate derived measurements. Compare areas and counts with manual results.
- Prevent leakage. Keep related images in the same split.
- Test on new conditions. Use a different scanner, site or season.
- Document annotation rules. Ambiguous boundaries limit accuracy.
- Keep experts in the loop. For diagnostic or safety decisions, use models as assistance with expert review, follow privacy and consent rules for patient or personal images, and avoid applying face or person detection without clear legal and ethical grounds.
Key terms
- Bounding box — a rectangle marking an object’s location in an image.
- Intersection over union (IoU) — the overlap between predicted and true regions divided by their union.
- Non-maximum suppression — removing duplicate detections of the same object.
- mAP — mean average precision, the standard detection accuracy score.
- Dice coefficient — an overlap measure for segmentation masks.
- Instance segmentation — outlining each separate object individually.
- Skip connection — a link carrying fine detail from encoder to decoder in U-Net.
Video coming soon
About five minutes on Computer vision (detection, segmentation): how it works in detail, how the data is acquired, and how it is analyzed.
More in 4.3 AI and Machine Learning
- Regularized linear models
- Decision trees and random forests
- Gradient boosting (XGBoost)
- Support vector machines
- k-NN
- k-means
- Gaussian processes
- Neural networks
- CNNs
- RNN/LSTM
- Transformers
- Autoencoders
- GANs
- Diffusion models
- Graph neural networks
- Reinforcement learning
- Transfer learning
- Federated learning
- Explainable AI (SHAP, LIME)
- Physics-informed neural networks
- Bayesian optimization
- Active learning
- Symbolic regression
- Genetic algorithms and particle swarm optimization
- Dimensionality reduction (t-SNE, UMAP)
- NLP and large language models
- Retrieval-augmented generation
- Computer vision (detection, segmentation)
- Anomaly detection
- Machine-learned interatomic potentials
- AutoML
