imfilter — Apply linear filtering to an image with selectable padding and output shape.
imfilter(A,H,options...) filters real numeric or logical image data with a real double scalar, vector, or matrix kernel. Images may have trailing channel or higher dimensions, while public H remains at most two-dimensional.
Syntax
B = imfilter(A, H)
B = imfilter(A, H, options...)Inputs
A- Type
Any- Required
- Yes
- Description
- Input image/array.
H- Type
Any- Required
- Yes
- Description
- Filter kernel.
options- Type
Any- Required
- Variadic
- Description
- Optional mode/shape/padding flags and fill values (e.g. 'same', 'full', 'valid', 'replicate', 'symmetric', 'circular', 'fill', padval, 'conv', 'corr').
Returns
B- Type
NumericArray- Description
- Filtered image output.
Errors
RunMat:imfilter:InvalidInput- When
- Input/kernel tensors are invalid or cannot be converted.
- Message
imfilter: invalid input
RunMat:imfilter:InvalidOption- When
- One or more option flags or option values are invalid.
- Message
imfilter: invalid option
RunMat:imfilter:Internal- When
- Internal filtering operation fails.
- Message
imfilter: internal operation failed
Mode, padding, and shape
Correlation (corr) with zero padding and same-size output is the default. conv reverses the kernel along its axes. replicate, symmetric, and circular select border behavior; a real numeric scalar supplies constant padding, and RunMat-gated fill optionally consumes a following numeric scalar (otherwise zero). A numeric padding value can appear before or after fill.
same preserves image shape, full includes every overlap, and RunMat-gated valid keeps complete overlaps (possibly yielding a zero-length axis). Options are processed left to right. H is not normalized automatically; fspecial or explicit scaling can construct normalized smoothing kernels.
The image may have multiple channels or higher dimensions. H must be nonempty real double scalar, vector, or matrix; filtering extends it with singleton trailing axes. Unsupported 64-bit integer image classes and complex/sparse inputs have stable input errors.
Class and residency
Real single/double images preserve their class. Logical images round/clamp back to logical output. Documented int8/int16/int32/uint8/uint16/uint32 images round to nearest and saturate into the original class. Host and automatic-placement fallback accumulate in double; explicit resident uint32 uses double and other documented explicit resident integers use single accumulation.
A supported floating provider imfilter hook may return a new resident output. Integer and logical resident inputs use checked owner-aware host reference computation and explicit-input restoration. A typed unsupported provider operation may take the host fallback; operational upload or execution failures retain their cause and are not silently replayed. imfilter is a standalone fusion boundary.
Examples
Smooth with zero padding
I = [1 2;3 4]; H = ones(3)/9; J = imfilter(I,H); size(J)Expected output:
ans = [2 2]Replicate the image border
I = magic(3); H = ones(3); J = imfilter(I,H,'replicate'); size(J)Expected output:
ans = [3 3]Keep every kernel overlap
I = [1 2;3 4]; H = [1 2;3 4]; J = imfilter(I,H,'full'); size(J)Expected output:
ans = [3 3]Reverse the kernel
I = reshape(1:6,[3 2]); H = [1 2;3 4]; J = imfilter(I,H,'conv'); size(J)Expected output:
ans = [3 2]Wrap image borders
I = [1 2;3 4]; H = [0 1;1 0]; J = imfilter(I,H,'circular'); size(J)Expected output:
ans = [2 2]Complete overlaps only
I = ones(5); H = ones(3); J = imfilter(I,H,'valid'); size(J)Expected output:
ans = [3 3]Using imfilter with coding agents
Open a RunMat example with live inputs, then ask the agent to explain how imfilter changes the result.
Run a small imfilter example, explain the result, then change one input and compare the output.
FAQ
Which computation mode is the default?⌄
Correlation is the default. The conv option reverses the kernel before filtering.
What do same, full, and valid mean?⌄
Same preserves image extent, full includes every overlap, and the RunMat-gated valid form includes only complete overlaps.
How do I set constant padding?⌄
Pass a real numeric scalar. The RunMat-gated fill keyword optionally accepts a following scalar and defaults to zero.
Does imfilter support color or N-D images?⌄
Yes. Trailing image axes are filtered independently when the public at-most-2-D H is extended by singleton axes.
Does imfilter normalize H?⌄
No. H is used exactly as supplied; fspecial can generate normalized smoothing kernels.
What if H is larger than A?⌄
Same and full use selected padding; valid may return a zero-length axis.
Can imfilter fuse into elementwise chains?⌄
No. It is a standalone filtering operation with an optional provider hook.
Are logical images supported?⌄
Yes. Logical values are computed numerically and rounded/clamped back to logical output; the prior editable reference's double-output claim did not match runtime behavior.
Related Image functions
Source you can read
Every RunMat function's source is public. Read exactly how imfilter is executed, line by line, in Rust.
- View the source for imfilter 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.