Log in

View Full Version : Script to modify U and V values in each pixel


alexh110
10th February 2013, 19:44
Was wondering if it would be possible to implement the following process as an Avisynth script using the new AVSLib and GScript extensions:

Starting with a DVD vob file originally sourced from an analogue PAL tape, I would like to populate a pair of arrays u(x,y) and v(x,y) with the PAL U and V values of each pixel in the first frame.

Then execute the following code to modify these values:

FOR y = 1 TO 576
FOR x = 1 TO 720
N(u(x, y)+k, v(x, y)+k) = N(u(x, y)+k, v(x, y)+k) + 1
NEXT x
NEXT y

FOR y1 = 1 TO 576
FOR x1 = 1 TO 720
FOR y = 1 TO 576
FOR x = 1 TO 720

IF u(x1,y1) = -u(x,y) AND v(x1,y1) = v(x,y) AND N(u(x1,y1)+k,v(x1,y1)+k) < N(u(x,y)+k,v(x,y)+k) THEN
u(x1,y1) = u(x,y)
GOTO cont
END IF

IF u(x1,y1) = -u(x,y) AND v(x1,y1) = -v(x,y) AND N(u(x1,y1)+k,v(x1,y1)+k) < N(u(x,y)+k,v(x,y)+k) THEN
u(x1,y1) = u(x,y)
v(x1,y1) = v(x,y)
GOTO cont
END IF

IF u(x1,y1) = u(x,y) AND v(x1,y1) = -v(x,y) AND N(u(x1,y1)+k,v(x1,y1)+k) < N(u(x,y)+k,v(x,y)+k) THEN
v(x1,y1) = v(x,y)
GOTO cont
END IF

NEXT x
NEXT y
cont:
NEXT x1
NEXT y1


(I wrote this in QBASIC for my own convenience, as I'm not familiar with other programming languages.)
The first block of code simply counts the number of pixels of each colour in the frame: so each element of the array N(u,v) corresponds to a different colour. N(u,v) would be dimensioned to contain all possible colours in the PAL palette.
The constant k would be set to a value that would shift all the negative u and v values to become positives, so we don't end up with negative indices in the array.

The whole process would be repeated for each frame in the footage, and the modified U and V values would be used to produce the output video stream.

alexh110
10th February 2013, 23:53
Thanks, but sadly I don't know C++ or Pascal, and don't have the time or patience to learn.
Was hoping it would be possible to use AviSynth's scripting language alone, given that the new extensions allow if-then statements, for-next loops and arrays.

alexh110
11th February 2013, 12:03
Looks like I'm out of luck then.
Just out of interest, how would I extract the U and V information from a vob file using Avisynth? Or is that not possible?

jmac698
12th February 2013, 19:06
Hi,
You are in exactly the same situation I have been for years. There's a real need to do pixel level manipulation. Now it can be done in Vapoursynth, but I haven't even tried it yet and Python just gives me a headache at the moment.
I eventually learned how to write a plugin to do what I wanted.
I believe a pure Avisynth solution may be possible for your problem.
I've come up with ways of summing across lines, counting and so on.
If I get time I can try it.

IanB
12th February 2013, 23:17
I can't see an obvious way to histogram the chroma space then use the relative counts to flip the chroma pixels sign. You are really well into needing a plugin to do that sort of transform.

For the algorithm an obvious improvement based on your condition IF u(x1,y1) = -u(x, y) AND v(x1,y1) = v(x, y) AND ... is to just directly access the count for the flipped chroma. You know the count for the current pixel has to be at least 1 because you are currently pointing at one of them and seeing you are testing for strictly less than you only act if the flipped count is suitable. So :-FOR y = 1 TO 576
FOR x = 1 TO 720
IF N(u(x, y)+k,v(x, y)+k) < N(-u(x, y)+k, v(x, y)+k) THEN
u(x, y) = -u(x, y)
GOTO cont
END IF

IF N(u(x, y)+k,v(x, y)+k) < N(-u(x, y)+k, -v(x, y)+k) THEN
u(x, y) = -u(x, y)
v(x, y) = -v(x, y)
GOTO cont
END IF

IF N(u(x, y)+k,v(x, y)+k) < N(u(x, y)+k, -v(x, y)+k) THEN
v(x, y) = -v(x, y)
GOTO cont
END IF
cont:
NEXT x
NEXT yAn even faster solution would be to scan the histogram counts and produce a newU and newV mapping table. Thus :-FOR U = -112 TO 112
FOR V = -112 TO 112
IF N(U+k,V+k) < N(-U+k, V+k) THEN
newU(U+k, V+k) = -U
newV(U+k, V+k) = V
GOTO cont
END IF

IF N(U+k,V+k) < N(-U+k, -V+k) THEN
newU(U+k, V+k) = -U
newV(U+k, V+k) = -V
GOTO cont
END IF

IF N(U+k,V+k) < N(-U+k, -V+k) THEN
newU(U+k, V+k) = U
newV(U+k, V+k) = -V
GOTO cont
END IF

newU(U+k, V+k) = U
newV(U+k, V+k) = V
cont:
NEXT V
NEXT Uand thenFOR y = 1 TO 576
FOR x = 1 TO 720
u(x, y) = newU(u(x, y)+k, v(x, y)+k)
v(x, y) = newV(u(x, y)+k, v(x, y)+k)
NEXT x
NEXT y