Log in

View Full Version : Getting results from compare


carrot691
3rd March 2009, 05:44
I am trying to find a frame that match an other video using compare(). The problem seems to be that the returned value of compare is a frame, but I would need value...

Ex :
psnr = compare(video, frame)
if psnr > 40 then "I don't know yet".

This is the be able to align to video on the same frame without having to do it manually.

Maybe I'm just on the wrong track, any pointers would help. Thanks.

Gavino
8th March 2009, 18:24
I'm not aware of a function that returns the PSNR. However, here's a way of doing basically what you ask:

# given a clip 'c' and a (potentially one-frame) clip 'f',
# show 'c' starting at frame matching (1st frame of) 'f'.
function findFrame(clip c, clip f, int "thresh") {
thresh = Default(thresh, 2) # threshold for declaring match
f = f.Loop(c.FrameCount, 0, 0)
n = findFrame2(c, f, thresh, 0)
return c.Trim(n, 0)
}

# recursive auxiliary function called by findFrame
function findFrame2(clip c, clip f, int thresh, int current_frame) {
current_frame >= c.FrameCount-1 || LumaDifference(c, f) <= thresh ?
\ current_frame :
\ findFrame2(c, f, thresh, current_frame+1)
}

# contrived example for testing:
c = AviSource("myVid.avi").ShowFrameNumber()
f = c.Trim(30, -1) #select frame 30

findFrame(c, f) # should show starting at frame 30
It uses LumaDifference to detect matching frames (use RGBDifference if you have RGB).
The threshold parameter can be amended to suit how close a match you have or require.
If no match is found, the last frame will be shown.