Matlab Process Images
MathWorksOfficialCreated by MathWorks
- Skill
- Tool integration
- Software development
- BSD-2-Clause
- No issues found
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
Download the skill as a ZIP:
https://mp-staging.autogineer.com/agent-skills/matlab/matlab-process-images/downloadUnzip 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
| Type | Range | When to use |
|---|---|---|
uint8 | 0–255 | Display, file I/O, memory-efficient storage |
uint16 | 0–65535 | High dynamic range sensors, medical. Camera RAW is n-bit in a uint16 container — rescale with rawinfo BlackLevel/WhiteLevel, not 65535 |
single / double | 0.0–1.0 | Arithmetic, filtering, computation |
logical | 0 or 1 | Binary 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,regionpropscentroids)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
imref2dandimref3dcan 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×NorM×N×1RGB:
M×N×3With 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.mdfirst — 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, loadmatlab-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; radiomics | No 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 export | matlab-display-image |
Displaying a 3-D volume: volshow, viewer3d, isosurface rendering, slice viewer, orthogonal views | matlab-display-volume |
| Hyperspectral or multispectral cubes, spectral indices, unmixing, ENVI, band selection | matlab-analyze-spectral-images |
| Calling a PyTorch or ONNX vision model from MATLAB | matlab-integrate-pytorch-vision |
| OCR, text recognition, reading text out of an image | matlab-recognize-text (requires Computer Vision Toolbox) |
| Factory-floor visual inspection: anomaly detection, defect/scratch/dent detection, gauging, pass/fail counting | matlab-use-visual-inspection (requires Visual Inspection Toolbox, R2026b+) |
| Barcode or QR reading, camera calibration | No 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 analysis | matlab-model-optics |
In scope: pick the reference
| If the task or the user's words involve... | Illustrative functions | Load |
|---|---|---|
| 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 processing | imfinfo, imhist, countEachLabel | references/image-understanding.md |
| read, write, load, save, import, export, open an image, convert format, batch, folder of images, TIFF, PNG, JPEG, RAW, HDR, test pattern | imread, imwrite, imageDatastore, hdrread | references/image-io.md |
| video, footage, frames, read video, write video, VideoReader, VideoWriter, frame rate, video codec, MP4, AVI | VideoReader, VideoWriter, readFrame, hasFrame, writeVideo | references/video-io.md |
| smooth, blur an image, sharpen, denoise, noise, grainy, filter, kernel, padding, edge-preserving, bilateral, non-local means, frequency filter | imgaussfilt, medfilt2, imsharpen, imbilatfilt, imnlmfilt, imdiffusefilt, imfilter, ordfilt2, locallapfilt, wiener2 | references/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 lighting | imadjust, stretchlim, adapthisteq, histeq, rescale, mat2gray, im2single, imadd, imlincomb, imblend, imflatfield | references/preprocessing.md |
| erode, dilate, open, close, structuring element, 2-D morphology, fill holes, remove small objects, skeletonize, thin, tidy up a binary result | strel, imerode, imdilate, imopen, imclose, imfill, bwareaopen, bwskel, imtophat, imreconstruct | references/morphological-ops.md |
| deblur, blurry, unfocused, out of focus, motion blur, camera shake, restore a blurred image, deconvolution, PSF estimation | deconvblind, deconvlucy, deconvwnr, deconvreg | references/deblurring.md |
| resize, rotate, crop, warp, scale, flip, straighten, affine, projective, perspective, undistort, lens distortion, spatial referencing, world coordinates, voxel coordinates, pixel coordinates, coordinate conversion | imresize, imrotate, imcrop, imwarp, imtranslate, affinetform2d, imref2d | references/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 segmentation | imbinarize, graythresh, multithresh, adaptthresh, watershed, imsegkmeans, imsegkmeans3, imsegisodata, imsegsam | references/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 UIs | segmentAnythingModel, grabcut, activecontour, lazysnapping, imsegfmm, grayconnected, superpixels | references/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, analysis | regionprops, bwconncomp, bwareafilt, bwpropfilt, bwlabel, edge, imfindcircles, hough, bwboundaries | references/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, esfrChart | references/quality-assessment.md |
| texture, repetitive pattern, frequency domain, Fourier, FFT, DCT, Radon, Gabor, co-occurrence, GLCM | graycomatrix, graycoprops, entropyfilt, fft2, dct2, radon, gabor | references/texture-transforms.md |
| color space, LAB, HSV, hue, saturation, white balance, color cast, color correction, color balance, ICC profile, grayscale conversion | rgb2lab, lab2rgb, rgb2hsv, chromadapt, illumwhite, makecform, deltaE, decorrstretch | references/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 volume | imgaussfilt3, 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, augmentedImageDatastore | references/deep-learning-imaging.md |
| build your own image app or GUI: sliders, live preview, parameter tuning, ROI widgets | uifigure, uigridlayout, uislider, drawrectangle | references/app-building.md |
DICOM, NIfTI, NRRD, DICOM-RT, patient metadata, anonymizing, medicalVolume, medicalImage, reading/writing medical formats, extractSlice, volume orientation, medical spatial referencing | medicalVolume, medicalImage, dicomreadVolume, dicomCollection, extractSlice, dicomContours, dicomanon | references/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 adapters | blockedImage, apply, getBlock, getRegion, selectBlockLocations, blockedImageDatastore | references/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,viewer2dR2026a:
uidraw,uipaint,linkviewersR2026b:
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
- ATTRIBUTION.mdAdded by AutoGineer1.1 KiB
- LICENSELicence file added by AutoGineer1.4 KiB
- SKILL.md15.8 KiB
- manifest.yaml811 B
- references/3d-volume-processing.md20.2 KiB
- references/analysis-measurement.md18.5 KiB
- references/app-building.md16.9 KiB
- references/blocked-3d-volumes.md9.9 KiB
- references/blocked-data-discovery.md22.1 KiB
- references/blocked-geospatial.md3.2 KiB
- references/blocked-specialized-workflows.md4.3 KiB
- references/blocked-training-and-labeling.md7.0 KiB
- references/blocked-visualization.md5.1 KiB
- references/blocked-whole-slide-images.md3.1 KiB
- references/color-processing.md9.0 KiB
- references/deblurring.md8.1 KiB
- references/deep-learning-imaging.md14.8 KiB
- references/dicom-anonymization.md3.3 KiB
- references/dicom-rt-workflows.md7.4 KiB
- references/filtering.md21.8 KiB
- references/geometric-transforms.md14.3 KiB
- references/image-io.md15.5 KiB
- references/image-understanding.md20.0 KiB
- references/ipt-reading-writing.md4.8 KiB
- references/large-image-processing.md12.3 KiB
- references/medical-data-io.md8.3 KiB
- references/mit-medical-volume.md8.2 KiB
- references/mit-spatial-referencing.md5.9 KiB
- references/morphological-ops.md18.0 KiB
- references/preprocessing.md14.4 KiB
- references/quality-assessment.md13.5 KiB
- references/registration.md24.7 KiB
- references/segmentation-automated.md9.7 KiB
- references/segmentation-interactive.md15.3 KiB
- references/texture-transforms.md10.4 KiB
- references/video-io.md3.4 KiB
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";
endRouting Based on File Type
| Detected type | Read with | Next step |
|---|---|---|
"dicom" | dicomread + dicominfo (IPT) or medicalImage (Medical Imaging Toolbox) | See references/medical-data-io.md |
"raw" | rawread then demosaic | See image-io.md Camera RAW section |
"standard" | imread | Continue with characterization below |
Other Specialized Formats (not handled by imread)
| Extension / Format | Read with | Notes |
|---|---|---|
.nii, .nii.gz (NIfTI) | niftiread + niftiinfo | Medical/neuro volumes — see references/medical-data-io.md |
.nrrd, .nhdr (NRRD) | nrrdread + nrrdinfo | Medical/scientific volumes — see references/medical-data-io.md |
.exr (OpenEXR) | exrread | HDR/VFX — float data, often linear color space |
.fits, .fts (FITS) | fitsread + fitsinfo | Astronomy — may be multi-extension |
.dpx (DPX) | dpxread + dpxinfo | Film/broadcast — 10-bit log encoding |
Interfile (.hdr + .img) | interfileread + interfileinfo | Nuclear medicine (SPECT/PET) |
| Multi-frame TIFF volumes | tiffreadVolume | 3-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));
endQuick 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
| Field | What it tells you |
|---|---|
ColorType | "truecolor", "grayscale", or "indexed" |
BitDepth | Bits per pixel (8, 16, 24, 48, etc.) |
Colormap | Non-empty if indexed image — the palette |
Transparency | "none", "alpha", or "simple" (PNG) |
BitsPerSample | Bits per channel (TIFF) |
SamplesPerPixel | Number of channels (TIFF) — 4 may indicate alpha |
NumberOfFrames | Multi-frame file count (GIF) |
SignificantBits | Actual 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]
endCommon 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);
endCaveat: 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
endCaution: 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
endDataset 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
| Check | Code | What it detects |
|---|---|---|
| Constant image | max(img(:)) == min(img(:)) | Blank/corrupted files |
| Clipped highlights | nnz(img == intmax(class(img))) / numel(img) > 0.01 | Overexposed regions |
| Clipped shadows | nnz(img == 0) / numel(img) > 0.05 | Underexposed regions |
| Near-empty dynamic range | (double(max(img(:))) - double(min(img(:)))) < 0.1 * double(intmax(class(img))) | Low contrast |
| Size inconsistency | Compare size(imread(...)) across samples | Mixed resolutions |
| Corrupt files | try/catch around imread | Unreadable 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));
endContent-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 std | Interpretation | Recommended action |
|---|---|---|
| < 0.01 | Clean image | No denoising needed |
| 0.01–0.05 | Moderate noise | imgaussfilt or imnlmfilt |
| 0.05–0.10 | Heavy noise | imnlmfilt with higher DegreeOfSmoothing |
| > 0.10 | Severe noise | Consider 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:
| Modes | Histogram shape | Segmentation approach |
|---|---|---|
| 2 | Bimodal (clear foreground/background) | imbinarize (global Otsu) |
| 2 (but uneven peak heights) | Bimodal with uneven illumination | imbinarize("adaptive") |
| 3–5 | Multimodal | multithresh + imquantize |
| 1 or smooth | Unimodal/flat | Clustering (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:
| Sharpness | Interpretation | Action |
|---|---|---|
| > 0.01 | Sharp / in focus | No deblurring needed |
| 0.001–0.01 | Mild blur | Consider imsharpen |
| < 0.001 | Significantly blurred | Deblurring 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 std | Interpretation | Recommended correction |
|---|---|---|
| < 0.05 | Uniform illumination | Global methods work fine |
| 0.05–0.15 | Moderate unevenness | imflatfield or adaptive thresholding |
| > 0.15 | Severe unevenness | imflatfield 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
endDecision guidance:
| Observation | Suggests |
|---|---|
| Few large objects, high solidity | Simple thresholding sufficient |
| Many small objects, uniform size | Particle analysis — bwareaopen + regionprops |
| Low solidity on large regions | Touching objects — need watershed or SAM |
| Very few objects detected | Threshold may be wrong, or image needs preprocessing first |
| Objects vary wildly in size | May 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 type | Edge density | Entropy | Disambiguating clues |
|---|---|---|---|
| Natural photo | 0.05–0.15 | 6–7.5 | RGB, moderate size (1–20 MP), smooth gradients |
| Document/scan | 0.01–0.05 | 2–5 | Bimodal histogram, very high contrast, often grayscale |
| Microscopy | 0.05–0.20 | 5–7 | uint16, many small objects, uneven illumination, often grayscale |
| Satellite/aerial | 0.10–0.20 | 6–7.5 | Large image (>2000×2000), multi-band, regular texture |
| Synthetic/rendered | 0.02–0.10 | 4–6 | Very low noise (std < 0.005), perfectly sharp edges, flat regions |
| Medical (CT/MRI) | 0.03–0.10 | 4–6 | int16, 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 type | Typical pipeline |
|---|---|
| Natural photo | Color correction → denoising → enhancement |
| Document/scan | Binarization → morphological cleanup → OCR |
| Microscopy | Flat-field correction → denoising → segmentation → measurement |
| Satellite | Normalization → index computation → classification |
| Medical | Window/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"); endCommon Mistakes
| Mistake | Why it's wrong | Correct approach |
|---|---|---|
Using imshow for display | Lacks DisplayRangeMode, OverlayData, and modern features | Use imageshow (R2024b+) |
| Using Python/shell to inspect images | MATLAB has imfinfo, imread, imageDatastore | Use MATLAB functions shown above |
| Assuming all images are uint8 RGB | Data may be uint16, indexed, grayscale, or float | Check imfinfo and class(imread(...)) |
| Displaying 12-bit data with default range | Image appears very dark (values << 65535) | Use DisplayRangeMode="12-bit" |
| Reading indexed image with one output | Gets index matrix, not true color | Use [X, map] = imread(...) then ind2rgb |
| Loading all images to check properties | Wastes time and memory | Use imfinfo for metadata (no pixel load) |
| Not checking for alpha channel | Alpha ignored, transparency lost | Use 3-output imread or check imfinfo.Transparency |
| Assuming consistent resolution | Dataset may have mixed sizes | Sample 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.0 | Use 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.
Download ZIP
The ZIP holds the skill’s files, its licence files and an ATTRIBUTION.md. Download matlab-process-images.zip