Welcome to Doom9's Forum, THE in-place to be for everyone interested in DVD conversion.

Before you start posting please read the forum rules. By posting to this forum you agree to abide by the rules.

 

Go Back   Doom9's Forum > Capturing and Editing Video > Avisynth Usage
Register FAQ Calendar Today's Posts Search

Reply
 
Thread Tools Search this Thread Display Modes
Old 6th January 2019, 15:28   #1  |  Link
StainlessS
HeartlessS Usurer
 
StainlessS's Avatar
 
Join Date: Dec 2009
Location: Over the rainbow
Posts: 10,980
GamMatch v0.05 - RGB Color Matcher - 06 Jan 2019

GamMatch v0.05, bout time it had its own thread.
v2.58 + v2.60/+ x86 and x64 dll's.

RGB24, RGB32 only. Requires VS 2008 Runtimes.

Code:
GamMatch(), [Gamma Matcher] A daft Idea, based upon an extraordinary Idea by VideoFred (the gent from Gent). (Daft idea and code by StainlessS).

    The Idea behind the daft idea(GamMac):-  http://forum.doom9.org/showthread.php?p=1774281#post1774281
    Original Idea:-                          http://forum.doom9.org/showthread.php?t=173683

RGB Only.

    Useful to correct bad color clip where there is a better color source of perhaps lower rez available.
    Additional tweaking via RedMul, GrnMul and BluMul multipliers.
      GamMatch, Allows to correct a clip that has had Levels + Gamma badly applied, to one of more channels, by using an in
    sync version clip with good un-messed with color, but perhaps of inferior resolution.
    GamMatch is unlikely to be of any use where eg Tweak or some other non RGB levels type mod has been applied.

    What it does(roughly):-
         Alter bad source color clip channel minimums, maximums and averages to match dc detect (good color) clip.

GamMatch(Clip c,clip dc,
      \ Float "RedMul"=1.0,Float "GrnMul"=1.0, Float "BluMul"=1.0,
      \ Float "Th"= 0.00,Float "loTh"=Th,Float "hiTh"=Th,
      \ int "RngLim"=11,Float "GamMax"=10.0,
      \ int "x"=20,int "y"=20,int "w"=-20,int "h"=-20,
      \ Bool "Show"=True,int "Verbosity"=2,Bool "Coords"=false
      \ )

    c, no default.              Bad Color source clip.

    dc, no default.             Detection clip (good color clip). Must be same ColorSpace and FrameCount as source clip, no other similarities enforced.

    RedMul, default 1.0         Red channel multiplier adjustment.   [0.1 <= RedMul <= 10.0]
    GrnMul, default 1.0         Green channel multiplier adjustment. [0.1 <= GrnMul <= 10.0]
    BluMul, default 1.0         Blue channel multiplier adjustment.  [0.1 <= BluMul <= 10.0]

    Th, Default 0.00           Sets Default for loTh and hiTh. Suggest Default, 0.00(percent).  [-1.0(OFF) , or 0.0 -> 1.0]
								   v0.05, NOTE, Default changed from 0.04 to 0.00.
    loTh, Default Th           As for Ignore_low in AutoLevels, or Threshold in YPlaneMin.  [-1.0, or 0.0 -> 1.0]
                               Percent, amount of extreme pixels (eg noise) to ignore when finding minimum R, G or B channel values.
                               -1.0 is OFF, input channel minimum is set to 0 as for levels(0,gamma,input_max, ... ).
                               If loTh >=0.0, then will scan frame looking for lowest pixel value whose cumulative sum
                               [including all pixels counts of lower value pixels] is greater than loTh%.
                               loTh, only shown in metrics if greater or equal to 0.0 ie switched ON (Always shown when Verbosity=3=FULL).
    hiTh, Default Th           As for Ignore_high in AutoLevels, or Threshold in YPlaneMax. [-1.0, or 0.0 -> 1.0]
                               Percent, amount of extreme pixels (eg noise) to ignore when finding maximum R, G or B channel values.
                               -1.0 is OFF, input channel maximum set to 255, as in levels(input_min,gamma,255, ... ).
                               If hiTh >=0.0, then will scan frame looking for highest pixel value whose cumulative sum
                               [including all pixels counts of higher value pixels] is greater than hiTh%.
                               hiTh, only shown in metrics if greater or equal to 0.0 ie switched ON (Always shown when Verbosity=3=FULL).

    RngLim, default 11         [1 <= RngLim <= 32]
                               If ALL THREE RAW input channel ranges ie (ChannelMax(max(hiTh,0.0))-ChannelMin(max(loTh,0.0))) are less than RngLim then
                               all scaling is disabled, and remapping is linear without gamma estimation, to range omin -> omax,
                               ie avoid remapping of Black, White frames, or single color frames.

    GamMax, default 10.0       Upper value for guess gamma [2.0 <= GamMax <= 10.0]
                                   Starting guess upper range and limit for gamma estimator (probably best left alone).
                                   The lower guess range and limit will be set to 1.0 / GamMax, by default 0.1.


    x,y, Both default 20.      Area of dc Detect clip frame to sample when getting averages and estimating Gamma function, allows to ignore rubbish at frame edges.
    w,h, Both default -20.     Specified as for crop eg x=10,y=20,w=-30,h=-40, as in crop(10,20,-30,-40).

    Show, default true        True, show metrics info on frame.
    Verbosity, default 2       0 = Only upper frame metrics Flags line only
                               1 = Upper frame metrics
                               2 = Upper + important ones. (default)
                               3 = Nearly Full metrics.
                               4 = Full Metrics except version info
                               5 = Full Metrics including version info
                               Upper frame metrics shown as eg:- (when Verbosity=5=FULL)

                                  nnnnn] Flags:- SR
                                             R         G         B
                                  DC:       3,251     7,247     5,251
                                  DC_AVE:  73.875    62.033    70.528       >>>>>>>>
                                  RAW:      8,247    12,223    32,239
                                  IN:       8,247    12,223    32,239
                                  IN_AVE:  62.515    88.859   133.384
                                  SCALED:  58.165    92.887   124.893
                                  GAMMA:    1.240     0.640     0.486
                                  OUTAVE:  73.875    62.032    70.529       <<<<<<<< This matching DC_Ave

                               where,
                                  nnnnn, is the frame number.
                                  Flags:- (Specific to current frame, can change frame to frame)
                                      'S' = Scale, mode signified by color.
                                            Greyed out. Scale = 0(No Effect). May be Greyed out if all channels Min/Max are 0,255.
                                            White.  Scale = 1[Scales input channel average maximum dynamic range of R,G,B]
                                            Orange. Scale = 2[Scales input channel average dynamic range of R and G and B, Individually]
                                      'R' = Limited by RngLim, mode signfied by color.
                                            Greyed out. Not Range Limited.
                                            Red, at least 1 channel has remapping disabled.
                                  DC:     Shows comma separated channel minimum and maximum of DC clip(dependent upon loTh, hiTh).
                                  DC_AVE: DC Clip channel averages.
                                  RAW:    Shows RAW comma separated channel minimum and maximum, eg ChannelMin(max(loTh,0.0)) and (ChannelMax(max(hiTh,0.0)),
                                          only shown if Verbosity>=3 or, if any RAW input range is less than RngLim AND any of the RAW inputs are different
                                          to the equivalent standard input.
                                  IN:     Shows comma separated channel minimum and maximum (dependent upon Scale, loTh, hiTh).
                                  IN_AVE: Input channel averages.
                                  SCALED: Scaled input averages, (dependent upon Scale, loTh, hiTh, channel minimums and maximums).
                                  GAMMA:  Estimated gamma to achieve OutAve for channel. (dependent upon pretty much everything).
                                  OUTAVE: Output channel average ie rendered result.

                               DC and DC_AVE derived from DC (Good color clip), all others from clip c (BAD color clip, Incl OUTAVE).

    Coords, default False.  If True, then shows DC clip with dotted lines showing the x,y,w,h coords plotted on frame.
	                        (All other GamMatch functionality disabled).
Test script
Code:
# Some good-ish color clip
#Avisource("F:\v\StarWars.AVI")
Avisource("D:\1941 Flint Michigan Parade [Low, 360p].mp4.AVI")
ConvertToRGB24.KillAudio

DC=Last

# Simulate bad Input clip
RED=ShowRed(Pixel_Type="Y8").Levels(  0,0.8,255, 8,247,coring=false)
GRN=ShowGreen(Pixel_Type="Y8").Levels(0,1.5,255,12,223,coring=false)
BLU=ShowBlue(Pixel_Type="Y8").Levels( 0,2.0,255,32,239,coring=false)
MergeRGB(RED,GRN,BLU,pixel_Type="RGB24")

# Simulate DC Low res clip with good color
DC=DC.Blur(1.58).Blur(1.58).Spline36Resize(320,240)   # Fake change of size for detect

O=Last

RedMul    = 1.0          # Required Ave multiplier for Red Channel, applied when requesting GuessGamma(reqAve*RedMul).
GrnMul    = 1.0          #    Same for Green channel.
BluMul    = 1.0          #    Same for Blue channel.  Allows tinkering/fine tuning.
Th   	  = 0.0          # Usually 0.0
RngLim    = 11           # Leave alone
GamMax    = 10.0         # Leave alone
Verb      = 5            # Verbocity FULL
SHOWCOORDS= False

x =5                     # Coords (for dc Detection Clip, crop off crud)
y =5
w=-5
h=-5

# Return O.GamMatch(DC,x=x,y=y,w=w,h=h,Coords=true)     # Show DC Coords Only

A_TEXT = RT_String("OUT: rMul=%.2f gMul=%.2f bMul=%.2f",RedMul,GrnMul,BluMul)
A=O.GamMatch(DC,RedMul=RedMul,GrnMul=GrnMul,BluMul=BluMul,
    \ Th=Th,RngLim=RngLim,GamMax=GamMax,
    \ x=x,y=y,w=w,h=h,
    \ Show=True,Verbosity=Verb)

B_TEXT = RT_String("OUT No Metrics: rMul=%.2f gMul=%.2f bMul=%.2f",RedMul,GrnMul,BluMul)
B=O.GamMatch(DC,RedMul=RedMul,GrnMul=GrnMul,BluMul=BluMul,
    \ Th=Th,RngLim=RngLim,GamMax=GamMax,
    \ x=x,y=y,w=w,h=h,
    \ Show=False)


COORDS=O.GamMatch(DC,x=x,y=y,w=w,h=h,Coords=True).Spline36Resize(width,Height)  
ODC=((SHOWCOORDS)?COORDS:DC).Spline36Resize(width,Height)                               source clip)
ODC_TEXT=(SHOWCOORDS)?"Detect Clip with Coords":"DC Detect (Match Original to this)"

TOP=StackHorizontal(Sub(O,"Original (Simulated bad clip)"),Sub(A,A_TEXT))
BOT=StackHorizontal(Sub(ODC,ODC_TEXT),Sub(B,B_TEXT))
StackVertical(TOP,BOT)
return Last

Function Sub(clip c,string tit) {StackVertical(c.BlankClip(height=20).
    \ ScriptClip("""Subtitle(RT_String("%d] %s",current_frame,tit))""",Args="tit",Local=true),c)}
Zip (~131KB) incl v2.58 + v2.60/+ x86 and x64 dll's, + source + full VS2008 project files.
See Mediafire in sig below this post.



Matching red Lenna to magazine published version of photo
(both have had different tweaks applied, mag is darker browner,
red is presumably deliberately screwed up as test image)
Code:
ImageSource(".\LennaRed.png",end=0).ConvertToRGB24
O=Last
DC=ImageSource(".\Lenna_Org.png",end=0).ConvertToRGB24 # magazine pubb'ed version)
DC=DC.Blur(0.2)

Th=0.0

A=O.GamMatch(DC,Th=Th,Show=True,Verbosity=1)
B=O.GamMatch(DC,Th=Th,Show=False)

TOP=StackHorizontal(Sub(O,"Lenna Bad RED clip)"),Sub(DC.Spline36Resize(width,Height),"DC Detect(Lenna_Magazine match to this)"))
BOT=StackHorizontal(Sub(A.Spline36Resize(width,Height) , "Metrics" ),Sub(B,"OUT No Metrics"))
Return StackVertical(TOP,BOT)

Function Sub(clip c,string tit) {StackVertical(c.BlankClip(height=20).ScriptClip("""Subtitle(RT_String("%d] %s",current_frame,tit))""",Args="tit",Local=true),c)}
Result is not brilliant matching, but then significant amount of color missing in red version[Red min = 54, Blue min =18], and other color missing in mag version.
Below is probably about the worst match that you are ever gonna get(till you show even worse one).
__________________
I sometimes post sober.
StainlessS@MediaFire ::: AND/OR ::: StainlessS@SendSpace

"Some infinities are bigger than other infinities", but how many of them are infinitely bigger ???

Last edited by StainlessS; 7th January 2019 at 14:07.
StainlessS is offline   Reply With Quote
Reply

Tags
color matching, colorlike


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 22:58.


Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.