XML technologies / SVG / Filters, transformations and gradients / Using and combining filters and lighting effectively / Filters and masks / The feConvolveMatrix filter element

(Excerpt from “Das Einsteigerseminar SVG — Webgrafiken mit XML” by Helma Spona)

The filter element <feConvolveMatrix> defines a matrix changing neighboring pixels. A 3x3 matrix, for example, multiplies its entries by corresponding image values and divides by the sum of matrix entries, assigning the result to image values. The figure shows the image matrix left and filter matrix right.

Image matrix left, filter matrix right

Figure: Image matrix left, filter matrix right.

Applying the filter processes pixels successively in blocks matching its matrix size, here 3x3, beginning with the gray area.

Multiply corresponding entries and add the products, then divide by the sum of filter entries. Each gray-area value is calculated as follows:

Ergebnis = (255*1 + 255*2 + 0*3 + 255*4 + 255*5 + 0*6 + 255*7 + 255*8 + 0*9) / (1+2+3+4+5+6+7+8+9)

The next figure compares original and calculated matrices. Tables resemble an image’s pixel grid more closely than standard mathematical matrix notation.

Original and recalculated matrix

Figure: Original and recalculated matrix.

The filter element <feConvolveMatrix> has, besides the universal attributes, these attributes:

Note:
Keep matrices small: large ones require much processing time.

Note:
If matrix entries sum to zero and the attribute divisor is omitted, the filter uses 1. Negative entries make a zero sum possible.

The figure shows the original graphic changed by filters, top to bottom: 4x4, 7x4 and 7x4preserveAlpha from the following listing.

The filters 7x4preserveAlpha and 7x4 differ only in the parameter preserveAlpha.

Different feConvolveMatrix filters applied

Figure: Different feConvolveMatrix filters applied.

<filter id="4x4">
  <feConvolveMatrix in="SourceGraphic" kernelMatrix="10 10 50 50 50 20 20 10 0 10 10 50 50 50 20 20" order="4"/>
</filter>
<filter id="6x4">
  <feConvolveMatrix in="SourceGraphic" kernelMatrix="255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0" divisor="5700" order="7 4"/>
</filter>
<filter id="6x4preserveAlpha">
  <feConvolveMatrix in="SourceGraphic" edgeMode="wrap" preserveAlpha="true" kernelMatrix="255 255 255 255 255 255 255 255 255 255 255 255 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0" divisor="5700" order="7 4"/>
</filter>

   

 

 

 

back to top