digitalFAQ.com Forum

digitalFAQ.com Forum (https://www.digitalfaq.com/forum/)
-   Restore, Filter, Improve Quality (https://www.digitalfaq.com/forum/video-restore/)
-   -   Problem with custom script in Avisynth to stabilize video? (https://www.digitalfaq.com/forum/video-restore/8541-problem-custom-script.html)

benzio 03-07-2018 11:27 AM

Problem with custom script in Avisynth to stabilize video?
 
Hello!
I would like to stabilize an interlaced video in this way: I want to avoid the single fields shifted up or down, probabily due to tracking problems in the vcr.

My idea is this:
I separate the fields first, and then if a frame shifted UP, DOWN, LEFT or RIGHT has an average difference from the previous two frames and the next two frames always lesser than the difference of the actual frame and the previous/next frames, then the frame has to be shifted in that direction by one pixel. And I would go recursive to have a radius of possible movement.

I created this function and I cannot tell what's happening. Sometimes it working (but only if the radius is 1), sometimes gives an error, sometimes makes virtualdub crash, and sometimes shifts the frame in the wrong direction when the radius is greater than one.
I'm getting mad about this!

I believe that is a variable scope problem (?) but i'm not sure about this.

Could you help me to spot the problem in my code please?



Code:

VIDEO1 = avisource("F:\DEVCR1_3 (piccol).AVI").assumeTFF().killaudio()
video = VIDEO1.SeparateFields()
video.removeTrackingSpikes(3)

function removeTrackingSpikes(clip source, int radius){
newvideo = pointresize(source, source.width,source.height*2).converttoyv12()

olddifferenceplus1 = Overlay(newvideo, trim(newvideo,1,0), mode="subtract").grayscale()
olddifferenceplus2 = Overlay(newvideo, trim(newvideo,2,0), mode="subtract").grayscale()
olddifferenceminus1 = Overlay(newvideo, trim(newvideo,0,1)+newvideo, mode="subtract").grayscale()
olddifferenceminus2 = Overlay(newvideo, trim(newvideo,0,2)+newvideo, mode="subtract").grayscale()

verticalminus1 = pointresize(newvideo, newvideo.width, newvideo.height, 0, -2)
newdifferenceplusb1 = Overlay(verticalminus1, trim(newvideo,1,0), mode="subtract").grayscale()
newdifferenceplusb2 = Overlay(verticalminus1, trim(newvideo,2,0), mode="subtract").grayscale()
newdifferenceminusb1 = Overlay(verticalminus1, trim(newvideo,0,1)+newvideo, mode="subtract").grayscale()
newdifferenceminusb2 = Overlay(verticalminus1, trim(newvideo,0,2)+newvideo, mode="subtract").grayscale()
newvideo = conditionalfilter(newvideo, verticalminus1, newvideo, "newdifferenceplusb1.averageLuma()+0.5 < olddifferenceplus1.averageLuma() && newdifferenceplusb2.averageLuma()+0.5 < olddifferenceplus2.averageLuma() && newdifferenceminusb1.averageLuma()+0.5 < olddifferenceminus1.averageLuma() && newdifferenceminusb2.averageLuma()+0.5 < olddifferenceminus2.averageLuma()", "equals", "true")

verticalplus1 = pointresize(newvideo, newvideo.width, newvideo.height, 0, 2)
newdifferenceplusb1 = Overlay(verticalplus1, trim(newvideo,1,0), mode="subtract").grayscale()
newdifferenceplusb2 = Overlay(verticalplus1, trim(newvideo,2,0), mode="subtract").grayscale()
newdifferenceminusb1 = Overlay(verticalplus1, trim(newvideo,0,1)+newvideo, mode="subtract").grayscale()
newdifferenceminusb2 = Overlay(verticalplus1, trim(newvideo,0,2)+newvideo, mode="subtract").grayscale()
newvideo = conditionalfilter(newvideo, verticalplus1, newvideo, "newdifferenceplusb1.averageLuma()+0.5 < olddifferenceplus1.averageLuma() && newdifferenceplusb2.averageLuma()+0.5 < olddifferenceplus2.averageLuma() && newdifferenceminusb1.averageLuma()+0.5 < olddifferenceminus1.averageLuma() && newdifferenceminusb2.averageLuma()+0.5 < olddifferenceminus2.averageLuma()", "equals", "true")

horizontalminus1 = pointresize(newvideo, newvideo.width, newvideo.height, -2, 0)
newdifferenceplusc1 = Overlay(horizontalminus1, trim(newvideo,1,0), mode="subtract").grayscale()
newdifferenceplusc2 = Overlay(horizontalminus1, trim(newvideo,2,0), mode="subtract").grayscale()
newdifferenceminusc1 = Overlay(horizontalminus1, trim(newvideo,0,1)+newvideo, mode="subtract").grayscale()
newdifferenceminusc2 = Overlay(horizontalminus1, trim(newvideo,0,2)+newvideo, mode="subtract").grayscale()
newvideo = conditionalfilter(newvideo, horizontalminus1, newvideo, "newdifferenceplusc1.averageLuma()+0.5 < olddifferenceplus1.averageLuma() && newdifferenceplusc2.averageLuma()+0.5 < olddifferenceplus2.averageLuma() && newdifferenceminusc1.averageLuma()+0.5 < olddifferenceminus1.averageLuma() && newdifferenceminusc2.averageLuma()+0.5 < olddifferenceminus2.averageLuma()", "equals", "true")

horizontalplus1 = pointresize(newvideo, newvideo.width, newvideo.height, 2, 0)
newdifferenceplusd1 = Overlay(horizontalplus1, trim(newvideo,1,0), mode="subtract").grayscale()
newdifferenceplusd2 = Overlay(horizontalplus1, trim(newvideo,2,0), mode="subtract").grayscale()
newdifferenceminusd1 = Overlay(horizontalplus1, trim(newvideo,0,1)+newvideo, mode="subtract").grayscale()
newdifferenceminusd2 = Overlay(horizontalplus1, trim(newvideo,0,2)+newvideo, mode="subtract").grayscale()
newvideo = conditionalfilter(newvideo, horizontalplus1, newvideo, "newdifferenceplusd1.averageLuma()+0.5 < olddifferenceplus1.averageLuma() && newdifferenceplusd2.averageLuma()+0.5 < olddifferenceplus2.averageLuma() && newdifferenceminusd1.averageLuma()+0.5 < olddifferenceminus1.averageLuma() && newdifferenceminusd2.averageLuma()+0.5 < olddifferenceminus2.averageLuma()", "equals", "true")

newvideo = newvideo.converttoyuy2().pointresize(newvideo.width,newvideo.height/2)

return (radius == 1) ? newvideo :  removeTrackingSpikes(newvideo, radius-1)
}



All times are GMT -5. The time now is 08:44 PM

Site design, images and content © 2002-2024 The Digital FAQ, www.digitalFAQ.com
Forum Software by vBulletin · Copyright © 2024 Jelsoft Enterprises Ltd.