View Single Post
Old 19th July 2011, 08:58   #1  |  Link
jmac698
Registered User
 
Join Date: Jan 2006
Posts: 1,867
Findtweak histogram matcher

This script finds the equivalent tweak statement to match two images. It's useful if there's a known transformation which is constant for the whole clips, such as two captures of the same video with different procamp settings.
It currently has a lot of problems and I'm open to comments.
Code:
#findtweak 0.1 by jmac
#Find brightness and contrast transformation
#requires masktools.  Doesn't work on real clips due to clipping. Crashes on monocolored clips.
#Two clips must be geometrically aligned. Will be confused by gamma.
#works by partitioning the clips into dim and bright pixels
#By using two measurements between original and tweaked you can find contrast and brightness
blankclip(length=99,width=64,pixel_type="YV12")
a=mt_lutspa(last,mode="absolute",expr="x 16 +")#ramp 16-79, avg=47.5
b=a.tweak(bright=15,cont=2.0,coring=false)#31-157, avg=94
cont=findtweak(a,b,0)
bright=findtweak(a,b,1)
stackhorizontal(b,a.tweak(bright=bright,cont=cont))
subtitle("Cont="+string(cont))
subtitle(y=20," Bright="+string(bright))

function findtweak(clip a, clip b, int parm){
  current_frame=0
  ma=a.averageluma
  mb=b.averageluma
  x1=trueavg(a,"<=",ma)
  x2=trueavg(a,">",ma)
  y1=trueavg(b,"<=",mb)
  y2=trueavg(b,">",mb)
  #in the test clip, looking for true avg of 31.5 (16-47), but half the pixels are 0 (or 1)
  #upper half is avg=63.5 (48-79)
  #tweaked values gives 62,127
  k=(y2-y1)/(x2-x1)
  b=(y1-16)-(x1-16)*k
  parm==0?k:b
}

function trueavg(clip a, string op, float luma){
#Return average of all pixels op luma *while ignoring others in the calculation*
#Do this by finding avg(x op luma ? x:1)-avg(x op luma ? x:0) etc.
  current_frame=0
  m0=mt_lut(a,expr="x "+string(luma)+" "+op+" x 0 ?").averageluma
  m1=mt_lut(a,expr="x "+string(luma)+" "+op+" x 1 ?").averageluma
  m0/(m1-m0)
}
jmac698 is offline   Reply With Quote