fspecial — Generate standard two-dimensional image-filter kernels.
fspecial(type, ...) creates average, disk, Gaussian, Laplacian, Laplacian-of-Gaussian, motion, Prewitt, Sobel, and unsharp correlation kernels for imfilter, filter2, and conv2.
Syntax
H = fspecial(type)
H = fspecial(type, arg1)
H = fspecial(type, arg1, arg2)Inputs
type- Type
StringScalar- Required
- Yes
- Description
- Filter name: average, disk, gaussian, laplacian, log, motion, prewitt, sobel, or unsharp.
arg1- Type
Any- Required
- No
- Description
- Filter-specific size, radius, alpha, or length.
arg1- Type
Any- Required
- No
- Description
- First filter-specific parameter.
arg2- Type
Any- Required
- No
- Description
- Second filter-specific parameter, such as sigma or motion angle.
Returns
H- Type
NumericArray- Description
- Generated 2-D correlation kernel.
Errors
RunMat:fspecial:InvalidArgument- When
- Filter name or argument count shape is invalid for the selected filter type.
- Message
fspecial: invalid argument
RunMat:fspecial:InvalidInput- When
- Filter parameters have invalid values or unsupported input types.
- Message
fspecial: invalid input
RunMat:fspecial:Internal- When
- Kernel generation or provider publication fails internally.
- Message
fspecial: internal kernel generation failure
Filter types and defaults
average and gaussian default to 3-by-3; log defaults to 5-by-5; disk defaults to radius 5 and an 11-by-11 footprint; motion defaults to length 9 and angle 0. Laplacian, Prewitt, Sobel, and unsharp are 3-by-3. Gaussian and LoG default sigma to 0.5; Laplacian alpha defaults to 0.2.
Average, Gaussian, and LoG sizes accept a positive scalar or a two-element [rows cols] vector. Disk uses a nonnegative radius; motion rounds a positive length and uses the next odd kernel width. Laplacian and unsharp accept alpha. Prewitt and Sobel accept no extra parameters.
Smoothing kernels are normalized, while derivative and sharpening kernels use their defined scaling. Invalid filter names, argument counts, dimensions, and parameter values have stable fspecial errors. RunMat-only unsharp and nondouble numeric controls require their extension gates.
Class and execution
The ordinary host result is a dense real double column-major tensor. Exact integer structural controls are decoded without binary64 rounding; integer computational parameters must be exactly representable in binary64. An ambient RUNMAT_ACCEL_FSPECIAL_DEVICE=1 request is separately extension-gated and may publish a resident provider-precision single or double kernel for supported forms (average, Gaussian, Laplacian, Prewitt, Sobel, unsharp). Disk, LoG, and motion remain host outputs.
A provider's explicit Unsupported result may use the already-computed host kernel. Operational provider failures retain their cause and never silently replay on host. Resident results must have the expected shape, real floating class, live allocation, and exact provider owner before publication. The operation is a fusion boundary; the ambient policy reads the environment and may throw.
Examples
Seven-by-seven box filter
H = fspecial('average',7); size(H)Expected output:
ans = [7 7]Rectangular Gaussian kernel
H = fspecial('gaussian',[5 7],1); size(H)Expected output:
ans = [5 7]Circular averaging kernel
H = fspecial('disk',4); size(H)Expected output:
ans = [9 9]Laplacian-of-Gaussian edge kernel
H = fspecial('log',[9 9],1.4); size(H)Expected output:
ans = [9 9]Thirty-degree motion blur
H = fspecial('motion',15,30); size(H)Expected output:
ans = [15 15]RunMat unsharp extension
H = fspecial('unsharp',0.6); size(H)Expected output:
ans = [3 3]Using fspecial with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how fspecial changes the result.
Run a small fspecial example, explain the result, then change one input and compare the output.
FAQ
Can fspecial publish a gpuArray?⌄
The separately gated RUNMAT_ACCEL_FSPECIAL_DEVICE=1 mode can publish supported kernels through an active provider. Unsupported filters or providers return the host kernel; operational provider errors do not silently fall back.
How do I specify a size?⌄
Average, Gaussian, and LoG accept a scalar side length or [rows cols]. Other filters use their documented radius, length, or alpha parameter.
Which kernels are available?⌄
Average, disk, Gaussian, Laplacian, LoG, motion, Prewitt, Sobel, and the RunMat-gated unsharp extension are implemented.
Are the kernels normalized?⌄
Average, disk, Gaussian, and motion kernels sum to one. LoG and derivative kernels instead have their defined zero-sum or sharpening scaling.
What if a parameter is invalid?⌄
Invalid names, dimensions, types, and parameter ranges raise a stable fspecial invalid-argument or invalid-input error before publication.
Are results exactly the same as MATLAB?⌄
The documented forms and defaults follow MATLAB-compatible behavior, but floating-point results may vary with implementation and provider precision; this reference does not claim bitwise or release-specific identity.
Related Image functions
Source you can read
Every RunMat function's source is public. Read exactly how fspecial is executed, line by line, in Rust.
- View the source for fspecial in Rust on GitHub
- Learn how the RunMat runtime works
- Found a bug? Open an issue with a minimal reproduction.
About RunMat
RunMat is a runtime that executes MATLAB-syntax code fast on any GPU. The runtime is free for individuals, including freelancers; students and faculty at an academic institution, for teaching or research there; contributors to an open-source project, for work on that project; organizations with 250 or fewer people and US$10M or less in annual revenue; and anyone, for non-production use. See runmat.com/license.
- RunMat automatically optimizes your math for GPU execution on Apple, Nvidia, and AMD hardware. No code changes needed. Simulations that took hours now take minutes.
- Start running code in seconds. RunMat runs in the browser, on the desktop, or from the CLI. No license server, no IT ticket.