logo Sign In

Post #615157

Author
g-force
Parent topic
Info: New colormatching script
Link to post in topic
https://originaltrilogy.com/post/id/615157/action/topic#615157
Date created
16-Dec-2012, 12:31 PM

Here is an avisynth script that I worked up that TRIES to match the colors of one source with another. Your mileage may vary. Feel free to comment, leave feedback, suggestions, examples etc. here.

Function RGBMatch(clip gref, clip bref, clip “input”, int “mix”)

{# v1.2 - by G-force

Make Red, Green and Blue channel average, (and min and max) of second clip match first clip

or optionally, make third clip match difference between first two

i.e., if the cropping is different between the sources, you could crop the sources first<

to better match each other and then only apply the result to the third (uncropped) clip

mix = how much min/max compensation to do, 0=none (average only), 255=fully match min/max (default)

input  = Default(input,bref)</span>

mix    = Default(mix,255)          </span>

global gref = gref

global bref = bref

global input = input

global mix = mix

scriptclip(bref,"""

grefrgb=gref.ConvertToRGB()

brefrgb=bref.ConvertToRGB()

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>inputrgb=input.ConvertToRGB()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>grefr = grefrgb.ShowRed().ConvertToYV12()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>grefg = grefrgb.ShowGreen().ConvertToYV12()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>grefb = grefrgb.ShowBlue().ConvertToYV12()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>brefr = brefrgb.ShowRed().ConvertToYV12()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>brefg = brefrgb.ShowGreen().ConvertToYV12()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>brefb = brefrgb.ShowBlue().ConvertToYV12()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>inr = inputrgb.ShowRed().ConvertToYV12()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>ing = inputrgb.ShowGreen().ConvertToYV12()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>inb = inputrgb.ShowBlue().ConvertToYV12()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>y1r = AverageLuma(grefr)</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>y1g = AverageLuma(grefg)</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>y1b = AverageLuma(grefb)</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>y2r = ((255-mix)0+mixYplanemin(grefr))/255</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>y2g = ((255-mix)0+mixYplanemin(grefg))/255</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>y2b = ((255-mix)0+mixYplanemin(grefb))/255</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>y3r = ((255-mix)255+mixYplanemax(grefr))/255</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>y3g = ((255-mix)255+mixYplanemax(grefg))/255</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>y3b = ((255-mix)255+mixYplanemax(grefb))/255</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>x1r = AverageLuma(brefr)</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>x1g = AverageLuma(brefg)</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>x1b = AverageLuma(brefb)</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>x2r = ((255-mix)0+mixYplanemin(brefr))/255</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>x2g = ((255-mix)0+mixYplanemin(brefg))/255</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>x2b = ((255-mix)0+mixYplanemin(brefb))/255</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>x3r = ((255-mix)255+mixYplanemax(brefr))/255</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>x3g = ((255-mix)255+mixYplanemax(brefg))/255</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>x3b = ((255-mix)255+mixYplanemax(brefb))/255</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>#compute coefficients for y=ax^2+bx+c using min, avg, max</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>ar  = x1r==x2r? 0: x1r==x3r? 0: x2r==x3r? 0:</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”> \ (y3r-y1r+(x1r-x3r)(y1r-y2r)/(x1r-x2r))/((x1r-x3r)(x2r-x3r))</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>br  = x1r==x2r? 0:</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”> \ ((y2r-y1r)/(x2r-x1r))-ar*(x1r+x2r)</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>cr  = y1r-arx1rx1r-br*x1r</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>ag  = x1g==x2g? 0: x1g==x3g? 0: x2g==x3g? 0:</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”> \ (y3g-y1g+(x1g-x3g)(y1g-y2g)/(x1g-x2g))/((x1g-x3g)(x2g-x3g))</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>bg  = x1g==x2g? 0:</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”> \ ((y2g-y1g)/(x2g-x1g))-ag*(x1g+x2g)</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>cg  = y1g-agx1gx1g-bg*x1g</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>ab  = x1b==x2b? 0: x1b==x3b? 0: x2b==x3b? 0:</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”> \ (y3b-y1b+(x1b-x3b)(y1b-y2b)/(x1b-x2b))/((x1b-x3b)(x2b-x3b))</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>bb  = x1b==x2b? 0:</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”> \ ((y2b-y1b)/(x2b-x1b))-ab*(x1b+x2b)</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>cb  = y1b-abx1bx1b-bb*x1b</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>inr=inr.MT_lut(“x x * “+string(ar)+” * x “+string(br)+” * + “+string(cr)+” +”).ConvertToRGB()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>ing=ing.MT_lut(“x x * “+string(ag)+” * x “+string(bg)+” * + “+string(cg)+” +”).ConvertToRGB()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>inb=inb.MT_lut(“x x * “+string(ab)+” * x “+string(bb)+” * + “+string(cb)+” +”).ConvertToRGB()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>MergeRGB(inr,ing,inb)</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>ConvertToYV12()</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>""")</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>
</span>

<span style=“font-size: 11px; line-height: 13.666666984558105px;”>Return(last)}</span>