Layer
(→ColorKeyMask) |
Raffriff42 (Talk | contribs) (formatting, links, phrasing touch-up) |
||
Line 1: | Line 1: | ||
== Layer == | == Layer == | ||
− | + | Later (aka ''overlay'', ''blend'', ''merge'') two clips of possibly different sizes, but with the same color format. | |
− | + | ||
− | {{ | + | For pixel-wise transparency information the alpha channel of an [[RGB32]] ''overlay_clip'' is used as a mask. |
+ | |||
+ | {{FuncDef | ||
+ | |Layer( clip ''base_clip'', clip ''overlay_clip'' [, string ''op'', int ''level'', int ''x'', int ''y'', int ''threshold'', bool ''use_chroma'' ] ) | ||
+ | }} | ||
:{{Par2|base_clip|clip|}} | :{{Par2|base_clip|clip|}} | ||
− | :: | + | :: The underlying clip, which determines the size and all other video and audio properties of the result. |
:{{Par2|overlay_clip|clip|}} | :{{Par2|overlay_clip|clip|}} | ||
− | :: | + | :: The clip which is merged onto {{FuncArg|clip}}. |
:{{Par2|op|string|"add"}} | :{{Par2|op|string|"add"}} | ||
− | :: | + | ::The merge operation to be performed, which can be one of the following: |
::{| class="wikitable" | ::{| class="wikitable" | ||
|- | |- | ||
Line 19: | Line 22: | ||
| '''Description''' | | '''Description''' | ||
|- | |- | ||
− | | add | + | | ''add'' |
− | | This is the default mode. Equivalent to [[Overlay]](mode="blend"). | + | | This is the default mode. Equivalent to [[Overlay]](mode="blend").<br> |
+ | {{FuncArg|overlay_clip}} will be copied on top of the original, in proportion to {{FuncArg|level}}, and subject to the alpha channel. | ||
|- | |- | ||
− | | subtract | + | | ''subtract'' |
− | | | + | | {{FuncArg|base_clip}} minus {{FuncArg|overlay_clip}}. <br> |
+ | If both clips are equal and {{FuncArg|level}}=128, a flat gray field is returned; compare to [[Subtract]]. | ||
|- | |- | ||
− | | lighten | + | | ''lighten'' |
− | | | + | | Copy {{FuncArg|overlay_clip}} over {{FuncArg|base_clip}} in areas where {{FuncArg|overlay_clip}} is lighter. |
|- | |- | ||
− | | darken | + | | ''darken'' |
− | | | + | | Copy {{FuncArg|overlay_clip}} over {{FuncArg|base_clip}} in areas where {{FuncArg|overlay_clip}} is darker. |
|- | |- | ||
− | | fast | + | | ''fast'' |
| Like ''add'', but without masking. | | Like ''add'', but without masking. | ||
|- | |- | ||
− | | mul | + | | ''mul'' |
− | | | + | | {{FuncArg|base_clip}} multiplied by {{FuncArg|overlay_clip}}. This will generally make the output darker (see [http://docs.gimp.org/2.8/en/gimp-concepts-layer-modes.html#layer-mode-multiply GIMP: Multiply]). |
|} | |} | ||
:{{Par2|level|int|(maximum)}} | :{{Par2|level|int|(maximum)}} | ||
::The strength of the performed operation: | ::The strength of the performed operation: | ||
− | ::* 0 | + | ::* 0 – no effect: {{FuncArg|base_clip}} is returned unchanged |
− | ::* 257 (256 for YUY2) | + | ::* 257 (256 for YUY2) – maximum strength |
:{{Par2|x|int|0}} | :{{Par2|x|int|0}} | ||
:{{Par2|y|int|0}} | :{{Par2|y|int|0}} | ||
− | ::offset position of | + | ::offset position of {{FuncArg|overlay_clip}} |
:{{Par2|threshold|int|0}} | :{{Par2|threshold|int|0}} | ||
Line 51: | Line 56: | ||
:{{Par2|use_chroma|bool|true}} | :{{Par2|use_chroma|bool|true}} | ||
− | :: | + | ::Use chroma of the {{FuncArg|overlay_clip}}. Default=true. When false, only luma is used. |
− | + | Audio, [[Clip_properties|FrameRate]] and [[Clip_properties|FrameCount]] are taken from the first clip. | |
− | |||
− | |||
+ | There are some differences in the behaviour and the allowed parameter depending on the color format and the operation; here are the details: | ||
− | + | *There is no mask (alpha channel) in [[YUY2]], so the alpha channel is assumed to be fully opaque (255) everywhere. | |
− | + | *In [[RGB32]] the {{FuncArg|overlay_clip}} alpha channel is multiplied with {{FuncArg|level}}, so the resulting alpha = (alpha_mask * level + 1) / 256. <br>This means for full strength of operation, alpha has to be 255 and {{FuncArg|level}} has to be 257. | |
− | ''add'': threshold is not used. The difference between | + | *These operators behave equally for [[RGB32]] or [[YUY2]]: |
− | :*alpha=0 → only base_clip visible | + | |
− | :*alpha=128 → base_clip and overlay_clip equally blended | + | :''fast'': use_chroma must be ''true''; {{FuncArg|level}} and {{FuncArg|threshold}} are not used. The result is simply the average of {{FuncArg|base_clip}} and {{FuncArg|overlay_clip}}. |
− | :*alpha=255 → only overlay_clip visible | + | |
− | Formula used : | + | :''add'': {{FuncArg|threshold}} is not used. The difference between {{FuncArg|base_clip}} and {{FuncArg|overlay_clip}} is multiplied with alpha and added to {{FuncArg|base_clip}}. |
+ | :*alpha=0 → only {{FuncArg|base_clip}} visible | ||
+ | :*alpha=128 → {{FuncArg|base_clip}} and {{FuncArg|overlay_clip}} equally blended | ||
+ | :*alpha=255 → only {{FuncArg|overlay_clip}} visible | ||
+ | :Formula used : | ||
:*RGB: [[File:Layer-Eq1.png]] | :*RGB: [[File:Layer-Eq1.png]] | ||
:*YUY2: [[File:Layer-Eq2.png]] | :*YUY2: [[File:Layer-Eq2.png]] | ||
− | ''subtract'': the same as add, but | + | :''subtract'': the same as add, but {{FuncArg|overlay_clip}} is inverted before adding. |
+ | *These operators seem to work correctly only in YUY2: [todo: check and update documentation] | ||
− | + | :''mul'': threshold is not used. {{FuncArg|base_clip}} is colored as {{FuncArg|overlay_clip}}, so {{FuncArg|use_chroma}} should be ''true''. | |
+ | :*alpha=0 → only {{FuncArg|base_clip}} visible. | ||
+ | :*alpha=255 → approx. the same luminance as {{FuncArg|base_clip}} but with the colors of {{FuncArg|overlay_clip}}. | ||
− | '' | + | :''lighten'': {{FuncArg|use_chroma}} must be ''true''. Performs the same operation as ''add'', but only when {{FuncArg|overlay_clip}} is BRIGHTER than {{FuncArg|base_clip}}. With a higher {{FuncArg|threshold}} the operation is more likely, so with {{FuncArg|threshold}}=255 is the same as ''add''; with {{FuncArg|threshold}}=0, {{FuncArg|base_clip}} is more likely passed unchanged, depending on the difference between {{FuncArg|base_clip}} and {{FuncArg|overlay_clip}}. |
− | + | ||
− | + | ||
− | + | :''darken'': the same as "lighten", but it is performed only when {{FuncArg|overlay_clip}} is DARKER than {{FuncArg|base_clip}}. | |
− | + | ||
− | ''darken'': the same as "lighten", but it is performed only when | + | |
− | + | ||
− | + | ||
====Examples==== | ====Examples==== | ||
*Demonstration of ''lighten'', ''darken'' and ''threshold'': | *Demonstration of ''lighten'', ''darken'' and ''threshold'': | ||
+ | <div {{BoxWidthIndent|48|1}} > | ||
[[ImageSource]]("[[:File:Lenna.png|Lenna.png]]") | [[ImageSource]]("[[:File:Lenna.png|Lenna.png]]") | ||
[[ConvertToYUY2]] | [[ConvertToYUY2]] | ||
Layer([[BlankClip]](Last, color=[[Color_presets|color_blue]]), op="lighten"''', threshold=40''') | Layer([[BlankClip]](Last, color=[[Color_presets|color_blue]]), op="lighten"''', threshold=40''') | ||
Layer([[BlankClip]](Last, color=[[Color_presets|color_yellow]]), op="darken"''', threshold=40''') | Layer([[BlankClip]](Last, color=[[Color_presets|color_yellow]]), op="darken"''', threshold=40''') | ||
+ | </div> | ||
{| | {| | ||
|[[File:Lenna-Layer-lighten-blue%2Bdarken-yellow,threshold%3D0(default).jpg|256px]] | |[[File:Lenna-Layer-lighten-blue%2Bdarken-yellow,threshold%3D0(default).jpg|256px]] | ||
Line 101: | Line 108: | ||
|} | |} | ||
− | *''Layer'' can be used to combine two captures of different broadcasts for reducing noise. A discussion of this idea can be found {{doom9|28438}}. Of course you have to ensure that the frames of the two clips match exactly -- use [[Trim]] or [[DeleteFrame]] as necessary. A sample script: | + | *'''Layer''' can be used to combine two captures of different broadcasts for reducing noise. A discussion of this idea can be found {{doom9|28438}}. Of course you have to ensure that the frames of the two clips match exactly -- use [[Trim]] or [[DeleteFrame]] as necessary. A sample script: |
− | + | <div {{BoxWidthIndent|48|1}} > | |
clip1 = [[AviSource]]("capture-pass-1.avi").[[ConvertToYUY2]] | clip1 = [[AviSource]]("capture-pass-1.avi").[[ConvertToYUY2]] | ||
clip2 = [[AviSource]]("capture-pass-2.avi").[[ConvertToYUY2]].[[Trim]](101, 0) | clip2 = [[AviSource]]("capture-pass-2.avi").[[ConvertToYUY2]].[[Trim]](101, 0) | ||
return Layer(clip1, clip2, "fast") | return Layer(clip1, clip2, "fast") | ||
+ | </div> | ||
+ | |||
== Mask == | == Mask == | ||
− | Applies a defined alpha-mask to clip | + | Applies a defined alpha-mask to a clip, by converting [[RGB32]]-only {{FuncArg|mask_clip}} to greyscale and copying that greyscale information to the alpha channel of RGB32-only {{FuncArg|clip}}. In the alpha channel, "black" means transparent and "white" means opaque. For those of you who familiar with Photoshop masks, the concept is the same. In fact you can create a black and white photo in Photoshop, load it in your script and use it as a mask. |
− | {{ | + | {{FuncDef|Mask(clip ''clip'', clip ''mask_clip'')}} |
====Examples==== | ====Examples==== | ||
− | ss.jpg is | + | Suppose "ss.jpg" is a snapshot from a video clip, which served as a guideline to create the mask using Paint. We use [[ImageSource]] to load the image in the script and '''Mask''' to apply it. |
− | + | <div {{BoxWidthIndent|68|1}} > | |
− | bg = AviSource("01gray.avi").ConvertToRGB32 | + | bg = [[AviSource]]("01gray.avi").[[ConvertToRGB32]] # here is the background clip |
− | mk = | + | mk = [[ImageSource]]("ss.jpg").ConvertToRGB32 # load the image |
− | top = AviSource("k3.avi").ConvertToRGB32 | + | top = AviSource("k3.avi").ConvertToRGB32.Mask(mk) # load the top layer clip and apply the mask to it |
− | Layer(bg, top) | + | Layer(bg, top) # layer the background and the top layer with mask |
+ | </div> | ||
== ResetMask == | == ResetMask == | ||
− | Applies an | + | Applies an opaque (white) alpha channel to a clip. The alpha channel of an [[RGB32]] clip is not always well-defined, depending on the source (it may contain random data); this filter is a fast way to apply an all-white mask. |
− | {{ | + | {{FuncDef|ResetMask(clip ''clip'')}} |
== ColorKeyMask == | == ColorKeyMask == | ||
− | Clears pixels in the alpha | + | Clears pixels in the alpha channel by comparing to a transparent {{FuncArg|color}} (default black). |
− | + | ||
− | + | ||
− | + | Each pixel with a color differing less than ({{FuncArg|tolB}},{{FuncArg|tolR}},{{FuncArg|tolG}}) (default 10) is set to transparent (black); otherwise it is left unchanged – note, it is NOT set to opaque (white). That's why you might need '''ResetMask''' before applying this filter. This behaviour allows an aggregate mask to be constructed with multiple calls to '''ColorKeyMask'''. | |
− | + | When {{FuncArg|tolR}} or {{FuncArg|tolG}} are not set, they get the value of {{FuncArg|tolB}} by default. Normally you start with a '''ResetMask''', then chain a few calls to '''ColorKeyMask''' to cause transparent holes where each color of interest occurs. See [[Overlay]] for examples. | |
− | {{ | + | {{FuncDef|ColorKeyMask(clip ''clip'', int ''color'' [, int ''tolB'', int ''tolG'', int ''tolR''])}} |
:{{Par2|clip|clip|}} | :{{Par2|clip|clip|}} | ||
Line 148: | Line 156: | ||
:{{Par2|tolB|int|10}} | :{{Par2|tolB|int|10}} | ||
− | :{{Par2|tolG|int|tolB}} | + | :{{Par2|tolG|int|(tolB)}} |
− | :{{Par2|tolR|int|tolB}} | + | :{{Par2|tolR|int|(tolB)}} |
::Color tolerance. See description above. | ::Color tolerance. See description above. | ||
Revision as of 04:51, 29 January 2016
Contents |
Layer
Later (aka overlay, blend, merge) two clips of possibly different sizes, but with the same color format.
For pixel-wise transparency information the alpha channel of an RGB32 overlay_clip is used as a mask.
Layer( clip base_clip, clip overlay_clip [, string op, int level, int x, int y, int threshold, bool use_chroma ] )
- clip base_clip =
- The underlying clip, which determines the size and all other video and audio properties of the result.
- clip overlay_clip =
- The clip which is merged onto clip.
- string op = "add"
- The merge operation to be performed, which can be one of the following:
Op Description add This is the default mode. Equivalent to Overlay(mode="blend").
overlay_clip will be copied on top of the original, in proportion to level, and subject to the alpha channel.
subtract base_clip minus overlay_clip.
If both clips are equal and level=128, a flat gray field is returned; compare to Subtract.
lighten Copy overlay_clip over base_clip in areas where overlay_clip is lighter. darken Copy overlay_clip over base_clip in areas where overlay_clip is darker. fast Like add, but without masking. mul base_clip multiplied by overlay_clip. This will generally make the output darker (see GIMP: Multiply).
- int level = (maximum)
- The strength of the performed operation:
- 0 – no effect: base_clip is returned unchanged
- 257 (256 for YUY2) – maximum strength
- The strength of the performed operation:
- int x = 0
- int y = 0
- offset position of overlay_clip
- int threshold = 0
- Higher values make the op="darken" and op="lighten" effect more sensitive. See example images below.
- bool use_chroma = true
- Use chroma of the overlay_clip. Default=true. When false, only luma is used.
Audio, FrameRate and FrameCount are taken from the first clip.
There are some differences in the behaviour and the allowed parameter depending on the color format and the operation; here are the details:
- There is no mask (alpha channel) in YUY2, so the alpha channel is assumed to be fully opaque (255) everywhere.
- In RGB32 the overlay_clip alpha channel is multiplied with level, so the resulting alpha = (alpha_mask * level + 1) / 256.
This means for full strength of operation, alpha has to be 255 and level has to be 257.
- fast: use_chroma must be true; level and threshold are not used. The result is simply the average of base_clip and overlay_clip.
- add: threshold is not used. The difference between base_clip and overlay_clip is multiplied with alpha and added to base_clip.
- alpha=0 → only base_clip visible
- alpha=128 → base_clip and overlay_clip equally blended
- alpha=255 → only overlay_clip visible
- Formula used :
- subtract: the same as add, but overlay_clip is inverted before adding.
- These operators seem to work correctly only in YUY2: [todo: check and update documentation]
- mul: threshold is not used. base_clip is colored as overlay_clip, so use_chroma should be true.
- alpha=0 → only base_clip visible.
- alpha=255 → approx. the same luminance as base_clip but with the colors of overlay_clip.
- lighten: use_chroma must be true. Performs the same operation as add, but only when overlay_clip is BRIGHTER than base_clip. With a higher threshold the operation is more likely, so with threshold=255 is the same as add; with threshold=0, base_clip is more likely passed unchanged, depending on the difference between base_clip and overlay_clip.
- darken: the same as "lighten", but it is performed only when overlay_clip is DARKER than base_clip.
Examples
- Demonstration of lighten, darken and threshold:
ImageSource("Lenna.png") ConvertToYUY2 Layer(BlankClip(Last, color=color_blue), op="lighten", threshold=40) Layer(BlankClip(Last, color=color_yellow), op="darken", threshold=40)
threshold = 0 (default) | threshold = 40 |
- Layer can be used to combine two captures of different broadcasts for reducing noise. A discussion of this idea can be found here. Of course you have to ensure that the frames of the two clips match exactly -- use Trim or DeleteFrame as necessary. A sample script:
clip1 = AviSource("capture-pass-1.avi").ConvertToYUY2 clip2 = AviSource("capture-pass-2.avi").ConvertToYUY2.Trim(101, 0) return Layer(clip1, clip2, "fast")
Mask
Applies a defined alpha-mask to a clip, by converting RGB32-only mask_clip to greyscale and copying that greyscale information to the alpha channel of RGB32-only clip. In the alpha channel, "black" means transparent and "white" means opaque. For those of you who familiar with Photoshop masks, the concept is the same. In fact you can create a black and white photo in Photoshop, load it in your script and use it as a mask.
Mask(clip clip, clip mask_clip)
Examples
Suppose "ss.jpg" is a snapshot from a video clip, which served as a guideline to create the mask using Paint. We use ImageSource to load the image in the script and Mask to apply it.
bg = AviSource("01gray.avi").ConvertToRGB32 # here is the background clip mk = ImageSource("ss.jpg").ConvertToRGB32 # load the image top = AviSource("k3.avi").ConvertToRGB32.Mask(mk) # load the top layer clip and apply the mask to it Layer(bg, top) # layer the background and the top layer with mask
ResetMask
Applies an opaque (white) alpha channel to a clip. The alpha channel of an RGB32 clip is not always well-defined, depending on the source (it may contain random data); this filter is a fast way to apply an all-white mask.
ResetMask(clip clip)
ColorKeyMask
Clears pixels in the alpha channel by comparing to a transparent color (default black).
Each pixel with a color differing less than (tolB,tolR,tolG) (default 10) is set to transparent (black); otherwise it is left unchanged – note, it is NOT set to opaque (white). That's why you might need ResetMask before applying this filter. This behaviour allows an aggregate mask to be constructed with multiple calls to ColorKeyMask.
When tolR or tolG are not set, they get the value of tolB by default. Normally you start with a ResetMask, then chain a few calls to ColorKeyMask to cause transparent holes where each color of interest occurs. See Overlay for examples.
ColorKeyMask(clip clip, int color [, int tolB, int tolG, int tolR])
- clip clip =
- Source clip.
- int color = black
- Transparent color. Default black.
- int tolB = 10
- int tolG = (tolB)
- int tolR = (tolB)
- Color tolerance. See description above.