Log in

View Full Version : Help me with math


VoodooFX
23rd November 2020, 15:37
We have defined positive integers X and Y (Y is lesser than X).
X-Y=Z
Then Z is a scale factor and a start value in a geometric sequence with common ratio 2, sequence goes till sum of sequence is not higher than X, let's call that last good sum of sequence - A (if sum with second number in a sequence is > X then A = Z).
B is the highest number from (X-A) and A.

How you would write Avisynth script to get B from X and Y?

EDIT (correct last line):
B is the highest number from (X-A) and An ("An" is the last number in the geometric sequence).

johnmeyer
23rd November 2020, 16:49
The "Max" function is what I think you're looking for. Example:


Source2 = (FILEWRITE)
\ ? WriteFileIf(source, filename, "
\ YDifferenceFromPrevious() / Max(YDifferenceFromPrevious( selectevery(1, -1)),0.00001) > badthreshold &&
\ YDifferenceToNext() / Max(YDifferenceToNext(selectevery(1, 1)),0.00001)>badthreshold", "current_frame", append = false)

Kisa_AG
23rd November 2020, 17:04
clp=BlankClip

X=4000
Y=1000
Z=X-Y

N=log(1+float(X)/float(Z))/log(2)

NN=Int(N)

A=int(Z*(Pow(2,NN)-1))

B=(X-A>A) ? X-A : A

clp

Subtitle( \
"X="+string(X)+"\n"+ \
"Y="+string(Y)+"\n"+ \
"Z="+string(Z)+"\n"+ \
"N="+string(N)+"\n"+ \
"N Integer="+string(NN)+"\n"+ \
"A="+string(A)+"\n"+ \
"X-A="+string(X-A)+"\n"+ \
"B="+string(B)+" (highest number from (X-A) and A)", lsp=10)

VoodooFX
23rd November 2020, 20:35
clp=BlankClip

X=4000
Y=1000
Z=X-Y

N=log(1+float(X)/float(Z))/log(2)

NN=Int(N)

A=int(Z*(Pow(2,NN)-1))

B=(X-A>A) ? X-A : A

clp

Subtitle( \
"X="+string(X)+"\n"+ \
"Y="+string(Y)+"\n"+ \
"Z="+string(Z)+"\n"+ \
"N="+string(N)+"\n"+ \
"N Integer="+string(NN)+"\n"+ \
"A="+string(A)+"\n"+ \
"X-A="+string(X-A)+"\n"+ \
"B="+string(B)+" (highest number from (X-A) and A)", lsp=10)

Wow, Russians have a good head for numbers. :)
Checked and it does what I described, too bad my description was not quite correct, here is how it should end correctly:

B is the highest number from (X-A) and An ("An" is the last number in the geometric sequence).

Formula wont get numbers I need if X/2 > Y, but I know that then B=Y, so it can go straight to B with a check at the start: if X/2 >= Y then B=Y

Kisa_AG
24th November 2020, 15:11
Wow, Russians have a good head for numbers. :)

Sometimes... :)

I made correction according to your changes, try this version:

clp=BlankClip

X=4000
Y=3998
Z=X-Y

N=log(1+float(X)/float(Z))/log(2)

NN=Int(N) #number of elements in the geometric sequence

A=int(Z*(Pow(2,NN)-1)) #sum of all elements in the geometric sequence

An=int(Z*Pow(2,NN-1)) # the last number in the geometric sequence

B=(X-A>An) ? X-A : An

clp

Subtitle( \
"X="+string(X)+"\n"+ \
"Y="+string(Y)+"\n"+ \
"Z="+string(Z)+"\n"+ \
"N="+string(N)+"\n"+ \
"N Integer="+string(NN)+"\n"+ \
"A="+string(A)+"\n"+ \
"An="+string(An)+"\n"+ \
"X-A="+string(X-A)+"\n"+ \
"B="+string(B)+" (highest number from (X-A) and An)", lsp=10)

VoodooFX
24th November 2020, 15:46
I made correction according to your changes

Right on time, I just started to write function and I'll see if my description fits (hopefully). Thanks.

VoodooFX
24th November 2020, 18:52
Looks like stuff works, "Buffer" should be highest val of "trimy", probably there is a simpler way. :)

X = 300 # Sequence of frames the logo is in a location
Y = 200 # Amount of frames to use for blending (ClipBlend)

PreTune = 200 # Pre blend binarization.
PostTune = 254 # Post blend binarization. Maybe a setting not for touch!?

# Spaghetti to get the buffer size for Trimergage():
Z=X-Y
N=log(1+float(X)/float(Z))/log(2)
NN=Int(N)
A=int(Z*(Pow(2,NN)-1))
A=(NN==1) ? A*2 : A
An=int(Z*Pow(2,NN-1))
B=(X-A>An) ? X-A : An
Buffer=(X/2 >= Y) ? Y : B

LoadPlugin("D:\LSMASHSource.dll")
video=LWLibavVideoSource("D:\sample2.mkv")

video=video.RequestLinear(rlim=Y+1,clim=Y+1)
mask=video.Greyscale.mt_binarize(threshold=PreTune).ClipBlend(Y).mt_binarize(threshold=PostTune).RequestLinear(rlim=Buffer,clim=Buffer)
mask.Trimergage(X=X, Y=Y)

function Trimergage(clip clp, int "X", int "Y", int "left") {
X = default (X, 0)
Y = default (Y, 0)
left = default (left, 0)

trimy = (left == 0) ? X-Y : (X <= left) ? X : left
trimy = (left == 0 && X/2 >= Y) ? Y : trimy
left = (left == 0) ? Y - trimy : left - trimy

trm = clp.Trim(trimy, 999999)
merged = clp.MergeLuma(trm, 0.5).mt_binarize(threshold=20)
final = (left > 0 ) ? merged.Trimergage(X=trimy*2, left=left) : merged

return final
}


EDIT:
Actually, script is to get a dynamic mask for sample2 (https://forum.doom9.org/showthread.php?p=1912410#post1912410) in this thread Detect Position of Watermark in movie and remove with logo inpaint (https://forum.doom9.org/showthread.php?t=181437), later I'll add it to the spaghetti labyrinths of InpaintDelogo.

First RequestLinear is to solve ClipBlend's buffering problem, second one is for buffering problem too but for Trimergage().

EDIT2:
Script doesn't look correct... tba when it will... :)

EDIT3:
Should be correct now(I hope)...

EDIT4:
All this was a theory... in practice Buffer should be Buffer=Y!