e01.org

<feConvolveMatrix>

The <feConvolveMatrix> SVG filter primitive applies a matrix convolution filter effect. A convolution combines pixels in the input image with neighboring pixels to produce a resulting image. A wide variety of imaging operations can be achieved through convolutions, including blurring, edge detection, sharpening, embossing and beveling.

The **`<feConvolveMatrix>`** [SVG](/docs/Web/SVG) filter primitive applies a matrix convolution filter effect. A convolution combines pixels in the input image with neighboring pixels to produce a resulting image. A wide variety of imaging operations can be achieved through convolutions, including blurring, edge detection, sharpening, embossing and beveling.

A matrix convolution is based on an n-by-m matrix (the convolution kernel) which describes how a given pixel value in the input image is combined with its neighboring pixel values to produce a resulting pixel value. Each result pixel is determined by applying the kernel matrix to the corresponding source pixel and its neighboring pixels. The basic convolution formula which is applied to each color value for a given pixel is:

<!-- prettier-ignore-start -->
<math display="block">
<semantics><mrow><mrow><msub><mi>color</mi><mrow><mi>X</mi><mi>,</mi><mi>Y</mi></mrow></msub><mo stretchy="false">=</mo><mrow><mfrac><mrow><mrow><mrow><munderover><mo stretchy="false">∑</mo><mrow><mrow><mi>i</mi><mo stretchy="false">=</mo><mn>0</mn></mrow></mrow><mrow><mrow><mi>orderY</mi><mo stretchy="false">−</mo><mn>1</mn></mrow></mrow></munderover><mrow><munderover><mo stretchy="false">∑</mo><mrow><mrow><mi>j</mi><mo stretchy="false">=</mo><mn>0</mn></mrow></mrow><mrow><mrow><mi>orderX</mi><mo stretchy="false">−</mo><mn>1</mn></mrow></mrow></munderover><msub><mi>source</mi><mrow><mrow><mrow><mi>x</mi><mo stretchy="false">−</mo><mi>targetX</mi></mrow><mo stretchy="false">+</mo><mi>j</mi></mrow><mi>,</mi><mrow><mrow><mi>y</mi><mo stretchy="false">−</mo><mi mathvariant="italic">targetY</mi></mrow><mo stretchy="false">+</mo><mi>i</mi></mrow></mrow></msub></mrow></mrow><mo stretchy="false">⋅</mo><msub><mi>kernelMatrix</mi><mrow><mrow><mrow><mi>orderX</mi><mo stretchy="false">−</mo><mi>j</mi></mrow><mo stretchy="false">−</mo><mn>1,</mn></mrow><mrow><mrow><mi>orderY</mi><mo stretchy="false">−</mo><mi>i</mi></mrow><mo stretchy="false">−</mo><mn>1</mn></mrow></mrow></msub></mrow></mrow><mrow><mi>divisor</mi></mrow></mfrac><mo stretchy="false">+</mo><mrow><mi>bias</mi><mo stretchy="false">⋅</mo><msub><mi>alpha</mi><mrow><mi>x</mi><mi>,</mi><mi>y</mi></mrow></msub></mrow></mrow></mrow></mrow><annotation encoding="TeX">\left(color\right)_{X , Y} = \frac{\sum_{i = 0}^{orderY - 1} \sum_{j = 0}^{orderX - 1} \left(source\right)_{x - targetX + j , y - \mathit{targetY} + i} \cdot \left(kernelMatrix\right)_{orderX - j - 1, orderY - i - 1}}{divisor} + bias \cdot \left(alpha\right)_{x , y}</annotation></semantics>
</math>
<!-- prettier-ignore-end -->

where "orderX" and "orderY" represent the X and Y values for the [`order`](/docs/Web/SVG/Reference/Attribute/order) attribute, and the other variables refer to the [`targetX`](/docs/Web/SVG/Reference/Attribute/targetX), [`targetY`](/docs/Web/SVG/Reference/Attribute/targetY), [`kernelMatrix`](/docs/Web/SVG/Reference/Attribute/kernelMatrix), [`divisor`](/docs/Web/SVG/Reference/Attribute/divisor), and [`bias`](/docs/Web/SVG/Reference/Attribute/bias) attributes.

Note in the above formulas that the values in the kernel matrix are applied such that the kernel matrix is rotated 180 degrees relative to the source and destination images in order to match convolution theory as described in many computer graphics textbooks.

To illustrate, suppose you have an input image which is 5 pixels by 5 pixels, whose color values for one of the color channels are as follows:

```plain
0 20 40 235 235
100 120 140 235 235
200 220 240 235 235
225 225 255 255 255
225 225 255 255 255
```

and you define a 3-by-3 convolution kernel as follows:

```plain
1 2 3
4 5 6
7 8 9
```

Let's focus on the color value at the second row and second column of the image (source pixel value is 120). Assuming the simplest case (where the input image's pixel grid aligns perfectly with the kernel's pixel grid) and assuming default values for attributes [`divisor`](/docs/Web/SVG/Reference/Attribute/divisor), [`targetX`](/docs/Web/SVG/Reference/Attribute/targetX) and [`targetY`](/docs/Web/SVG/Reference/Attribute/targetY), then resulting color value will be:

```plain
(9*0 + 8*20 + 7*40 +
6*100 + 5*120 + 4*140 +
3*200 + 2*220 + 1*240) / (9+8+7+6+5+4+3+2+1)
```

Like other filter primitives, it handles color components in the `linearRGB` color space by default. You can use color-interpolation-filters to use `sRGB` instead.

## Usage context

## Attributes

- in
- order
- kernelMatrix
- divisor
- bias
- targetX
- targetY
- edgeMode
- kernelUnitLength
- preserveAlpha
- [Filter primitive attributes](/docs/Web/SVG/Reference/Attribute#filter_primitive_attributes_presentation_attributes): x, y, width, height, result

## DOM Interface

This element implements the SVGFEConvolveMatrixElement interface.

## Example

### SVG

```html
<svg
width="200"
height="200"
viewBox="0 0 200 200"
xmlns="http://www.w3.org/2000/svg"
xmlns:xlink="http://www.w3.org/1999/xlink">
<defs>
<filter id="emboss">
<feConvolveMatrix
kernelMatrix="3 0 0
0 0 0
0 0 -3" />
</filter>
</defs>

<image
href="mdn.svg"
x="0"
y="0"
height="200"
width="200"
filter="url(#emboss)" />
</svg>
```

### Result

200

## Specifications

## Browser compatibility

## See also

- [SVG filter primitive attributes](/docs/Web/SVG/Reference/Attribute#filter_primitive_attributes_presentation_attributes)
- filter
- animate
- set
- feBlend
- feColorMatrix
- feComponentTransfer
- feComposite
- feDiffuseLighting
- feDisplacementMap
- feFlood
- feGaussianBlur
- feImage
- feMerge
- feMorphology
- feOffset
- feSpecularLighting
- feTile
- feTurbulence
- [SVG tutorial: Filter effects](/docs/Web/SVG/Tutorials/SVG_from_scratch/Filter_effects)

source: MDN Web Docs mirror · page

Cite this

"". MDN Web Docs. CC BY-SA 2.5 (MDN). https://developer.mozilla.org/en-US/docs/Web/SVG/Reference/Element/feConvolveMatrix

JSON record · API collection · citation policy