GeneralConvolution

From Avisynth wiki
(Difference between revisions)
Jump to: navigation, search
m (added category)
(add link to avs+ documentation)
 
(6 intermediate revisions by 3 users not shown)
Line 1: Line 1:
{{Template:FuncDef|GeneralConvolution (clip ''clip'', [int ''bias'', string ''matrix'', float ''divisor'', bool ''auto''])}}
+
<div {{BlueBox2|40|0|3px solid purple}} >
 +
{{AvsPlusFullname}}<br>
 +
Up-to-date documentation: [https://avisynthplus.readthedocs.io/en/latest/avisynthdoc/corefilters/convolution.html https://avisynthplus.readthedocs.io]
 +
</div>
  
This filter performs a matrix convolution on a RGB32 clip.
 
  
== Parameters ==
+
Performs a matrix convolution on an [[RGB32]] ({{AvsPluscon}} any format) clip.
{| border="1"
+
 
|-
+
== Syntax and Parameters ==
| ''clip''
+
<div style="max-width:68em" >
| RGB32 clip
+
{{Template:FuncDef
|-
+
|GeneralConvolution (clip ''clip'', [ int ''bias'', string ''matrix'', float ''divisor'', bool ''auto'' ] )
| ''bias'' (default: 0)
+
}}
| additive bias to adjust the total output intensity
+
 
|-
+
{{AvsPluscon}}
| ''matrix'' (default: "0 0 0 0 1 0 0 0 0")
+
{{Template:FuncDef
| can be a 3x3 or 5x5 matrix with 9 or 25 integer numbers between 0 and 255
+
|GeneralConvolution (clip ''clip'', [ float ''bias'', string ''matrix'', float ''divisor'', bool ''auto'', bool ''luma'', bool ''chroma'', bool ''alpha'' ] )
|-  
+
}}
| ''divisor'' (default: 1)
+
 
| divides the output of the convolution (calculated before adding bias)
+
:{{Par2|clip|clip|}}
|-
+
::Source clip. Must be [[RGB32]]. {{AvsPluscon}} All color spaces since Avisynth+ r2768.
| ''auto'' (default: true)
+
 
| Enables the auto scaling functionality. This divides the result by the sum of the elements of the matrix. The value of divisor is applied in addition to this auto scaling factor. If the sum of elements is zero, auto is disabled.
+
:{{Par2|bias|int|0}} {{AvsPluscon}} {{Par2|bias|float|0.0}}
|}
+
::Additive bias to adjust the total output intensity.
 +
 
 +
:{{Par2|matrix|string|"0 0 0 0 1 0 0 0 0"}}
 +
::A 3×3 or 5×5 matrix with 3<sup>2</sup> (9) or 5<sup>2</sup> (25) integer values.
 +
::{{AvsPluscon}} A 3×3, 5×5, 7×7 or 9×9 matrix with 3<sup>2</sup> (9), 5<sup>2</sup> (25), 7<sup>2</sup> (49), 9<sup>2</sup> (81) float or integer values. float values are converted to integers  for 8-16 bit clips
 +
 
 +
:{{Par2|divisor|float|1.0}}
 +
::Divides the output of the convolution before adding {{FuncArg|bias}}.
 +
 
 +
:{{Par2|auto|bool|true}}
 +
::Enables ''auto scaling''. Auto scaling divides the output of the convolution by the sum of the elements of the matrix. The value of {{FuncArg|divisor}} is applied in addition to this auto scaling factor. If the sum of elements is zero, auto scaling is disabled.
  
The ''divisor'' is usually the sum of the elements of the matrix. But when the sum is zero, you can leave divisor=1 and use the ''bias'' setting to correct the pixel values. The ''bias'' could be useful if the pixel values are negative due to the convolution. After adding a bias, the pixels are just clipped to zero (and 255 if they are larger than 255).
+
:{{AvsPluscon}}
 +
:{{Par2|luma|bool|true}}
 +
:{{Par2|chroma|bool|true}}
 +
:{{Par2|alpha|bool|true}}
 +
::Enables processing only selected planes. For RGB clips {{FuncArg|luma}} and {{FuncArg|chroma}} setting is ignored. Unprocessed planes are simply copied. E.g. alpha=false can speed up RGB32 processing, usually A channel is not used.
 +
 
 +
The {{FuncArg|divisor}} is usually the sum of the elements of the matrix. But when the sum is zero, you can leave {{FuncArg|divisor}}=1 and use the {{FuncArg|bias}} setting to correct the pixel values. The {{FuncArg|bias}} could be useful if the pixel values are negative due to the convolution. After adding {{FuncArg|bias}}, the pixels are clipped to the range 0-255 {{AvsPluscon}} or the appropriate min-max range of the specific bit depth. In 32 bit float formats no clamp happens.
  
 
Around the borders the edge pixels are simply repeated to service the matrix.
 
Around the borders the edge pixels are simply repeated to service the matrix.
 
+
</div>
  
 
== Examples ==   
 
== Examples ==   
 
+
* Blur:
# Blur:
+
<div {{BoxWidthIndent|28|2}} >
 
  GeneralConvolution(0, "
 
  GeneralConvolution(0, "
 
     10 10 10 10 10
 
     10 10 10 10 10
Line 36: Line 54:
 
     10 10 10 10 10  
 
     10 10 10 10 10  
 
     10 10 10 10 10 ", 256, false)
 
     10 10 10 10 10 ", 256, false)
 
+
</div>
# Horizontal (Sobel) edge detection:  
+
{{HalfBreak}}
 +
* Horizontal (Sobel) edge detection:  
 +
<div {{BoxWidthIndent|28|2}} >
 
  GeneralConvolution(0, "
 
  GeneralConvolution(0, "
 
     1  2  1  
 
     1  2  1  
 
     0  0  0  
 
     0  0  0  
 
     -1 -2 -1 ", 8)
 
     -1 -2 -1 ", 8)
 
+
</div>
# Vertical (Sobel) Edge Detection:  
+
{{HalfBreak}}
 +
* Vertical (Sobel) Edge Detection:  
 +
<div {{BoxWidthIndent|28|2}} >
 
  GeneralConvolution(0, "
 
  GeneralConvolution(0, "
 
     1  0 -1  
 
     1  0 -1  
 
     2  0 -2  
 
     2  0 -2  
 
     1  0 -1 ", 8)
 
     1  0 -1 ", 8)
 
+
</div>
# Displacement (simply move the position of the "1" for left, right, up, down)
+
{{HalfBreak}}
 +
* Displacement (simply move the position of the "1" for left, right, up, down)
 +
<div {{BoxWidthIndent|28|2}} >
 
  GeneralConvolution(0, "
 
  GeneralConvolution(0, "
 
     0 1 0  
 
     0 1 0  
 
     0 0 0  
 
     0 0 0  
 
     0 0 0 ")
 
     0 0 0 ")
 
+
</div>
# Displacement by half pixel up (auto scaling):
+
{{HalfBreak}}
 +
* Displacement by half pixel up (auto scaling):
 +
<div {{BoxWidthIndent|28|2}} >
 
  GeneralConvolution(0, "
 
  GeneralConvolution(0, "
 
     0  1  0  
 
     0  1  0  
 
     0  1  0  
 
     0  1  0  
 
     0  0  0 ")
 
     0  0  0 ")
 
+
</div>
# Displacement by half pixel to the right (manual scaling):
+
{{HalfBreak}}
 +
* Displacement by half pixel to the right (manual scaling):
 +
<div {{BoxWidthIndent|28|2}} >
 
  GeneralConvolution(0, "
 
  GeneralConvolution(0, "
 
     0  0  0
 
     0  0  0
 
     0 128 128
 
     0 128 128
 
     0  0  0 ", 256, false)
 
     0  0  0 ", 256, false)
 
+
</div>
# Sharpness filter:
+
{{HalfBreak}}
 +
* Sharpness filter:
 +
<div {{BoxWidthIndent|28|2}} >
 
  GeneralConvolution(0, "
 
  GeneralConvolution(0, "
 
     0  -1  0  
 
     0  -1  0  
 
   -1    5  -1  
 
   -1    5  -1  
 
     0  -1  0 ", 0, true)
 
     0  -1  0 ", 0, true)
+
</div>
#In this case, the new pixel values y(m,n) are given by
+
:In this case, the new pixel values <span style="font-family:times,georgia,serif;">y(m,n)</span> are given by
#y(m,n) = ( -1*x(m-1,n) - 1*x(m,n-1) + 5*x(m,n) - 1*x(m,n+1) - 1*x(m+1,n) )/1.0 + 0
+
:<span style="font-family:times,georgia,serif;">y(m,n) = ( -1*x(m-1,n) - 1*x(m,n-1) + 5*x(m,n) - 1*x(m,n+1) - 1*x(m+1,n) )/1.0 + 0</span>
 
+
{{HalfBreak}}
# Slight blur filter with black level clipping and 25% brightening:
+
* Slight blur filter with black level clipping and 25% brightening:
 +
<div {{BoxWidthIndent|28|2}} >
 
  GeneralConvolution(-16, "
 
  GeneralConvolution(-16, "
 
     0  12  0
 
     0  12  0
 
   12  256  12
 
   12  256  12
 
     0  12  0 ", 0.75 ,true)
 
     0  12  0 ", 0.75 ,true)
+
</div>
# In this case, the new pixel values y(m,n) are given by
+
:In this case, the new pixel values <span style="font-family:times,georgia,serif;">y(m,n)</span> are given by
# y(m,n) = ( 12*x(m-1,n) + 12*x(m,n-1) + 256*x(m,n) + 12*x(m,n+1) + 12*x(m+1,n) )/(12+12+256+12+12)/0.75 - 16
+
:<span style="font-family:times,georgia,serif;">y(m,n) = ( 12*x(m-1,n) + 12*x(m,n-1) + 256*x(m,n) + 12*x(m,n+1) + 12*x(m+1,n) )/(12+12+256+12+12)/0.75 - 16</span>
 
+
{{HalfBreak}}
# Emboss filter (3D relief effect)
+
* Emboss filter (3D relief effect)
 +
<div {{BoxWidthIndent|28|2}} >
 
  GeneralConvolution(128, "
 
  GeneralConvolution(128, "
 
  -1 0 0
 
  -1 0 0
 
   0 0 0
 
   0 0 0
 
   0 0 1")
 
   0 0 1")
 +
</div>
  
Some other examples can be found [http://web.archive.org/web/20100105183639/http://www.gamedev.net/reference/programming/features/imageproc/page2.asp here].
+
== External Links ==
 +
* [https://jeanbruenn.info/2011/03/13/avisynths-convolution-stuff-explained/ ''AviSynth’s GeneralConvolution explained'']
 +
* Some other examples can be found [http://web.archive.org/web/20100105183639/http://www.gamedev.net/reference/programming/features/imageproc/page2.asp here].
  
 
+
== Changelog ==
'''Changelog''':
+
 
{| border="1"
 
{| border="1"
 +
|-
 +
| {{AvsPluscon}} r2768
 +
| Supports all color spaces, added 7x7 and 9x9 matrix sizes, added luma, chroma, alpha
 
|-  
 
|-  
 
| v2.55
 
| v2.55
Line 103: Line 140:
 
| Initial Release
 
| Initial Release
 
|}
 
|}
<br>
+
 
== External Links ==
+
 
* [http://web.archive.org/web/20120802031716/http://jeanbruenn.info/2011/03/13/avisynths-convolution-stuff-explained/ AviSynth’s GeneralConvolution explained]
+
 
[[Category:Internal filters]]
 
[[Category:Internal filters]]
 
[[Category:Blurring]]
 
[[Category:Blurring]]
 
[[Category:Sharpeners]]
 
[[Category:Sharpeners]]

Latest revision as of 18:23, 17 September 2022

AviSynth+
Up-to-date documentation: https://avisynthplus.readthedocs.io


Performs a matrix convolution on an RGB32 (AVS+ any format) clip.

Contents

[edit] Syntax and Parameters

GeneralConvolution (clip clip, [ int bias, string matrix, float divisor, bool auto ] )

AVS+ GeneralConvolution (clip clip, [ float bias, string matrix, float divisor, bool auto, bool luma, bool chroma, bool alpha ] )

clip  clip =
Source clip. Must be RGB32. AVS+ All color spaces since Avisynth+ r2768.
int  bias = 0
AVS+ float  bias = 0.0
Additive bias to adjust the total output intensity.
string  matrix = "0 0 0 0 1 0 0 0 0"
A 3×3 or 5×5 matrix with 32 (9) or 52 (25) integer values.
AVS+ A 3×3, 5×5, 7×7 or 9×9 matrix with 32 (9), 52 (25), 72 (49), 92 (81) float or integer values. float values are converted to integers for 8-16 bit clips
float  divisor = 1.0
Divides the output of the convolution before adding bias.
bool  auto = true
Enables auto scaling. Auto scaling divides the output of the convolution by the sum of the elements of the matrix. The value of divisor is applied in addition to this auto scaling factor. If the sum of elements is zero, auto scaling is disabled.
AVS+
bool  luma = true
bool  chroma = true
bool  alpha = true
Enables processing only selected planes. For RGB clips luma and chroma setting is ignored. Unprocessed planes are simply copied. E.g. alpha=false can speed up RGB32 processing, usually A channel is not used.

The divisor is usually the sum of the elements of the matrix. But when the sum is zero, you can leave divisor=1 and use the bias setting to correct the pixel values. The bias could be useful if the pixel values are negative due to the convolution. After adding bias, the pixels are clipped to the range 0-255 AVS+ or the appropriate min-max range of the specific bit depth. In 32 bit float formats no clamp happens.

Around the borders the edge pixels are simply repeated to service the matrix.

[edit] Examples

  • Blur:
GeneralConvolution(0, "
   10 10 10 10 10
   10 10 10 10 10 
   10 10 16 10 10 
   10 10 10 10 10 
   10 10 10 10 10 ", 256, false)
 
  • Horizontal (Sobel) edge detection:
GeneralConvolution(0, "
    1  2  1 
    0  0  0 
   -1 -2 -1 ", 8)
 
  • Vertical (Sobel) Edge Detection:
GeneralConvolution(0, "
   1  0 -1 
   2  0 -2 
   1  0 -1 ", 8)
 
  • Displacement (simply move the position of the "1" for left, right, up, down)
GeneralConvolution(0, "
   0 1 0 
   0 0 0 
   0 0 0 ")
 
  • Displacement by half pixel up (auto scaling):
GeneralConvolution(0, "
   0   1   0 
   0   1   0 
   0   0   0 ")
 
  • Displacement by half pixel to the right (manual scaling):
GeneralConvolution(0, "
   0   0   0
   0 128 128
   0   0   0 ", 256, false)
 
  • Sharpness filter:
GeneralConvolution(0, "
   0   -1   0 
  -1    5  -1 
   0   -1   0 ", 0, true)
In this case, the new pixel values y(m,n) are given by
y(m,n) = ( -1*x(m-1,n) - 1*x(m,n-1) + 5*x(m,n) - 1*x(m,n+1) - 1*x(m+1,n) )/1.0 + 0
 
  • Slight blur filter with black level clipping and 25% brightening:
GeneralConvolution(-16, "
   0   12   0
  12  256  12
   0   12   0 ", 0.75 ,true)
In this case, the new pixel values y(m,n) are given by
y(m,n) = ( 12*x(m-1,n) + 12*x(m,n-1) + 256*x(m,n) + 12*x(m,n+1) + 12*x(m+1,n) )/(12+12+256+12+12)/0.75 - 16
 
  • Emboss filter (3D relief effect)
GeneralConvolution(128, "
-1 0 0
 0 0 0
 0 0 1")

[edit] External Links

[edit] Changelog

AVS+ r2768 Supports all color spaces, added 7x7 and 9x9 matrix sizes, added luma, chroma, alpha
v2.55 Added divisor, auto.
v2 Initial Release
Personal tools