Layer

From Avisynth wiki
Revision as of 17:21, 2 January 2016 by Admin (Talk | contribs)

Jump to: navigation, search

Contents

Layer

This filter can overlay two clips of different sizes (but with the same color format) using different operation modes. For pixel-wise transparency information the 4th color channel of RGB32 (A- or alpha-channel) 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"
Name of 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"). The 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 - or to say it in another way: base_clip plus negative overlay_clip. If both clips are equal and level=128, a flat gray field is returned; compare to Subtract.
lighten This will copy the overlay_clip to the base_clip in areas where the overlay_clip is lighter than the base_clip.
darken This will copy the overlay_clip to the base_clip in areas where overlay_clip is darker than the base_clip.
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: the base_clip is returned unchanged
  • 257 (256 for YUY2) - maximum strength
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.


There are some differences in the behaviour and the allowed parameter depending on the color format and the operation and here are the details:

  • There is no mask (alpha-channel) in YUY2, so the alpha-channel is assumed to be fully opaque everywhere.
  • In RGB32 the alpha-channel of the overlay_clip 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.


These operators behave equally for RGB32 or YUY2:

fast: use_chroma must be TRUE, level and threshold is 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 the 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 :-

  • RGB:  Layer-Eq1.png
  • YUY2: Layer-Eq2.png

subtract: the same as add, but the overlay_clip is inverted before.


These operators seem to work correctly only in YUY2: [todo: check and update documentation]

mul: threshold is not used. The 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 but with the colors of Overlay.

lighten: use_chroma must be TRUE. Performs the same operation as add, but only when the result is BRIGHTER than the base the new values is used. With a higher threshold the operation is more likely, so with threshold=255 it's the same as "add", with threshold=0 the 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 the result is DARKER than the base.

The audio and framerate/framecount are taken from the first 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)
Lenna-Layer-lighten-blue+darken-yellow,threshold=0(default).jpg Lenna-Layer-lighten-blue+darken-yellow,threshold=40.jpg
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 clip, for use with Layer, by converting mask_clip to greyscale and using that for the mask (the alpha-channel) of RGB32. In this channel "black" means completely transparent and "white" means completely 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

ss.jpg is derived from a snapshot from a video clip, which served as a guideline to create the mask just 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 clip with the mask


ResetMask

Applies an "all-opaque" (that is white) alpha-mask to clip, for use with Layer. The alpha-channel of a RGB32-clip is not always well-defined (depending on the source), this filter is the faster 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 (that is black), otherwise it is left unchanged i.e. It is NOT set to opaque - that's why you might need ResetMask before applying this filter. This allows a aggregate mask to be constructed with multiple calls.

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.

For AviSynth versions older than v2.58, there were no separate tolerance levels for blue, green and red. There was only one tolerance level called "tolerance" and this was used for blue, green and red simultaneously.

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.
Personal tools