matlab/matlab-agentic-toolkit / skills-catalog/image-processing-and-computer-vision/matlab-process-images
matlab-process-images
VerifiedLoad this first for any MATLAB task involving images, photos, scans, frames, volumes, or visual data — reading, writing, filtering, enhancing, denoising, sharpening, deblurring, thresholding, segmenting, masking, detecting, counting objects, removing backgrounds, measuring, registering, stitching, aligning, cropping, resizing, or transforming. Covers image I/O (TIFF, PNG, JPEG, RAW, HDR), histograms, edge detection, color, morphology, deep learning for images, quality assessment, and 3-D volumes. Also for image problems (dark, noisy, blurry, low contrast, uneven lighting, hazy, color cast) or data exploration (folder of images). Includes medical imaging I/O (DICOM, NIfTI, NRRD, medicalVolume) and large/blocked images (blockedImage, whole-slide). Skip if the user just wants you to look at an image, with no MATLAB work. Not for Simulink video-block models, C/C++/CUDA code generation or GPU deployment of a network, or point clouds. Hyperspectral and visual inspection have sibling skills.
references/video-io.md
View on GitHub# Video I/O
Read, process, and write video files using `VideoReader` and `VideoWriter`.
## Reading Video
### Key Gotcha: Memory
`read(v)` loads ALL frames into memory. For large videos, iterate instead:
```matlab
v = VideoReader("clip.mp4");
while hasFrame(v)
frame = readFrame(v);
% Process frame...
end
```
For specific frame ranges: `frames = read(v, [10 20]);`
### Frame Counting
Accessing `NumFrames` on MP4 files can be **very slow** — it must scan the entire file (e.g., ~24 s on a 69-minute clip) and blocks MATLAB during the scan.
- **Approximate count (fast):** Use `Duration * FrameRate` when you only need to estimate length, check if a video is "short" or "long", or size a progress bar.
```matlab
v = VideoReader("clip.mp4");
approxFrames = round(v.Duration * v.FrameRate);
```
- **Exact count (slow):** Use `v.NumFrames` only when an exact count is required (e.g., preallocating an array of exactly the right size).
- **Exact count without blocking:** Use `parfeval` with `backgroundPool` to compute the count asynchronously. Note that `fetchOutputs` still blocks until the operation completes.
```matlab
v = VideoReader("clip.mp4");
f = parfeval(backgroundPool, @(v) v.NumFrames, 1, v);
% ... do other work ...
exactCount = fetchOutputs(f); % blocks here until done
```
## Writing Video
### VideoWriter — Always Close
An unclosed `VideoWriter` produces a corrupt file. Use `onCleanup`:
```matlab
vw = VideoWriter("output.mp4", "MPEG-4");
cleanup = onCleanup(@() close(vw));
vw.FrameRate = 30;
vw.Quality = 95;
open(vw);
for i = 1:numFrames
writeVideo(vw, frames(:,:,:,i));
end
```
### Profiles
| Profile | Extension | Notes |
|---------|-----------|-------|
| `"MPEG-4"` | .mp4 | H.264 compressed, most common |
| `"Motion JPEG AVI"` | .avi | Per-frame JPEG compression |
| `"Motion JPEG 2000"` | .mj2 | Per-frame JPEG 2000, supports >8-bit and lossless |
| `"Archival"` | .mj2 | Lossless Motion JPEG 2000 for archival quality |
| `"Grayscale AVI"` | .avi | Uncompressed grayscale |
| `"Uncompressed AVI"` | .avi | Uncompressed RGB |
### MPEG-4 Frame Dimensions
MPEG-4 (H.264) requires even frame dimensions. **Do not pad or crop frames manually** — `VideoWriter` accepts odd-dimension frame inputs and performs the required padding internally.
### Variable Frame Rate
`VideoWriter` writes **constant frame rate video only**. In a read-process-write workflow on a variable frame rate source, the output will use the writer's `FrameRate` for all frames. The original per-frame timing is **not preserved**, even if you copy the reader's average frame rate to the writer.
## Common Mistakes
| Mistake | Why it's wrong | Correct approach |
|---------|---------------|-----------------|
| `read(v)` on large video | Loads ALL frames into memory | Use `hasFrame`/`readFrame` loop |
| `v.NumFrames` to check if video is long | Blocks MATLAB for tens of seconds on MP4 | Use `v.Duration * v.FrameRate` for approximate count |
| Manual padding for odd-dimension H.264 | Unnecessary extra code and potential artifacts | `VideoWriter` handles padding internally |
| Copying reader `FrameRate` to writer for VFR video | Silently loses original frame timing | Acknowledge constant-rate limitation; document the deviation |
| Forgetting to close `VideoWriter` | Produces corrupt output file | Use `onCleanup(@() close(vw))` |
----
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: 198k | 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 |