AgentSkillsCN

matlab-image-processing-toolbox

MATLAB 图像处理工具箱提供多种滤波功能(imgaussfilt、medfilt2、wiener2)、分割算法(graythresh、imbinarize、multithresh、watershed、activecontour)、形态学运算(strel、imopen、imclose、imerode、imdilate、bwareaopen、imfill)、区域测量(regionprops、bwconncomp、bwlabel),以及图像类型转换(im2double、im2uint8、mat2gray)。适用于噪声抑制、边缘检测、二值掩码清理、连通组件分析,以及医学影像(MRI、CT、显微镜成像)的预处理工作。

SKILL.md
--- frontmatter
name: matlab-image-processing-toolbox
description: MATLAB Image Processing Toolbox for filtering (imgaussfilt, medfilt2, wiener2), segmentation (graythresh, imbinarize, multithresh, watershed, activecontour), morphological operations (strel, imopen, imclose, imerode, imdilate, bwareaopen, imfill), region measurement (regionprops, bwconncomp, bwlabel), and image type conversions (im2double, im2uint8, mat2gray). Use for noise reduction, edge detection, binary mask cleanup, connected component analysis, and preprocessing medical images (MRI, CT, microscopy).

MATLAB Image Processing Toolbox

Expert skill for medical image analysis using MATLAB's Image Processing Toolbox (IPT R2025a+).

When to Use This Skill

  • Image filtering, denoising, or enhancement in MATLAB
  • Image segmentation (thresholding, watershed, active contours, deep learning)
  • Morphological operations (erosion, dilation, opening, closing, reconstruction)
  • Fourier/frequency domain filtering and transforms
  • Image registration and geometric transformations
  • Feature extraction (edges, regions, texture)
  • Medical imaging: MRI, CT/X-ray, microscopy/histology
  • Deep learning segmentation (semanticseg, U-Net, DeepLabv3+)
  • Large image processing (blockproc) and GPU acceleration
  • Data type handling and conversions

Read Before Coding

TaskKnowledge CardKey Functions
Reduce noisefiltering-denoising.mdimgaussfilt, medfilt2, wiener2
Detect edgesedge, imgradient, imgradientxy
Clean binary masksmorphology-binary.mdbwareaopen, imfill, imopen, imclose
Select thresholdsegmentation-thresholding.mdgraythresh, imbinarize, multithresh
Separate touching objectswatershed, bwdist, imhmin
Measure regionsfeature-regions.mdregionprops, bwconncomp, bwlabel
Register imagesimregister, imregtform, imwarp
Process large imagesblockproc, gpuArray
Segment with DLdeep-learning-segmentation.mdsemanticseg, unetLayers
Convert data typesdata-types.mdim2double, im2uint8, mat2gray
Process MRImedical-mri.mddicomread, adapthisteq, bias correction
Process CT/X-rayHU windowing, bone segmentation
Count cellsmedical-microscopy.mdCell detection, stain normalization

Note: Rows with "—" indicate functions covered in SKILL.md quick patterns. Future knowledge cards may be added.

Critical Rules

Rule 1: Data Type Handling

The #1 source of errors. Different types have different ranges:

TypeRangeWhen to Use
uint8[0, 255]Display, storage, most IPT functions
uint16[0, 65535]Medical images (DICOM), high dynamic range
double[0, 1] normalizedArithmetic operations, filtering
logical0 or 1Binary masks, segmentation results
matlab
% WRONG: Mixed types cause silent truncation
result = uint8_image + double_mask;  % Truncates!

% CORRECT: Explicit conversion
img = im2double(uint8_image);  % [0,255] → [0,1]
result = img + mask;
output = im2uint8(result);     % [0,1] → [0,255]

Rule 2: Filter Normalization

matlab
% LOWPASS: Coefficients must sum to 1 (preserves brightness)
h_avg = fspecial('average', 5);       % sum = 1
h_gauss = fspecial('gaussian', 5, 1); % sum ≈ 1

% HIGHPASS: Coefficients must sum to 0 (extracts changes)
h_laplacian = fspecial('laplacian');  % sum = 0
h_log = fspecial('log', 9, 1.5);      % sum = 0

% Verify before custom filters:
assert(abs(sum(h(:)) - 1) < 1e-6, 'Lowpass filter must sum to 1');

Rule 3: Boundary Handling

matlab
% For medical images: use 'replicate' to avoid edge artifacts
filtered = imfilter(img, h, 'replicate');

% Options: 'replicate' | 'symmetric' | 'circular' | numeric value
% Default is zero-padding which creates dark borders!

Rule 4: Structuring Element Sizing

SE radius ≈ half the feature size you want to affect.

matlab
% Remove noise blobs smaller than ~10 pixels diameter
se = strel('disk', 5);  % radius = 10/2 = 5
cleaned = imopen(bw, se);

% Fill holes up to ~20 pixels diameter
se = strel('disk', 10);
filled = imclose(bw, se);

Rule 5: Threshold Output Range

graythresh returns normalized [0,1] regardless of input type:

matlab
img_uint8 = imread('image.png');
level = graythresh(img_uint8);  % Returns 0.45, NOT 115

% For uint8 threshold value:
threshold_uint8 = level * 255;  % 0.45 * 255 = 115

% Better: use imbinarize (handles automatically)
bw = imbinarize(img_uint8);  % Internally uses graythresh

Rule 6: Memory for Large Images

matlab
% Use blockproc for images that don't fit in memory
% BorderSize MUST match filter radius to avoid tile artifacts

fun = @(block) imgaussfilt(block.data, 2);
result = blockproc(huge_image, [512 512], fun, ...
    'BorderSize', [6 6], ...      % 3*sigma for Gaussian
    'TrimBorder', true, ...
    'UseParallel', true);

Quick Patterns

Standard Preprocessing Pipeline

matlab
function out = preprocess(img)
    img = im2double(img);                           % Normalize
    img = imgaussfilt(img, 1);                      % Denoise
    img = adapthisteq(img, 'ClipLimit', 0.02);      % Enhance contrast
    out = mat2gray(img);                            % Ensure [0,1]
end

Binary Mask Cleanup

matlab
function bw = clean_mask(bw, min_area, se_radius)
    se = strel('disk', se_radius);
    bw = imopen(bw, se);              % Remove small protrusions
    bw = imclose(bw, se);             % Close small gaps
    bw = imfill(bw, 'holes');         % Fill holes
    bw = bwareaopen(bw, min_area);    % Remove small objects
end

Segment and Measure

matlab
function stats = segment_measure(img)
    bw = imbinarize(img, 'adaptive');
    bw = bwareaopen(bw, 50);
    cc = bwconncomp(bw);
    stats = regionprops('table', cc, img, ...
        'Area', 'Centroid', 'MeanIntensity', 'Eccentricity');
end

Function Quick Reference

Filtering

FunctionPurposeExample
imfilterApply custom filterimfilter(I, h, 'replicate')
imgaussfiltGaussian smoothingimgaussfilt(I, sigma)
medfilt2Median filter (salt-pepper)medfilt2(I, [3 3])
wiener2Adaptive Wienerwiener2(I, [5 5])
imsharpenUnsharp maskingimsharpen(I, 'Amount', 1)

Segmentation

FunctionPurposeExample
graythreshOtsu thresholdlevel = graythresh(I)
imbinarizeBinarize imagebw = imbinarize(I, 'adaptive')
multithreshMulti-level thresholdthresh = multithresh(I, 2)
watershedWatershed segmentationL = watershed(D)
activecontourActive contoursbw = activecontour(I, mask)

Morphology

FunctionPurposeExample
strelCreate structuring elementse = strel('disk', 5)
imerode / imdilateErosion / Dilationimerode(bw, se)
imopen / imcloseOpening / Closingimopen(bw, se)
bwareaopenRemove small objectsbwareaopen(bw, 100)
imfillFill holesimfill(bw, 'holes')

Features

FunctionPurposeExample
edgeEdge detectionedge(I, 'Canny')
regionpropsRegion measurementsregionprops('table', bw, I, 'Area')
bwconncompConnected componentscc = bwconncomp(bw)
graycomatrixTexture GLCMglcm = graycomatrix(I)

I/O & Types

FunctionPurposeExample
imread / imwriteRead/write imagesI = imread('img.png')
dicomreadRead DICOMI = dicomread('scan.dcm')
im2doubleTo double [0,1]I = im2double(I)
im2uint8To uint8 [0,255]I = im2uint8(I)

Knowledge Cards

Detailed documentation organized by topic:

Core Processing:

  • knowledge/cards/filtering-denoising.md - Noise reduction techniques
  • knowledge/cards/segmentation-thresholding.md - Otsu, adaptive, multi-level
  • knowledge/cards/morphology-binary.md - Binary morphological operations

Feature Extraction:

  • knowledge/cards/feature-regions.md - regionprops, connected components

Medical Imaging:

  • knowledge/cards/medical-mri.md - MRI preprocessing and segmentation
  • knowledge/cards/medical-microscopy.md - Cell counting, histology

Advanced:

  • knowledge/cards/deep-learning-segmentation.md - semanticseg, U-Net
  • knowledge/cards/data-types.md - Type conversions and pitfalls

Cross-Toolbox Integration

For wavelet-based image processing (multiresolution denoising, fusion), see: matlab-wavelet-toolbox skill.

matlab
% Example: Wavelet + IPT fusion for MRI denoising
denoised = wdenoise2(mri, 'DenoisingMethod', 'Bayes');  % Wavelet
cleaned = imopen(denoised, strel('disk', 2));           % IPT morphology
enhanced = adapthisteq(cleaned);                        % IPT contrast

Source: MathWorks Image Processing Toolbox Documentation (R2025a)