Generated banner for Matlab Process Images
Generated logo for Matlab Process Images

Matlab Process Images

MathWorksOfficialCreated by MathWorks

  • Skill
  • Tool integration
  • Software development
  • BSD-2-Clause
  • No issues found

From the author

Load 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.
— the description by MathWorks

AutoGineer summary

Written by AutoGineer from the skill’s own files, not by the author

This skill serves as the primary entry point for MATLAB tasks involving images, scans, frames, and 3-D volumes. It provides core conventions for image data types, coordinate systems, and display practices, while routing users to specific references for tasks such as I/O, filtering, segmentation, and measurement. It ensures consistent use of Image Processing Toolbox functions and helps users navigate common pitfalls in image analysis workflows.

What it does

  • Provides standardized conventions for image data types and coordinate systems.
  • Routes users to specialized references for image I/O, filtering, and transformation tasks.
  • Defines best practices for displaying images using the imageshow function.
  • Offers guidance on handling 3-D volumetric data and large-scale images.
  • Assists with selecting appropriate functions for segmentation and object measurement.
  • Provides troubleshooting patterns for common image processing errors.

When to use it

  • When performing image processing tasks in MATLAB, such as filtering, enhancing, or segmenting.
  • When working with image formats like TIFF, PNG, JPEG, DICOM, or NIfTI.
  • When needing to understand MATLAB-specific coordinate conventions for image data.
  • When determining the correct toolbox function for a specific image analysis goal.
  • When troubleshooting syntax or type-related errors in image processing scripts.

Advantages

  • Ensures consistent coding patterns across different image processing tasks.
  • Reduces errors by providing clear guidance on coordinate and data type conventions.
  • Simplifies the selection of appropriate tools for complex workflows like 3-D volume processing.
  • Promotes the use of modern display functions like imageshow over legacy alternatives.
  • Provides a structured approach to identifying the correct documentation for specific functions.

Usage examples

“How do I read a DICOM file and display it in MATLAB?”
The agent will guide you to use the medical-data-io reference for reading the file and the imageshow function for display, following the core conventions.
“I need to count the number of objects in a binary image.”
The agent will provide the standard analysis pipeline using bwconncomp and regionprops, ensuring the input is correctly formatted as logical.
“How do I perform 3-D filtering on a volume?”
The agent will direct you to the 3-D volume processing reference to select the appropriate 3-D specific function, such as imgaussfilt3.
“Why is my regionprops measurement returning only one object?”
The agent will explain that the input must be a logical array and provide the code to convert your numeric array to logical.

Install

  1. Download the skill as a ZIP:

    https://autogineer.com/agent-skills/matlab/matlab-process-images/download
  2. Unzip it into the cross-client skills folder of your project:

    mkdir -p .agents/skills && unzip matlab-process-images.zip -d .agents/skills

Documentation by MathWorks

Image Processing

Process, analyze, and transform images using MATLAB Image Processing Toolbox (IPT). This skill routes you to the correct reference for your task and provides core conventions that apply across all IPT work.

When to Use

Loaded for general-purpose IPT work: file and batch I/O, filtering, restoration, morphology, segmentation, measurement, geometric transforms, color, texture, quality metrics, deep learning on images, and image apps. Non-medical microscopy — cell and particle counting on plain IPT — is in scope.

Hand off (and co-load) instead of answering alone: see the handoff table under ## Routing.

When NOT to Use

  • Training a network with no image-specific preprocessing — use Deep Learning Toolbox directly

  • Simulink or Stateflow video blocks — use Simulink directly

  • C/C++ code generation, MEX, GPU acceleration and deployment — use MATLAB Coder / GPU Coder directly

  • Point clouds, 3-D reconstruction, and video stabilization — use Computer Vision Toolbox directly

  • General MATLAB questions unrelated to images

  • Looking at an image yourself (e.g., "look at this screenshot") when no MATLAB work is requested — use your own vision instead

Core Conventions

Image Data Types
TypeRangeWhen to use
uint80–255Display, file I/O, memory-efficient storage
uint160–65535High dynamic range sensors, medical. Camera RAW is n-bit in a uint16 container — rescale with rawinfo BlackLevel/WhiteLevel, not 65535
single / double0.0–1.0Arithmetic, filtering, computation
logical0 or 1Binary masks, segmentation results

Most IPT functions accept integer types directly — do not convert to floating-point unless required.

Default Coordinate Conventions
  • MATLAB images are (row, col) — row is Y (down), column is X (right)

  • size(img) returns [height, width, channels]

  • Spatial functions use (x, y) order for coordinates (e.g., imref2d, regionprops centroids)

  • Pixel centers are at integer intrinsic coordinates; in default world referencing, pixel (1,1) center is at world (1.0, 1.0) and the image edge starts at 0.5

  • imref2d and imref3d can specify non-default spatial referencing to place images into world coordinate systems. By default they use the unit-spaced, integer-pixel-centered coordinate system described here, e.g. Rout = imref2d(imageSize). Functions that do not accept spatial referencing objects assume this intrinsic coordinate system for any operation requiring spatial referencing.

Multichannel Images
  • Grayscale: M×N or M×N×1

  • RGB: M×N×3

  • With alpha: read using [img, ~, alpha] = imread(...) (alpha is the third output)

  • Convert: rgb2gray, im2gray (preferred, handles more input types)

Display Functions — use imageshow, not imshow

Always use imageshow instead of imshow in all generated code — scripts, apps, examples, and quick previews. The signatures are compatible for common cases: replace imshow(...) with imageshow(...). imageshow (R2024b+) handles sub-bit-depth containers (e.g., 12-bit in uint16) via DisplayRangeMode, supports OverlayData for label/mask overlays, and works with viewer2d. imshow lacks all of these capabilities and should not appear in new code.

Title convention: imageshow creates a viewer2d, not an axes — gca does not return it. To add a title, get the viewer from the Image object's parent:

obj = imageshow(im);
viewer = obj.Parent;
title(viewer, "Title Text");

Do not use title("text") or title(gca, "text") after imageshow — these silently fail or create a stray axes title.

Code Execution

Execute code blocks via evaluate_matlab_code before presenting them to the user. If it errors, fix and re-run. This catches type mismatches, wrong argument orders, and nonexistent functions before the user sees them. If execution requires user data or toolboxes not available in the current session, present the code with a note that it should be run with the user's own data — do not loop trying to synthesize stand-in data.

Error Recovery

If code throws an error after execution, load the matlab-read-documentation skill and look up the function's doc page to verify syntax, required arguments, and supported input types before retrying.

When to Ask the User

Ask for clarification before proceeding when:

  • The image type/format is ambiguous and affects the processing approach

  • Multiple valid approaches exist with different tradeoffs (speed vs quality, lossy vs lossless)

  • The task requires choosing parameters that depend on domain knowledge (e.g., object size thresholds, noise characteristics)

  • The user's MATLAB release is unknown and the recommended function may not be available

Routing

Match your task in the tables below. Check the handoff table first. If the task belongs to another skill, load that skill in addition to this one — the Core Conventions above (data types, coordinate order, code execution) apply to all image data. Do not re-derive them from the sibling. If a row in both tables fits, the handoff table wins on workflow; this skill still owns conventions.

  • Unfamiliar data? Load references/image-understanding.md first — but only when class, range, or content is unknown.

  • Starting from a function name, not a task? The function column is illustrative, not exhaustive. To locate any function: grep -rln '<functionName>' references/. To look up syntax details, load matlab-read-documentation.

  • Pipeline tasks: load several references; cap at three unless the task genuinely spans more.

  • Follow the patterns in loaded references. References contain specific code patterns and workflows — use them instead of improvising equivalent code from general knowledge.

  • No row matches? Name the owning product, state the closest IPT fallback, and say so — do not guess a reference.

Check first: is this another skill's job?
If the task involves...Also load
Segmenting anatomy — tumor, lesion, organ, vessel — in CT, MRI, PET, ultrasound; MedSAM; radiomicsNo skill covers this — Medical Imaging Toolbox (segmentMedicalImage, groundTruthMedical); look up the doc page
Displaying or viewing a 2-D image: imshow, imageshow, viewer2d, montage, mask overlay, annotation, colorbar, colormap, figure exportmatlab-display-image
Displaying a 3-D volume: volshow, viewer3d, isosurface rendering, slice viewer, orthogonal viewsmatlab-display-volume
Hyperspectral or multispectral cubes, spectral indices, unmixing, ENVI, band selectionmatlab-analyze-spectral-images
Calling a PyTorch or ONNX vision model from MATLABmatlab-integrate-pytorch-vision
OCR, text recognition, reading text out of an imagematlab-recognize-text (requires Computer Vision Toolbox)
Factory-floor visual inspection: anomaly detection, defect/scratch/dent detection, gauging, pass/fail countingmatlab-use-visual-inspection (requires Visual Inspection Toolbox, R2026b+)
Barcode or QR reading, camera calibrationNo skill covers this — Computer Vision Toolbox (readBarcode, estimateCameraParameters); look up the doc page
Optical system design: lens import, prescription-side MTF, spot diagrams, ray tracing, tolerance analysismatlab-model-optics
In scope: pick the reference
If the task or the user's words involve...Illustrative functionsLoad
inspect, understand, characterize, explore data, look at my images, what kind of image, what am I working with, what's in this folder, describe the data, summarize the dataset, what format, bit depth, class and range, indexed image, alpha channel, metadata, file info, dataset characterization, before I start processingimfinfo, imhist, countEachLabelreferences/image-understanding.md
read, write, load, save, import, export, open an image, convert format, batch, folder of images, TIFF, PNG, JPEG, RAW, HDR, test patternimread, imwrite, imageDatastore, hdrreadreferences/image-io.md
video, footage, frames, read video, write video, VideoReader, VideoWriter, frame rate, video codec, MP4, AVIVideoReader, VideoWriter, readFrame, hasFrame, writeVideoreferences/video-io.md
smooth, blur an image, sharpen, denoise, noise, grainy, filter, kernel, padding, edge-preserving, bilateral, non-local means, frequency filterimgaussfilt, medfilt2, imsharpen, imbilatfilt, imnlmfilt, imdiffusefilt, imfilter, ordfilt2, locallapfilt, wiener2references/filtering.md
contrast, brighten, darken, too dark, washed out, enhance, improve, histogram, equalize, CLAHE, normalize, rescale, blend, arithmetic, type convert, flat-field, illumination correction, vignetting, uneven lightingimadjust, stretchlim, adapthisteq, histeq, rescale, mat2gray, im2single, imadd, imlincomb, imblend, imflatfieldreferences/preprocessing.md
erode, dilate, open, close, structuring element, 2-D morphology, fill holes, remove small objects, skeletonize, thin, tidy up a binary resultstrel, imerode, imdilate, imopen, imclose, imfill, bwareaopen, bwskel, imtophat, imreconstructreferences/morphological-ops.md
deblur, blurry, unfocused, out of focus, motion blur, camera shake, restore a blurred image, deconvolution, PSF estimationdeconvblind, deconvlucy, deconvwnr, deconvregreferences/deblurring.md
resize, rotate, crop, warp, scale, flip, straighten, affine, projective, perspective, undistort, lens distortion, spatial referencing, world coordinates, voxel coordinates, pixel coordinates, coordinate conversionimresize, imrotate, imcrop, imwarp, imtranslate, affinetform2d, imref2dreferences/geometric-transforms.md
align two images, stitch, mosaic, panorama, overlapping shots, seam, motion correction, multi-modal alignment, control points, non-rigid, deformable, homography (feature-based requires Computer Vision Toolbox)imregister, imregtform, imregdemons, imregcorr, detectSIFTFeatures, matchFeatures, estgeotform2d (last three: Computer Vision Toolbox), imregdeform (Medical Imaging Toolbox)references/registration.md
segment or threshold with no user input: binarize, Otsu, watershed, k-means or colour clustering, imsegsam automatic mode, deep-learning segmentationimbinarize, graythresh, multithresh, adaptthresh, watershed, imsegkmeans, imsegkmeans3, imsegisodata, imsegsamreferences/segmentation-automated.md
segment with user input: clicks, points, boxes, scribbles, seeds, prompted SAM, grabcut, active contours, region growing, lazy snapping, superpixels, App Designer segmentation UIssegmentAnythingModel, grabcut, activecontour, lazysnapping, imsegfmm, grayconnected, superpixelsreferences/segmentation-interactive.md
measure, count objects, how many, area, perimeter, centroid, size, shape, width, thickness, gap, diameter, Feret, orientation, detect edges, circles, lines, boundary tracing, ROI, world units, metrology, region, region properties, analysisregionprops, bwconncomp, bwareafilt, bwpropfilt, bwlabel, edge, imfindcircles, hough, bwboundariesreferences/analysis-measurement.md
quality, PSNR, SSIM, sharpness, noise level, is it blurry, compare two images, difference, before/after, MTF (camera/chart SFR), test chart, artifact (register first — full-reference metrics need pixel correspondence)psnr, ssim, multissim, immse, brisque, niqe, piqe, measureSharpness, esfrChartreferences/quality-assessment.md
texture, repetitive pattern, frequency domain, Fourier, FFT, DCT, Radon, Gabor, co-occurrence, GLCMgraycomatrix, graycoprops, entropyfilt, fft2, dct2, radon, gaborreferences/texture-transforms.md
color space, LAB, HSV, hue, saturation, white balance, color cast, color correction, color balance, ICC profile, grayscale conversionrgb2lab, lab2rgb, rgb2hsv, chromadapt, illumwhite, makecform, deltaE, decorrstretchreferences/color-processing.md
3-D array, volume, volumetric, voxel, slice stack, z-stack — filtering, 3-D morphology, 3-D transforms, or measurement applied to a volumeimgaussfilt3, medfilt3, imresize3, regionprops3, bwvolumefilt (R2026b), bwpropfilt3 (R2026b)references/3d-volume-processing.md
deep learning, neural network, train, U-Net, semantic segmentation networks, classify images, pretrained model, transfer learning, fine-tune, inference, augment, training dataset (requires DLT, often CVT)unet, deeplabv3plus, trainnet, semanticseg, imageDataAugmenter, augmentedImageDatastorereferences/deep-learning-imaging.md
build your own image app or GUI: sliders, live preview, parameter tuning, ROI widgetsuifigure, uigridlayout, uislider, drawrectanglereferences/app-building.md
DICOM, NIfTI, NRRD, DICOM-RT, patient metadata, anonymizing, medicalVolume, medicalImage, reading/writing medical formats, extractSlice, volume orientation, medical spatial referencingmedicalVolume, medicalImage, dicomreadVolume, dicomCollection, extractSlice, dicomContours, dicomanonreferences/medical-data-io.md
gigapixel, whole-slide, out-of-memory images, blockedImage, tiled workflows, image pyramids, .svs, .ndpi, .czi, TIFF > 10,000 px, parallel image processing, custom adaptersblockedImage, apply, getBlock, getRegion, selectBlockLocations, blockedImageDatastorereferences/large-image-processing.md

Toolbox Detection

When unsure what's available, check installed toolboxes:

hasIPT = ~isempty(ver('images'));
hasCVT = ~isempty(ver('vision'));
hasDLT = ~isempty(ver('nnet'));
hasMIT = ~isempty(ver('medical'));
hasWavelet = ~isempty(ver('wavelet'));

Most functions in this skill require Image Processing Toolbox. The deep-learning-imaging reference also requires Deep Learning Toolbox. Feature-based registration (detectSIFTFeatures, matchFeatures, estgeotform2d) requires Computer Vision Toolbox.

Release-Gated Features

This skill targets R2024b+ but some features require newer releases. References annotate these with the required release. Key ones:

  • R2024b: imageshow, imblend, viewer2d

  • R2026a: uidraw, uipaint, linkviewers

  • R2026b: bwvolumefilt, bwpropfilt3, uiannotate

If targeting an older release, check with isMATLABReleaseOlderThan("R2026a") and use the fallback patterns documented in the references. To check release notes for a specific function, load matlab-read-documentation.


Copyright 2026 The MathWorks, Inc.


Files

references/image-understanding.md

Image Data Understanding

Guidance for characterizing image data using MATLAB before designing a processing pipeline. Always inspect the data first — never guess at image properties or resort to external tools (Python, shell commands) for tasks MATLAB handles natively.

Often loaded with: image-io.md (reading files), then task-specific references based on what data inspection reveals.

Critical Rule

ALL image inspection and characterization MUST be done in MATLAB. Do NOT:

  • Write Python scripts (PIL, OpenCV, numpy) to read or inspect images

  • Use shell commands (file, identify, exiftool, ffprobe)

  • Call external tools or libraries for format detection, metadata, or pixel statistics

MATLAB provides imfinfo, imread, imageDatastore, and the functions below for every characterization task. There is never a reason to leave MATLAB for image understanding.

File Format Triage

Before processing, determine what kind of image file you're dealing with. This is critical because several format families require dedicated read functions — imread will fail or produce wrong results on them.

function fileType = triageImageFile(filename)
    % Determine file type and recommend read path
    %
    % Returns: "dicom", "raw", or "standard"

    % 1. Check for DICOM (files often have no extension)
    try
        info = dicominfo(filename);
        fprintf("DICOM file detected — Modality: %s, Patient: %s\n", ...
            info.Modality, info.PatientName.FamilyName);
        fileType = "dicom";
        return
    catch
        % Not DICOM — continue
    end

    % 2. Check for camera RAW by extension
    [~, ~, ext] = fileparts(filename);
    rawExts = [".nef",".cr2",".cr3",".crw",".arw",".dng",".orf",".rw2",".raf",".srw",".pef"];
    if any(strcmpi(ext, rawExts))
        info = rawinfo(filename);
        fprintf("Camera RAW file — Sensor: %s, CFA size: %s\n", ...
            info.CFASensorType, mat2str(info.CFAImageSize));
        fileType = "raw";
        return
    end

    % 3. Standard image format
    fileType = "standard";
end
Routing Based on File Type
Detected typeRead withNext step
"dicom"dicomread + dicominfo (IPT) or medicalImage (Medical Imaging Toolbox)See references/medical-data-io.md
"raw"rawread then demosaicSee image-io.md Camera RAW section
"standard"imreadContinue with characterization below
Other Specialized Formats (not handled by imread)
Extension / FormatRead withNotes
.nii, .nii.gz (NIfTI)niftiread + niftiinfoMedical/neuro volumes — see references/medical-data-io.md
.nrrd, .nhdr (NRRD)nrrdread + nrrdinfoMedical/scientific volumes — see references/medical-data-io.md
.exr (OpenEXR)exrreadHDR/VFX — float data, often linear color space
.fits, .fts (FITS)fitsread + fitsinfoAstronomy — may be multi-extension
.dpx (DPX)dpxread + dpxinfoFilm/broadcast — 10-bit log encoding
Interfile (.hdr + .img)interfileread + interfileinfoNuclear medicine (SPECT/PET)
Multi-frame TIFF volumestiffreadVolume3-D stacks stored as multi-page TIFF
Detecting DICOM in a Folder

DICOM files often have no extension or use .dcm, .ima, or numbered names. When inspecting a folder of unknown files:

files = dir(fullfile(folder, "*"));
files = files(~[files.isdir]);

isDicom = false(numel(files), 1);
for i = 1:numel(files)
    try
        dicominfo(fullfile(folder, files(i).name));
        isDicom(i) = true;
    catch
        % Not DICOM
    end
end

if any(isDicom)
    fprintf("%d of %d files are DICOM — use medicalVolume or dicomreadVolume\n", ...
        nnz(isDicom), numel(files));
end

Quick Characterization — Single Image

% File-level metadata (no pixel loading)
info = imfinfo("image.tif");
fprintf("Format: %s\n", info.Format);
fprintf("Dimensions: %d x %d\n", info.Width, info.Height);
fprintf("Color type: %s\n", info.ColorType);   % "truecolor", "grayscale", "indexed"
fprintf("Bit depth: %d\n", info.BitDepth);
fprintf("File size: %.1f KB\n", info.FileSize/1024);

% STOP: Check dimensions before loading pixels
if info.Width * info.Height > 100e6
    % Large image (>100 MP) — see references/large-image-processing.md
    return
end

% Pixel-level characterization (requires loading)
img = imread("image.tif");
fprintf("Class: %s\n", class(img));
fprintf("Size: %s\n", mat2str(size(img)));
fprintf("Data range: [%g, %g]\n", min(img(:)), max(img(:)));
Key imfinfo Fields
FieldWhat it tells you
ColorType"truecolor", "grayscale", or "indexed"
BitDepthBits per pixel (8, 16, 24, 48, etc.)
ColormapNon-empty if indexed image — the palette
Transparency"none", "alpha", or "simple" (PNG)
BitsPerSampleBits per channel (TIFF)
SamplesPerPixelNumber of channels (TIFF) — 4 may indicate alpha
NumberOfFramesMulti-frame file count (GIF)
SignificantBitsActual significant bits (PNG — rare but definitive)

Multi-frame files: numel(imfinfo("stack.tif")) gives the frame count without loading pixels.

Detecting Indexed Images

info = imfinfo(filename);
if strcmp(info.ColorType, "indexed")
    [X, map] = imread(filename);  % X = index matrix, map = Nx3 colormap
    fprintf("Palette size: %d colors\n", size(map, 1));
    % Convert to RGB for processing
    rgbImg = ind2rgb(X, map);     % Returns double [0,1]
end

Common mistake: Reading an indexed image with img = imread(...) (one output) silently returns the index matrix. It looks like a grayscale image but the pixel values are palette indices, not intensities.

Detecting Effective Bit Depth

Sensors often produce 10-bit or 12-bit data stored in a 16-bit container. The file says "16-bit" but data only uses a fraction of the range.

img = imread(filename);
if isa(img, "uint16")
    maxVal = double(max(img(:)));
    sensorBits = [10 12 14 16];
    effectiveBits = sensorBits(find(maxVal <= 2.^sensorBits - 1, 1));
    fprintf("Stored as 16-bit, effective depth: %d-bit (max value: %d)\n", ...
        effectiveBits, maxVal);
end

Caveat: This heuristic reports the minimum bit depth that could contain the max value. An underexposed 16-bit image may report as "12-bit" simply because pixel values are low. Confirm with the camera/sensor spec when possible.

Display Implications

Use imageshow with the appropriate DisplayRangeMode for correct visualization:

imageshow(img, DisplayRangeMode="12-bit");   % Range [0, 4095]
imageshow(img, DisplayRangeMode="10-bit");   % Range [0, 1023]
imageshow(img, DisplayRangeMode="data-range"); % Range [min(img), max(img)]

Do NOT use DisplayRangeMode="type-range" (the default) for sub-range data — the image will appear very dark because values occupy only a small fraction of [0, 65535].

Detecting Alpha Channels

% PNG: alpha is the third output of imread
info = imfinfo(filename);
hasAlpha = ~strcmp(info.Transparency, "none");
[img, ~, alpha] = imread(filename);  % alpha is non-empty if PNG has transparency

% TIFF: alpha is packed into the image array (M×N×4), third output is always empty
% SamplesPerPixel == 4 could be RGBA or CMYK — check ColorType to distinguish
info = imfinfo(filename);
if info.SamplesPerPixel == 4 && strcmp(info.ColorType, "truecolor")
    data = imread(filename);          % M×N×4
    img   = data(:,:,1:3);            % RGB
    alpha = data(:,:,4);              % Alpha
end

Caution: The three-output form [img, ~, alpha] = imread(...) returns alpha only for PNG, CUR, and ICO. For TIFF, it always returns empty — use the approach above instead.

Detecting Grayscale Stored as RGB

Some pipelines save grayscale images as 3-channel RGB where all channels are identical. This wastes memory 3x.

if size(img, 3) == 3
    if isequal(img(:,:,1), img(:,:,2)) && isequal(img(:,:,2), img(:,:,3))
        img = img(:,:,1);  % Collapse to true grayscale
        fprintf("Converted gray-as-RGB to grayscale (3x memory saved)\n");
    end
end

Dataset Characterization — Folder of Images

When asked to look at, understand, explore, describe, or characterize images in a folder, use this imageDatastore pattern. Do not use dir to list files and inspect them ad hoc — imageDatastore handles format detection, subfolder traversal, and class labeling automatically:

dataFolder = "path/to/Data";
imds = imageDatastore(dataFolder, IncludeSubfolders=true, ...
    LabelSource="foldernames");

%% 1. Dataset overview
fprintf("=== Dataset Overview ===\n");
fprintf("Total images: %d\n", numel(imds.Files));
fprintf("Classes:\n");
disp(countEachLabel(imds));

%% 2. File format distribution
exts = strings(numel(imds.Files), 1);
for i = 1:numel(imds.Files)
    [~, ~, exts(i)] = fileparts(imds.Files{i});
end
[uniqueExts, ~, idx] = unique(exts);
counts = accumarray(idx, 1);
fprintf("Formats: ");
for i = 1:numel(uniqueExts)
    fprintf("%s (%d)  ", uniqueExts(i), counts(i));
end
fprintf("\n");

%% 3. Sample characterization — read a few to understand properties
numSamples = min(10, numel(imds.Files));
sampleIdx = round(linspace(1, numel(imds.Files), numSamples));
fprintf("\n=== Sample Characterization ===\n");
for i = sampleIdx
    info = imfinfo(imds.Files{i});
    info = info(1);
    fprintf("  %s: %dx%d, %s, %d-bit\n", ...
        info.Format, info.Width, info.Height, info.ColorType, info.BitDepth);
end

%% 4. Pixel statistics from samples
fprintf("\n=== Pixel Statistics ===\n");
for i = sampleIdx(1:min(3,end))
    img = imread(imds.Files{i});
    fprintf("  %s — class: %s, range: [%g, %g]\n", ...
        imds.Files{i}, class(img), min(img(:)), max(img(:)));
end

%% 5. Visual overview — show sample grid
montage(imds, Indices=sampleIdx, ThumbnailSize=[128 128]);

Data Quality Checks

CheckCodeWhat it detects
Constant imagemax(img(:)) == min(img(:))Blank/corrupted files
Clipped highlightsnnz(img == intmax(class(img))) / numel(img) > 0.01Overexposed regions
Clipped shadowsnnz(img == 0) / numel(img) > 0.05Underexposed regions
Near-empty dynamic range(double(max(img(:))) - double(min(img(:)))) < 0.1 * double(intmax(class(img)))Low contrast
Size inconsistencyCompare size(imread(...)) across samplesMixed resolutions
Corrupt filestry/catch around imreadUnreadable files
% Quick corruption check
badFiles = {};
for i = 1:numel(imds.Files)
    try
        imread(imds.Files{i});
    catch
        badFiles{end+1} = imds.Files{i}; %#ok<SAGROW>
    end
end
if ~isempty(badFiles)
    fprintf("Found %d corrupt files\n", numel(badFiles));
end

Content-Level Characterization

Do NOT run all subsections below. Select only the 1–2 analyses relevant to your specific task. Run on a single representative image, not the entire dataset.

Noise Estimation

Estimate noise standard deviation to decide whether denoising is needed. Two methods are available depending on installed toolboxes:

gray = im2single(im2gray(img));

if ~isempty(ver("wavelet"))
    % Wavelet MAD estimator (more accurate, requires Wavelet Toolbox)
    [~, ~, ~, d] = dwt2(gray, "haar");         % Single-level 2-D DWT, diagonal detail
    noiseStd = median(abs(d(:))) / 0.6745;     % Robust MAD estimator
else
    % Laplacian-based estimator (Immerkaer 1996, IPT only)
    kern = [1 -2 1; -2 4 -2; 1 -2 1];
    filtered = imfilter(gray, kern, "symmetric");
    [H, W] = size(gray);
    noiseStd = sqrt(0.5*pi) / (6*W*H) * sum(abs(filtered(:)));
end
fprintf("Estimated noise std: %.4f\n", noiseStd);

Decision guidance (for single [0,1] range):

Noise stdInterpretationRecommended action
< 0.01Clean imageNo denoising needed
0.01–0.05Moderate noiseimgaussfilt or imnlmfilt
0.05–0.10Heavy noiseimnlmfilt with higher DegreeOfSmoothing
> 0.10Severe noiseConsider medfilt2 (if salt & pepper) or multiple passes

See filtering.md for denoising function details.

Histogram Shape — Segmentation Planning

The histogram shape directly determines which segmentation approach to use:

gray = im2gray(img);
[counts, binLocs] = imhist(gray);

% Compute number of significant peaks (modes)
smoothCounts = smoothdata(double(counts), "gaussian", 7);  % 1-D smoothing
peaks = islocalmax(smoothCounts, MinProminence=max(smoothCounts)*0.05);
numModes = nnz(peaks);
fprintf("Histogram modes: %d\n", numModes);

Decision guidance:

ModesHistogram shapeSegmentation approach
2Bimodal (clear foreground/background)imbinarize (global Otsu)
2 (but uneven peak heights)Bimodal with uneven illuminationimbinarize("adaptive")
3–5Multimodalmultithresh + imquantize
1 or smoothUnimodal/flatClustering (imsegkmeans) or edge-based methods
Blur / Sharpness Estimation

Estimate whether the image is in focus using Laplacian variance:

gray = im2double(im2gray(img));
lap = imfilter(gray, fspecial("laplacian", 0), "replicate");
sharpness = var(lap(:));
fprintf("Sharpness (Laplacian variance): %.6f\n", sharpness);

Decision guidance:

SharpnessInterpretationAction
> 0.01Sharp / in focusNo deblurring needed
0.001–0.01Mild blurConsider imsharpen
< 0.001Significantly blurredDeblurring needed — see deblurring.md

Note: These thresholds are image-dependent. Compare across a dataset to establish baseline, or use no-reference quality metrics (niqe, brisque) for a more robust assessment.

Illumination Uniformity

Detect uneven illumination that would break global thresholding:

gray = im2double(im2gray(img));

% Estimate background with large Gaussian
background = imgaussfilt(gray, 50);

% Measure variation in background
bgRange = max(background(:)) - min(background(:));
bgStd = std(background(:));
fprintf("Background range: %.3f, std: %.3f\n", bgRange, bgStd);

Decision guidance:

Background stdInterpretationRecommended correction
< 0.05Uniform illuminationGlobal methods work fine
0.05–0.15Moderate unevennessimflatfield or adaptive thresholding
> 0.15Severe unevennessimflatfield before any segmentation

See preprocessing.md for imflatfield usage details.

Foreground/Background Structure

Characterize objects in the image to inform segmentation strategy:

% Quick binary estimate
bw = imbinarize(im2gray(img));
bw = bwareaopen(bw, 20);  % Remove tiny specks
cc = bwconncomp(bw);

fprintf("Estimated objects: %d\n", cc.NumObjects);

if cc.NumObjects > 0
    props = regionprops("table", cc, "Area", "Eccentricity", "Solidity");
    fprintf("Area range: [%d, %d] pixels\n", min(props.Area), max(props.Area));
    fprintf("Median eccentricity: %.2f\n", median(props.Eccentricity));
    fprintf("Median solidity: %.2f\n", median(props.Solidity));

    % Detect touching objects (low solidity + large area suggests merged blobs)
    if any(props.Solidity < 0.7 & props.Area > median(props.Area)*3)
        fprintf("Warning: likely touching/overlapping objects — consider watershed\n");
    end
end

Decision guidance:

ObservationSuggests
Few large objects, high soliditySimple thresholding sufficient
Many small objects, uniform sizeParticle analysis — bwareaopen + regionprops
Low solidity on large regionsTouching objects — need watershed or SAM
Very few objects detectedThreshold may be wrong, or image needs preprocessing first
Objects vary wildly in sizeMay need multi-scale approach or size-based filtering
Content Type Heuristics

Identify what kind of image you're dealing with to select appropriate processing:

gray = im2gray(img);
[h, w, c] = size(img);

% Compute diagnostic features
edgeDensity = nnz(edge(gray, "Canny")) / numel(gray);
if isinteger(gray)
    uniqueColors = numel(unique(gray(:))) / double(intmax(class(gray)));
    entropy_val = entropy(gray);
else
    uniqueColors = numel(unique(gray(:))) / numel(gray);
    entropy_val = entropy(im2uint8(mat2gray(gray)));
end

fprintf("Edge density: %.3f\n", edgeDensity);
fprintf("Color utilization: %.1f%%\n", uniqueColors * 100);
fprintf("Entropy: %.2f\n", entropy_val);

Characteristic signatures:

Content typeEdge densityEntropyDisambiguating clues
Natural photo0.05–0.156–7.5RGB, moderate size (1–20 MP), smooth gradients
Document/scan0.01–0.052–5Bimodal histogram, very high contrast, often grayscale
Microscopy0.05–0.205–7uint16, many small objects, uneven illumination, often grayscale
Satellite/aerial0.10–0.206–7.5Large image (>2000×2000), multi-band, regular texture
Synthetic/rendered0.02–0.104–6Very low noise (std < 0.005), perfectly sharp edges, flat regions
Medical (CT/MRI)0.03–0.104–6int16, narrow dynamic range, often 3-D or DICOM source

When ranges overlap, use the "Disambiguating clues" column — check data type, image dimensions, and file source metadata to resolve.

Processing implications:

Content typeTypical pipeline
Natural photoColor correction → denoising → enhancement
Document/scanBinarization → morphological cleanup → OCR
MicroscopyFlat-field correction → denoising → segmentation → measurement
SatelliteNormalization → index computation → classification
MedicalWindow/level → segmentation → volumetric analysis

Summarize Before Processing

After characterizing data, summarize findings before proposing a pipeline. Include both file-level and content-level observations:

fprintf("\n=== Summary ===\n");
fprintf("Format: %s, %dx%d, %s, %d-bit\n", format, width, height, colorType, bitDepth);
fprintf("Data class: %s, range: [%g, %g]\n", dataClass, minVal, maxVal);
fprintf("Sharpness: %.5f | BG uniformity: %.3f\n", sharpness, bgStd);
fprintf("Histogram modes: %d | Objects detected: %d\n", numModes, numObjects);

% Actionable recommendations
if sharpness < 0.001, fprintf("→ Image is blurred — consider deblurring\n"); end
if bgStd > 0.05,   fprintf("→ Uneven illumination — apply imflatfield\n"); end
if numModes == 2,   fprintf("→ Bimodal histogram — imbinarize should work well\n"); end

Common Mistakes

MistakeWhy it's wrongCorrect approach
Using imshow for displayLacks DisplayRangeMode, OverlayData, and modern featuresUse imageshow (R2024b+)
Using Python/shell to inspect imagesMATLAB has imfinfo, imread, imageDatastoreUse MATLAB functions shown above
Assuming all images are uint8 RGBData may be uint16, indexed, grayscale, or floatCheck imfinfo and class(imread(...))
Displaying 12-bit data with default rangeImage appears very dark (values << 65535)Use DisplayRangeMode="12-bit"
Reading indexed image with one outputGets index matrix, not true colorUse [X, map] = imread(...) then ind2rgb
Loading all images to check propertiesWastes time and memoryUse imfinfo for metadata (no pixel load)
Not checking for alpha channelAlpha ignored, transparency lostUse 3-output imread or check imfinfo.Transparency
Assuming consistent resolutionDataset may have mixed sizesSample multiple files before batch processing
imhist on float data outside [0,1]Silently clips to [0,1] — bins cover only 0–1 even if data ranges to 5.0Use histogram(img(:)) for arbitrary-range float data

Copyright 2026 The MathWorks, Inc.


Requirements and compatibility

Works with: Claude Code, Claude apps, OpenAI Codex, GitHub Copilot, Cursor, Gemini CLI, Kiro, OpenCode, Goose, Amp, Roo Code, JetBrains Junie, Factory Droid, Devin (ex-Windsurf), Mistral Vibe, OpenHands, Letta

“Works with” means the tool reads the open Agent Skills format; a skill written for one tool may use features others lack.

Security

No issues found by our static scan· No issues found

A static scan is not a guarantee. Skills are third-party content: review a skill before you let an agent use it.

Attribution and licence

Created by
MathWorks
Publisher
MathWorks (Official)
Source
matlab/matlab-agentic-toolkit · skills-catalog/image-processing-and-computer-vision/matlab-process-images · commit 2a7f215
Licence
BSD 2-Clause License (redistribution allowed)
  • LICENSE (added by AutoGineer from LICENSE.md)
  • ATTRIBUTION.md (written by AutoGineer)
Synced

AutoGineer is not affiliated with or endorsed by MathWorks or MathWorks.

Download ZIP

The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download matlab-process-images.zip