AgentSkillsCN

matlab-medical-imaging-toolbox

MATLAB 医学影像工具箱可用于加载医学图像(medicalVolume、dicomread、dicominfo、dicomCollection、niftiread、nrrdread)、坐标系转换(intrinsicToWorld、worldToIntrinsic、medicalref3d)、3D 可视化(volshow 附带 OverlayData、sliceViewer)、配准(imregmoment、imregdeform、imregtform)、放射组学(radiomics 对象、intensityFeatures、shapeFeatures、textureFeatures),以及 AI 分割(medicalSegmentAnythingModel、segmentCells2D)。适用于 DICOM/NIfTI I/O、患者到体素的坐标转换,以及体积医学图像工作流。

SKILL.md
--- frontmatter
name: matlab-medical-imaging-toolbox
description: MATLAB Medical Imaging Toolbox for loading medical images (medicalVolume, dicomread, dicominfo, dicomCollection, niftiread, nrrdread), coordinate systems (intrinsicToWorld, worldToIntrinsic, medicalref3d), 3D visualization (volshow with OverlayData, sliceViewer), registration (imregmoment, imregdeform, imregtform), radiomics (radiomics object, intensityFeatures, shapeFeatures, textureFeatures), and AI segmentation (medicalSegmentAnythingModel, segmentCells2D). Use for DICOM/NIfTI I/O, Patient-to-voxel coordinate conversion, and volumetric medical image workflows.

MATLAB Medical Imaging Toolbox

Expert skill for 3D medical image analysis using MATLAB's Medical Imaging Toolbox (MIT R2025b+).

Cross-Toolbox Note: For filtering, segmentation, and morphology operations, see matlab-image-processing-toolbox skill. MIT handles I/O, spatial referencing, registration, and medical-specific workflows.

When to Use This Skill

  • Reading/writing DICOM series, NIfTI (.nii/.nii.gz), or NRRD files
  • Creating medicalVolume objects with spatial referencing
  • Converting between Patient (world) and Intrinsic (voxel) coordinates
  • 3D volume visualization (volshow, sliceViewer)
  • Medical image registration (rigid, affine, deformable, multimodal)
  • Radiomics feature extraction (IBSI-compliant)
  • AI-assisted segmentation with MedSAM or Cellpose
  • PACS server integration for DICOM retrieval
  • Multi-modality workflows (PET/CT fusion, MRI series)
  • Ground truth labeling with Medical Image Labeler app

Read Before Coding

TaskKnowledge CardKey Functions
Read DICOM seriesfile-io-dicom.mdmedicalVolume, dicomread, dicomCollection
Load NIfTI/NRRDfile-io-nifti-nrrd.mdniftiread, nrrdread, niftiinfo
Create medical volumemedical-volume.mdmedicalVolume, medicalImage, medicalref3d
Coordinate conversioncoordinate-systems.mdintrinsicToWorld, worldToIntrinsic
3D visualizationvisualization-3d.mdvolshow, sliceViewer, labelvolshow
Rigid registrationregistration-rigid.mdimregmoment, imregicp, fitgeotform3d
Deformable registrationregistration-deformable.mdimregdeform, imreggroupwise
Extract radiomicsradiomics-features.mdintensityFeatures, shapeFeatures, textureFeatures
Segment with MedSAMsegmentation-medsam.mdmedicalSegmentAnythingModel
Cell detectionsegmentation-cellpose.mdsegmentCells2D, trainCellpose
Label ground truthlabeling-workflow.mdgroundTruthMedical, Medical Image Labeler
PACS server accesspacs-integration.mddicomConnection, dicomquery, dicomget
Apply filteringcross-toolbox-ipt.md→ See IPT skill
Threshold/segmentcross-toolbox-ipt.md→ See IPT skill

Critical Rules

Rule 1: Coordinate Systems - The #1 Source of Errors

Medical images have TWO coordinate systems:

SystemUnitsOriginUse Case
IntrinsicVoxel indices (i,j,k)Corner of first voxelArray indexing, IPT functions
Patient/WorldPhysical units (mm)DICOM-defined patient originClinical measurements, registration
matlab
% WRONG: Mixing coordinate systems
pixel = V.Voxels(100, 200, 50);  % Intrinsic indexing
world_point = [100, 200, 50];    % Treating indices as world coords!

% CORRECT: Explicit conversion (separate coordinate arrays)
V = medicalVolume('scan.nii');
i = 100; j = 200; k = 50;  % Voxel indices
[x, y, z] = intrinsicToWorld(V.VolumeGeometry, i, j, k);
% x, y, z are now in mm, in patient coordinate system
world_point = [x, y, z];

% Convert back (also requires separate arrays)
[row, col, slice] = worldToSubscript(V.VolumeGeometry, x, y, z);

Rule 2: Always Use medicalVolume for Spatial Context

Never read raw voxels without spatial information:

matlab
% WRONG: Loses spatial referencing
data = niftiread('brain.nii');  % Just a 3D array, no spatial info

% CORRECT: Preserves geometry
V = medicalVolume('brain.nii');
% V.VolumeGeometry contains transformation matrix
% V.VoxelSpacing is in mm
% V.SpatialUnits documents the units

Rule 3: Check Orientation Before Slice Extraction

Slice orientation depends on acquisition, not array dimension:

matlab
V = medicalVolume('scan.dcm');

% Check orientation
disp(V.Orientation);        % 'transverse', 'sagittal', or 'coronal'
disp(V.PlaneMapping);       % Maps dimensions to anatomical planes

% Extract slices in patient coordinates (not array indices!)
slice = extractSlice(V, 50, 'transverse');  % 50th transverse slice

% For axial browsing regardless of acquisition:
sliceViewer(V);  % Automatically handles orientation

Rule 4: DICOM Metadata First

Always inspect metadata before processing:

matlab
% Get collection info for DICOM series
collection = dicomCollection('DICOM_folder');
disp(collection.Properties.VariableNames);

% Check modality, series description
info = dicominfo('slice001.dcm');
fprintf('Modality: %s\n', info.Modality);
fprintf('Series: %s\n', info.SeriesDescription);
fprintf('Spacing: %.2f x %.2f x %.2f mm\n', ...
    info.PixelSpacing(1), info.PixelSpacing(2), info.SliceThickness);

Rule 5: Memory Management for 3D Volumes

Large volumes (512×512×500+) require careful memory handling:

matlab
% Check volume size before loading
info = niftiinfo('large_scan.nii');
voxels = prod(info.ImageSize);
bytes_needed = voxels * info.BitsPerPixel / 8;
fprintf('Size: %.2f GB\n', bytes_needed / 1e9);

% Process slice-by-slice for huge volumes
for k = 1:V.NumTransverseSlices
    slice = extractSlice(V, k, 'transverse');
    % Process slice using IPT functions
    processed = imgaussfilt(slice, 1.5);  % IPT
    V = replaceSlice(V, processed, k, 'transverse');
end

Rule 6: Cross-Toolbox Workflow

MIT handles I/O and spatial referencing; IPT handles pixel-level processing:

matlab
% Load with MIT (preserves spatial info)
V = medicalVolume('scan.nii');

% Process with IPT (see matlab-image-processing-toolbox skill)
voxels = im2double(V.Voxels);
voxels = imgaussfilt3(voxels, 1.5);           % Gaussian smoothing
voxels = adapthisteq(voxels, 'NumTiles', [4 4 4]);  % CLAHE

% Segment with IPT
level = graythresh(voxels(:));
mask = voxels > level;
mask = imopen(mask, strel('sphere', 3));      % Morphological cleanup

% Create labeled volume with MIT (preserves spatial context)
labelVol = medicalVolume(uint8(mask), V.VolumeGeometry);
labelVol.Modality = 'SEG';
write(labelVol, 'segmentation.nii');

Quick Patterns

Load DICOM Series to medicalVolume

matlab
% From directory of DICOM files
V = medicalVolume('path/to/dicom_folder');

% From dicomCollection for multi-series
coll = dicomCollection('patient_folder', 'IncludeSubfolders', true);
disp(coll);  % Shows all series

% Load specific series
V = medicalVolume(coll, 'Rows', 2);  % 2nd series in collection

Extract Slice in Patient Coordinates

matlab
V = medicalVolume('brain.nii');

% Get middle slices in each plane
midT = round(V.NumTransverseSlices / 2);
midC = round(V.NumCoronalSlices / 2);
midS = round(V.NumSagittalSlices / 2);

transverse = extractSlice(V, midT, 'transverse');
coronal = extractSlice(V, midC, 'coronal');
sagittal = extractSlice(V, midS, 'sagittal');

% Display
figure;
subplot(1,3,1); imshow(transverse, []); title('Transverse');
subplot(1,3,2); imshow(coronal, []); title('Coronal');
subplot(1,3,3); imshow(sagittal, []); title('Sagittal');

Register Two Volumes

matlab
fixed = medicalVolume('pre_contrast.nii');
moving = medicalVolume('post_contrast.nii');

% Rigid registration (fast, for same-modality)
[registered, tform] = imregmoment(moving, fixed);

% Affine registration with optimization
[optimizer, metric] = imregconfig('monomodal');
tform = imregtform(moving.Voxels, moving.VolumeGeometry, ...
                   fixed.Voxels, fixed.VolumeGeometry, ...
                   'affine', optimizer, metric);

% Apply transformation
registered = imwarp(moving.Voxels, tform, 'OutputView', ...
    imref3d(size(fixed.Voxels)));

Extract Radiomics Features

matlab
V = medicalVolume('tumor_scan.nii');
mask = medicalVolume('tumor_mask.nii');

% Resample to isotropic (required for some features)
V_iso = resample(V, [1 1 1]);  % 1mm isotropic
mask_iso = resample(mask, [1 1 1]);

% Create radiomics object first (required pattern)
R = radiomics(V_iso.Voxels, mask_iso.Voxels > 0);

% Extract IBSI-compliant features as methods of radiomics object
intensity = intensityFeatures(R);
shape = shapeFeatures(R);
texture = textureFeatures(R);

% Combine into table
features = [intensity, shape, texture];

Segment with MedSAM

matlab
% Load model (requires support package)
model = medicalSegmentAnythingModel;

% Load image and extract embeddings
V = medicalVolume('liver_ct.nii');
slice = extractSlice(V, 50, 'transverse');
embeddings = extractEmbeddings(model, slice);

% Get image size (required parameter)
imageSize = size(slice);

% Segment with bounding box prompt (imageSize is required)
bbox = [100, 100, 200, 150];  % [x, y, width, height]
[mask, score] = segmentObjectsFromEmbeddings(model, embeddings, imageSize, ...
    BoundingBox=bbox);

Function Quick Reference

File I/O

FunctionPurposeExample
medicalVolumeLoad 3D medical imageV = medicalVolume('scan.nii')
medicalImageLoad 2D medical image seriesI = medicalImage('us_video.dcm')
dicomreadRead single DICOM fileimg = dicomread('slice.dcm')
dicominfoRead DICOM metadatainfo = dicominfo('slice.dcm')
dicomCollectionCatalog DICOM folderscoll = dicomCollection(folder)
niftireadRead NIfTI filedata = niftiread('brain.nii')
nrrdreadRead NRRD filedata = nrrdread('scan.nrrd')
writeWrite medicalVolume to NIfTIwrite(V, 'output.nii')

Spatial Referencing

FunctionPurposeExample
medicalref3dCreate spatial referenceR = medicalref3d(volumeSize, voxelSpacing)
intrinsicToWorldVoxel to patient coords[X,Y,Z] = intrinsicToWorld(R, I, J, K)
worldToIntrinsicPatient to voxel coords[I,J,K] = worldToIntrinsic(R, X, Y, Z)
worldToSubscriptPatient coords to indices[row,col,slice] = worldToSubscript(R, X, Y, Z)
extractSliceGet slice in patient spaces = extractSlice(V, n, 'transverse')
resampleResample to new resolutionV2 = resample(V, [1 1 1])

Visualization

FunctionPurposeExample
volshow3D volume renderingvolshow(V) or volshow(V, OverlayData=L)
sliceViewerOrthogonal slice browsersliceViewer(V)
labelvolshowREMOVED in R2025bUse volshow(V, OverlayData=labels) instead
montage2D slice montagemontage(V.Voxels)
implayVideo playback (2D series)implay(I)

Registration

FunctionPurposeExample
imregmomentFast moment-based rigid[tform, reg] = imregmoment(moving, fixed)
imregicpIterative closest point[regSurf, tform] = imregicp(surf1, surf2)
imregdeformDeformable registrationD = imregdeform(moving, fixed)
imreggroupwiseGroupwise registrationtforms = imreggroupwise(volumes)
fitgeotform3dLandmark-based transformtform = fitgeotform3d(pts1, pts2, 'affine')

Radiomics

FunctionPurposeExample
radiomicsCreate radiomics objectR = radiomics(data, roi)
intensityFeaturesHistogram-based featuresF = intensityFeatures(R)
shapeFeatures3D shape descriptorsF = shapeFeatures(R)
textureFeaturesGLCM, GLRLM, etc.F = textureFeatures(R)

AI Segmentation

FunctionPurposeExample
medicalSegmentAnythingModelLoad MedSAMmodel = medicalSegmentAnythingModel
extractEmbeddingsCompute image embeddingsemb = extractEmbeddings(model, img)
segmentObjectsFromEmbeddingsSegment with promptsmask = segmentObjectsFromEmbeddings(...)
segmentCells2D*Cellpose 2D segmentationL = segmentCells2D(img, 'cyto')
segmentCells3D*Cellpose 3D segmentationL = segmentCells3D(V, 'nuclei')
trainCellpose*Train custom Cellposemodel = trainCellpose(ds, opts)

* Cellpose functions require the "Deep Learning Toolbox Model for Cellpose" support package. Install via Add-On Explorer.

PACS Integration

FunctionPurposeExample
dicomConnectionConnect to PACSconn = dicomConnection(ip, port)
dicomqueryQuery PACS for studiesresults = dicomquery(conn, 'Study')
dicomgetRetrieve imagesdicomget(conn, results, folder)
dicomstoreSend images to PACSdicomstore(conn, folder)

Knowledge Cards

Detailed documentation organized by topic:

File I/O:

  • knowledge/cards/file-io-dicom.md - DICOM reading, metadata, series handling
  • knowledge/cards/file-io-nifti-nrrd.md - NIfTI and NRRD formats

Core Concepts:

  • knowledge/cards/medical-volume.md - medicalVolume class and properties
  • knowledge/cards/coordinate-systems.md - Patient vs Intrinsic coordinates (CRITICAL)
  • knowledge/cards/visualization-3d.md - volshow, sliceViewer, rendering options

Registration:

  • knowledge/cards/registration-rigid.md - Rigid/affine registration methods
  • knowledge/cards/registration-deformable.md - Deformable and groupwise registration

Analysis:

  • knowledge/cards/radiomics-features.md - IBSI-compliant radiomics extraction
  • knowledge/cards/segmentation-medsam.md - Medical Segment Anything Model
  • knowledge/cards/segmentation-cellpose.md - Cellpose for microscopy

Workflows:

  • knowledge/cards/labeling-workflow.md - Medical Image Labeler app
  • knowledge/cards/pacs-integration.md - PACS server communication
  • knowledge/cards/cross-toolbox-ipt.md - Using IPT functions with MIT

Cross-Toolbox Integration

For filtering, segmentation, and morphology → matlab-image-processing-toolbox

MIT TaskIPT FunctionIPT Knowledge Card
Denoise volume slicesimgaussfilt, medfilt2, wiener2filtering-denoising.md
Enhance contrastadapthisteq, imadjustfiltering-denoising.md
Threshold volumegraythresh, imbinarizesegmentation-thresholding.md
Clean binary masksimopen, imclose, imfill, bwareaopenmorphology-binary.md
Measure regionsregionprops3, bwconncompfeature-regions.md
Detect edgesedge, imgradientfeature-edges.md
Data type conversionim2double, im2uint8data-types.md

For wavelet-based denoising → matlab-wavelet-toolbox

matlab
% Combined workflow: MIT + IPT + Wavelet
V = medicalVolume('noisy_mri.nii');           % MIT: Load with spatial info

% Process each slice (IPT + Wavelet)
for k = 1:size(V.Voxels, 3)
    slice = im2double(V.Voxels(:,:,k));       % IPT: Convert type
    slice = wdenoise2(slice);                  % Wavelet: Denoise
    slice = adapthisteq(slice);                % IPT: Enhance
    V.Voxels(:,:,k) = slice;
end

write(V, 'enhanced_mri.nii');                  % MIT: Write with spatial info

Source: MathWorks Medical Imaging Toolbox Documentation (R2025b) User Guide: 468 pages | Function Reference: 362 pages