Log in

View Full Version : Mask function help


Dogway
7th January 2011, 13:35
Im trying to make a function for masking based on luma (from this post (http://forum.doom9.org/showpost.php?p=761786&postcount=3)). I tried to convert it to masktools2 and inverse the mask effect (now it affects to bright areas). I would like to make a bool operator for chossing whether bright or dark areas, and another one where I can define by a string the filter I want to apply to chosen area.


function LumaMask(clip input, int "black", int "white", string "lumamask", string "filter"){

LO = string(default(black, 24))
HI = string(default(white, 48))
luma =lumamask
filt = string(filter)

darkM = input.mt_lut("x "+LO+" < 255 x "+HI+" > 0 255 x "+LO+" - 255 "+HI+" "+LO+" - / * - ? ?",U=2,V=2)
brightM = input.mt_lut("x "+LO+" < 0 x "+HI+" > 255 0 x "+LO+" - 255 "+LO+" "+HI+" - / * - ? ?",U=2,V=2)

filter = input.filt

mt_merge(input, filter, luma, U=3,V=3)
return last
}

Didée
7th January 2011, 14:35
You're thinking too complicated. :)

To reverse the whole logic to "leave dark untouched, filter bright", you just swap the clip arguments of mt_merge.

FitY2UV doesn't exist anymore. In exchange, mt_merge now has the toggle "luma=true/false". When true, luma mask is used for the UV channels, too.

String evaluation is of course possible. But why? I don't see any benefit to declare filtersting="""MyFilter(with,"some",parameter)""" before, and use some doggerel result = Eval( string(clip) + filterstring ) in the function, when you can as well directly use a clip: filter=MyFilter(with,"some",parameter), and simply use result = filter in the function.

(You need string evaluation if you want to apply a filter to something that is constructed only in the function. But not for a basic input clip that gets used as-is.)

function drkbrgtfltr(clip input, clip filtered, int "black", int "white", bool "darkfilt")
{
LO = string(default(black, 24))
HI = string(default(white, 48))
darkfilt = default(darkfilt,true)

darkmask = input.mt_lut("x "+LO+" < 255 x "+HI+" > 0 255 x "+LO+" - 255 "+HI+" "+LO+" - / * - ? ?",U=1,V=1)

darkfilt ? mt_merge(input, filtered, darkmask, luma=true, U=3,V=3)
\ : mt_merge(filtered, input, darkmask, luma=true, U=3,V=3)
return last
}

Dogway
7th January 2011, 15:00
Oh thanks for the input! looks I was close... :P the first thing I tried was swaping but I got a green screen that's why all the thinking afterwards. Yes, define the filter outside function might be better, less hassle I guess.

Gavino
7th January 2011, 15:15
Yes, define the filter outside function might be better, less hassle I guess.
I agree, but if you really want to pass in a filter string, the simplest way to do it would be to replace "clip filtered" by "string filterString" in the function definition, then at the start of the function, add
input
filtered=Eval(filterString)