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.
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.
Figure: Original and recalculated matrix.
The filter element <feConvolveMatrix> has, besides the universal attributes, these attributes:
ordersets matrix size. Supply width alone or width and height; omitted height equals width. A 3x3 matrix can therefore useorder="3"ororder="3 3", separated by spaces or commas. Omitting the attributeorderuses 3x3. Width and height must be positive integers.
Note:
Keep matrices small: large ones require much processing time.
kernelMatrixdefines the matrix as a comma- or space-separated number list. Its count must match the dimensions specified byorder: width and height 3 require nine entries.divisordefines the divisor. Above, the sum of matrix values was used; that is the default when the attributedivisoris omitted. Otherwise the given value replaces the sum. Values must exceed 0.
Note:
If matrix entries sum to zero and the attribute divisor is omitted, the filter uses 1. Negative entries make a zero sum possible.
biasadds a number to the calculated pixel value, further changing color. Omitting it defaults to zero, making no additional color change.targetXandtargetYposition the filter matrix within the image matrix. The first row and column have index 0. The value oftargetXortargetYmust be at least 0 and below matrix width or height respectively. Both default to 0.edgeModecontrols matrix edges. For example, when a 3x3 matrix leaves two pixels at an edge, it determines their treatment. Possible values areduplicate,wrapandnone. Withduplicateexisting pixels are duplicated to fill missing ones. Withwrapvalues from the opposite corner or edge fill them. Withnoneblack pixels fill them.preserveAlphaaccepts the valuestrueandfalse. Withfalseall channels change, including alpha. Withtruethe alpha channel stays unchanged.falseis the default.
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.
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>
Copyright © verlag moderne industrie GmbH 2001
You may print the online version for your private use.
Otherwise, this chapter from “Das Einsteigerseminar SVG — Webgrafiken mit XML” is subject to the same conditions as the bound edition. The work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
verlag moderne industrie GmbH, Justus-von-Liebig-Straße 1, D-86899 Landsberg/Lech, fon ++49 (0) 81 91 / 125 – 0, leserservice(at)mi-verlag.de
back to top