Log in

View Full Version : Mask not working


alexh110
11th July 2025, 22:58
Hi to all!

Have been trying to write an Avisynth script this week with the help of Bing AI.

Unfortunately it seems to be terrible at coding, producing one syntax error after another!

The code is supposed to identify all the pixels whose chroma falls within a user defined quadrant of PAL colourspace, and reflect the chroma in the U and/or V axes to map them into the user defined target quadrant.

All other chroma in the image should remain unchanged.

Currently the mask is completely unpopulated: so it's not identifying the pixels that need modifying.
Consequently my output pane looks identical to my input pane.

I've attached the script below.
Really hope one of you can help, because it's driving me nuts!


# === Load Plugins ===
LoadPlugin("ffms2.dll")
LoadPlugin("MaskTools2.dll")

# === USER CONFIGURATION ===
input_file = "input.mkv"
start_frame = 16000
end_frame = 18000
side_by_side = true
show_frame_num = true

# === Chroma Quadrant Mapping ===
src_U_pos = false
src_V_pos = false
dst_U_pos = true
dst_V_pos = true

# === Load and Prepare Source ===
src_hd = FFmpegSource2(input_file)
src_trim = Trim(src_hd, start_frame, end_frame)
src_yuv = ConvertToYV12(src_trim).LanczosResize(720, 576)
Y = ExtractY(src_yuv)
U = UToY(src_yuv).ConvertToY8()
V = VToY(src_yuv).ConvertToY8()

# === Determine Chroma Flip Conditions Based on Quadrant Mismatch ===
needs_flip_U = src_U_pos != dst_U_pos
needs_flip_V = src_V_pos != dst_V_pos

# === Build Source Quadrant Mask ===

# Define U and V conditions based on quadrant position in chroma color space

# U condition: does U value fall in positive or negative half of UV space?
u_mask = mt_lut(U, expr=src_U_pos ? "x 128 > 255 0 ?" : "x 128 < 255 0 ?", chroma="none")

# V condition: does V value fall in positive or negative half of UV space?
v_mask = mt_lut(V, expr=src_V_pos ? "x 128 > 255 0 ?" : "x 128 < 255 0 ?", chroma="none")

# Logical AND to isolate pixels where both conditions are true
quad_mask = mt_logic(u_mask, v_mask, "and")

# Binarize for clean merge application
quad_mask_bin = mt_binarize(quad_mask, threshold=1)

# === Flip U/V if Needed ===
reflect_x_rpn = mt_polish("(x - 128) * -1 + 128")
reflect_y_rpn = mt_polish("(y - 128) * -1 + 128")

U_flipped = needs_flip_U ? mt_lutxy(U, V, expr=reflect_x_rpn, chroma="none") : U
V_flipped = needs_flip_V ? mt_lutxy(V, U, expr=reflect_y_rpn, chroma="none") : V

# === Apply Masked Merge Only Within Source Quadrant ===
U_mapped = needs_flip_U ? mt_merge(U, U_flipped, quad_mask_bin).ConvertToY8() : U
V_mapped = needs_flip_V ? mt_merge(V, V_flipped, quad_mask_bin).ConvertToY8() : V

# === Resize Chroma Planes to Match Y ===
U_mapped = U_mapped.BilinearResize(720, 576)
V_mapped = V_mapped.BilinearResize(720, 576)

# === Combine Corrected Frame ===
corrected = CombinePlanes(Y, U_mapped, V_mapped, planes="YUV")

# === Diagnostics Overlay (optional) ===
final_rgb = corrected

# === Output Display ===
left = ConvertToRGB(src_yuv)
right = ConvertToRGB(final_rgb)

return side_by_side ? StackHorizontal(left, right) : right

alexh110
13th July 2025, 18:13
I have managed to resolve this now, with the help of Microsoft Copilot.
So no need to respond.