Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage

Reply
 
Thread Tools Search this Thread Display Modes
Old 23rd November 2020, 15:37   #1  |  Link
VoodooFX
Banana User
 
VoodooFX's Avatar
 
Join Date: Sep 2008
Posts: 985
Help me with math

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).

Last edited by VoodooFX; 24th November 2020 at 14:23. Reason: now it should be described properly (once again)
VoodooFX is offline   Reply With Quote
Old 23rd November 2020, 16:49   #2  |  Link
johnmeyer
Registered User
 
Join Date: Feb 2002
Location: California
Posts: 2,691
The "Max" function is what I think you're looking for. Example:


Code:
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)
johnmeyer is offline   Reply With Quote
Old 23rd November 2020, 17:04   #3  |  Link
Kisa_AG
Registered User
 
Join Date: Sep 2005
Location: Moscow, Russia
Posts: 65
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)

Last edited by Kisa_AG; 23rd November 2020 at 17:39. Reason: Correction due to nwe input
Kisa_AG is offline   Reply With Quote
Old 23rd November 2020, 20:35   #4  |  Link
VoodooFX
Banana User
 
VoodooFX's Avatar
 
Join Date: Sep 2008
Posts: 985
Quote:
Originally Posted by Kisa_AG View Post
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

Last edited by VoodooFX; 24th November 2020 at 14:23.
VoodooFX is offline   Reply With Quote
Old 24th November 2020, 15:11   #5  |  Link
Kisa_AG
Registered User
 
Join Date: Sep 2005
Location: Moscow, Russia
Posts: 65
Quote:
Originally Posted by VoodooFX View Post
Wow, Russians have a good head for numbers.
Sometimes...

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

Code:
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)
Kisa_AG is offline   Reply With Quote
Old 24th November 2020, 15:46   #6  |  Link
VoodooFX
Banana User
 
VoodooFX's Avatar
 
Join Date: Sep 2008
Posts: 985
Quote:
Originally Posted by Kisa_AG View Post
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 is offline   Reply With Quote
Old 24th November 2020, 18:52   #7  |  Link
VoodooFX
Banana User
 
VoodooFX's Avatar
 
Join Date: Sep 2008
Posts: 985
Looks like stuff works, "Buffer" should be highest val of "trimy", probably there is a simpler way.
Code:
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 in this thread Detect Position of Watermark in movie and remove with logo inpaint, 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!

Last edited by VoodooFX; 28th November 2020 at 17:05.
VoodooFX is offline   Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 18:03.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.