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/ipt-reading-writing.md
View on GitHub# IPT Reading and Writing Patterns
Read and write medical imaging data using Image Processing Toolbox functions. Use these patterns when Medical Imaging Toolbox is NOT available.
## Reading DICOM
### Single DICOM file
```matlab
info = dicominfo("slice.dcm");
pixels = dicomread(info);
% Access metadata
modality = info.Modality;
pixelSpacing = [info.PixelSpacing(1), info.PixelSpacing(2)]; % [row, col] in mm
```
### DICOM folder (single series)
```matlab
[V, spatial, dim] = dicomreadVolume(dicomFolder);
V = squeeze(V); % ALWAYS squeeze — removes singleton 4th dim for grayscale
% spatial struct fields:
% spatial.PixelSpacings — Nx2 matrix, [rowSpacing colSpacing] per slice
% spatial.PatientPositions — Nx3 matrix, [x y z] of first pixel per slice
% spatial.PatientOrientations — 2x3xN, direction cosines per slice
pixelSpacing = spatial.PixelSpacings(1, :); % [Δrow Δcol] in mm
positions = spatial.PatientPositions; % Nx3
sliceSpacing = abs(positions(2,3) - positions(1,3)); % slice thickness from positions
```
### DICOM collection (multiple series)
```matlab
% List all series in a directory (includes subfolders by default)
collection = dicomCollection(parentDir, IncludeSubfolders=true);
% Table columns: Filenames, Rows, Columns, Channels, Frames,
% StudyDescription, SeriesDescription, Modality,
% PatientName, PatientID, StudyDate, SeriesNumber
% Read a specific series by row name
[V, spatial] = dicomreadVolume(collection, "s2");
V = squeeze(V);
```
## Reading NIfTI
```matlab
V = niftiread(niftiFile); % Numeric array
info = niftiinfo(niftiFile); % Metadata struct
% Key info fields:
% info.ImageSize — [rows cols slices]
% info.PixelDimensions — [dx dy dz] voxel spacing in mm
% info.Transform — affine3d object (intrinsic to world)
% info.Datatype — 'uint16', 'int16', 'single', etc.
% info.SpaceUnits — 'Millimeter', 'Meter', etc.
voxelSize = info.PixelDimensions(1:3);
```
## Rescale Slope / Intercept
`dicomread` returns **raw stored values** — it does NOT apply RescaleSlope/RescaleIntercept. For CT data, convert to Hounsfield Units manually:
```matlab
img = dicomread("ct_slice.dcm");
info = dicominfo("ct_slice.dcm");
% Apply rescaling to get calibrated units (e.g., Hounsfield Units for CT)
img_hu = double(img) * info.RescaleSlope + info.RescaleIntercept; % RescaleSlope must not be empty
```
Note: `medicalVolume` and `medicalImage` (Medical Imaging Toolbox) auto-apply rescaling — their `Voxels`/`Pixels` property is already in calibrated units.
## Writing DICOM
```matlab
% Write a single slice
dicomwrite(uint16(sliceData), "output.dcm");
% Write with metadata (copy from original)
info = dicominfo("original.dcm");
dicomwrite(uint16(sliceData), "output.dcm", info, CreateMode="Copy");
```
**Key:** RT Structure files require `CreateMode="Copy"` — the SOP class is not supported in full verification mode.
## Writing NIfTI
```matlab
% Basic write (no spatial metadata)
niftiwrite(V, "output.nii");
% Write with metadata (preserves voxel spacing, transform)
info = niftiinfo("original.nii");
info.Datatype = class(V);
niftiwrite(V, "output.nii", info);
% Compressed output
niftiwrite(V, "output.nii.gz", info, Compressed=true);
```
**Limitation:** `niftiwrite` with a manually constructed info struct is error-prone. If Medical Imaging Toolbox is available, use `write(medVol, "output.nii")` instead.
## Building imref3d (Spatial Referencing for Registration)
`imref3d` maps voxel indices to world coordinates. Required for registration functions.
```matlab
% From NIfTI info
ref = imref3d(size(V), ...
info.PixelDimensions(2), ... % XWorldLimits (col spacing)
info.PixelDimensions(1), ... % YWorldLimits (row spacing)
info.PixelDimensions(3)); % ZWorldLimits (slice spacing)
% From dicomreadVolume spatial struct
ref = imref3d(size(V), ...
spatial.PixelSpacings(1, 2), ... % col spacing
spatial.PixelSpacings(1, 1), ... % row spacing
abs(spatial.PatientPositions(2,3) - spatial.PatientPositions(1,3))); % slice spacing
```
**Argument order:** `imref3d(imageSize, xSpacing, ySpacing, zSpacing)` — x is columns, y is rows.
## Common Mistakes
| Mistake | Correct Approach |
|---------|-----------------|
| Not using `squeeze` on `dicomreadVolume` output | Always `squeeze(V)` for grayscale data |
| Using `dicomreadVolume` per folder to list series | `dicomCollection(dir)` then read selected |
| `imref3d(size, ySpacing, xSpacing, z)` — swapped x/y | `imref3d(size, xSpacing, ySpacing, zSpacing)` — x is columns |
| Using raw `dicomread` values as Hounsfield Units | Apply `RescaleSlope * value + RescaleIntercept` |
| Building NIfTI info struct from scratch | Use `niftiinfo` on existing file, or `medicalVolume.write` |
----
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 |