matlab/matlab-agentic-toolkit / skills-catalog/image-processing-and-computer-vision/matlab-use-visual-inspection
matlab-use-visual-inspection
VerifiedBuild machine vision inspection systems with MATLAB Visual Inspection Toolbox. Covers the full arc from problem selection through deployment: anomaly detection (Student-Teacher, PatchCore, FastFlow, FCDD), object detection (YOLOX), counting (CounTR, shape matching), gauging and measurement (caliper, fitcircle, fitangle), blob analysis (regionprops, calibrated measurement), shape matching for fixturing and localization, synthetic data generation for training augmentation, and deployment to standalone apps, embedded C/C++, NVIDIA GPUs (TensorRT), NPUs (ONNX), and Beckhoff PLCs. Routes to rule-based tools when problems are geometric and deterministic, AI-based tools when defect appearance varies, and hybrid pipelines combining both.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 12.3 KB |
| manifest.yaml | config | 629 B |
| references/blob-analysis.md | markdown | 7.0 KB |
| references/count-objects.md | markdown | 6.4 KB |
| references/deploy-inspection.md | markdown | 10.1 KB |
| references/detect-anomaly.md | markdown | 10.0 KB |
| references/detect-objects.md | markdown | 11.4 KB |
| references/match-shape.md | markdown | 7.1 KB |
| references/measure-gauging.md | markdown | 11.1 KB |
| references/synthetic-data.md | markdown | 11.7 KB |
references/count-objects.md
View on GitHub
# Counting Objects in Images
Count objects using the right approach for your data and environment. Three
primary methods are available, each suited to different scenarios.
## When to Use
- Counting parts on a conveyor or tray
- Counting defects in an inspection image
- Inventory verification (are all N parts present?)
- Quality check (count matches expected quantity)
## Decision Tree
| Scenario | Data Available | Recommended Approach |
|----------|---------------|---------------------|
| Controlled environment, uniform objects, no labeled data | Single template image | `shapemodel`/`matchshape` with high `MaxMatches` |
| Intra-class variation, few examples (1–3 boxes), no full annotation | A few exemplar bounding boxes | `counTRObjectCounter` (low-shot counting) |
| Complex scenes, labeled bounding box data | Full annotation set | `yoloxObjectDetector` → count detections |
### How to Choose
1. **Are objects uniform in shape?** (same part, same orientation range)
- Yes → shape matching. Count = number of matches. No training needed.
2. **Do objects vary in appearance?** (different sizes, orientations, partial views)
- Yes, but you only have a few examples → CounTR (1–3 exemplar boxes)
- Yes, and you have full bounding box annotations → YOLOX detection
Blob analysis (`bwconncomp` + `regionprops`) is also a standard approach but
requires careful parameter tuning to generalize well — particularly for touching
or overlapping objects. The approaches above are typically more robust for
industrial counting tasks.
## Approach 1: Shape Matching (Template-Based)
Best when objects are geometrically uniform and appear against a relatively
clean background. Requires zero training — just one template.
```matlab
model = shapemodel(template, RotationRange=[-30 30]);
results = matchshape(model, searchImage, MaxMatches=Inf, ScoreThreshold=0.7);
count = numel(results);
fprintf("Found %d objects\n", count)
```
Set `MaxMatches` at least as high as the maximum expected count in the scene.
Use `Inf` when the count is unknown; use a known upper bound (e.g., 50) when
you want to limit computation for speed.
See `references/match-shape.md` for full guidance on parameter tuning, synthetic
templates, and multi-class counting.
**Strengths:** No training, fast, deterministic, works with a single template.
**Limitations:** Struggles with high intra-class variation, heavy occlusion, or
cluttered backgrounds where edges overlap.
## Approach 2: CounTR (Low-Shot Counting)
Best when objects vary in appearance and you have only 1–3 exemplar bounding
boxes. CounTR is a transformer-based density estimation model — it predicts a
density map whose integral equals the count.
### Construction from Exemplar Boxes
Provide an image and bounding boxes around a few representative objects:
```matlab
exemplarImg = imread("scene.png");
exemplarBoxes = [120 80 45 45; 300 210 50 48; 55 310 42 44];
counter = counTRObjectCounter(exemplarImg, exemplarBoxes);
```
### Construction from Patches
Alternatively, provide pre-cropped patches directly:
```matlab
patches = {patch1; patch2; patch3};
counter = counTRObjectCounter(patches);
```
Patches are internally resized to 64×64×3.
### Counting Objects
```matlab
count = countObjects(counter, testImage);
```
For a datastore of images:
```matlab
counts = countObjects(counter, imds);
```
Returns an integer-valued count per image.
### Density Map Visualization
The density map shows where the model believes objects are located:
```matlab
dmap = densityMap(counter, testImage);
overlay = anomalyMapOverlay(testImage, dmap, "Blend", "equal");
imshow(overlay)
title(sprintf("Count = %d", countObjects(counter, testImage)))
```
### Key Considerations
- **Exemplar quality matters:** Choose exemplars representative of the variation
in the scene (different sizes, orientations, lighting if present).
- **No localization:** CounTR provides a density map but not individual bounding
boxes. If you need per-object localization, use YOLOX detection instead.
- **Support package required:** CounTR requires the CounTR support package for
Visual Inspection Toolbox.
## Approach 3: YOLOX Detection
Best for complex scenes with full bounding box annotations. Count = number of
detected objects above threshold.
```matlab
[bboxes, scores, labels] = detect(detector, img, Threshold=0.5);
count = size(bboxes, 1);
```
See `references/detect-objects.md` for training, threshold selection, and
evaluation workflows.
**Strengths:** Precise localization, per-class counting, handles complex scenes.
**Limitations:** Requires labeled training data (bounding boxes for all objects).
## Evaluation
### Shape Matching
Count accuracy is deterministic given parameters. Tune `ScoreThreshold` and
verify against known counts:
```matlab
results = matchshape(model, img, MaxMatches=Inf, ScoreThreshold=0.7);
countError = numel(results) - groundTruthCount;
```
### CounTR
Evaluate counting accuracy across a test set:
```matlab
counts = countObjects(counter, dsTest);
groundTruth = [10; 12; 8; 15; 11];
mae = mean(abs(counts - groundTruth));
fprintf("Mean Absolute Error: %.2f\n", mae)
```
### YOLOX
Use `evaluateObjectDetection` for full AP/PR analysis. Count accuracy follows
from detection accuracy at the chosen threshold.
## Conventions
- **Always:** start with the decision tree — don't default to generic IPT approaches
- **Always:** set `MaxMatches` high enough to not limit the count (at least the max expected objects in the scene)
- **Never:** use CounTR when you need per-object bounding boxes — it only provides density maps
- **Prefer:** shape matching over CounTR when objects are geometrically uniform (simpler, no model needed)
- **Prefer:** CounTR over YOLOX when you lack full bounding box annotations but have a few exemplars
## Common Mistakes
| Mistake | Why It's Wrong | Correct Approach |
|---------|---------------|-----------------|
| Defaulting to YOLOX without labeled data | Can't train without bounding box annotations | CounTR (few exemplars) or shape matching (template) |
| `matchshape` with default `MaxMatches=1` | Only finds one object | Set `MaxMatches` ≥ max expected count |
| Using CounTR when objects are perfectly uniform | Overkill — shape matching is simpler and more precise | `shapemodel`/`matchshape` |
| Not choosing representative CounTR exemplars | Model won't generalize to variation in scene | Pick exemplars covering size/orientation range |
----
Copyright 2026 The MathWorks, Inc.
----
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| writing-skillsobra/superpowersUse when creating new skills, editing existing skills, or verifying skills work before deployment | Stars: 322k | Updated | Claude CodeCodex | Verified | Install |
| xlsxanthropics/skillsUse this skill any time a spreadsheet file is the primary input or output. This means any task where the user wants to: open, read, edit, or fix an existing .xlsx, .xlsm, .xltx, .csv, or .tsv file (e.g., adding columns, computing formulas, formatting, charting, cleaning messy data); create a new spreadsheet from scratch or from other data sources; or convert between tabular file formats. Trigger especially when the user references a spreadsheet file by name or path — even casually (like "the xlsx in my downloads") — and wants something done to it or produced from it. Also trigger for cleaning or restructuring messy tabular data files (malformed rows, misplaced headers, junk data) into proper spreadsheets. The deliverable must be a spreadsheet file. Do NOT trigger when the primary deliverable is a Word document, HTML report, standalone Python script, database pipeline, or Google Sheets API integration, even if tabular data is involved. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |
| shipping-and-launchaddyosmani/agent-skillsPrepares production launches. Use when preparing to deploy to production, or when asking what needs to be in place before shipping. Use when you need a pre-launch checklist, when setting up monitoring, when planning a staged rollout, or when you need a rollback strategy. | Stars: 119k | Updated | Claude CodeCodex | Verified | Install |
| childhoodthedotmack/claude-memBabysit a shipped release after merge, publication, or deployment. Verify published packages, production behavior, installed plugins, and late CI or user reports until the observation window is clean; use babysit for PR readiness before release. | Stars: 118k | Updated | Claude CodeCodex | Verified | Install |