Log in

View Full Version : I made a perl script that makes an RGBAdjust cmd for white balancing


HighInBC
18th June 2005, 22:40
This is a very simple perl subroutine for white balancing clips in avisynth(it produces an RGBAdjust command), it should be very easy to understand:


sub wb
{
use Win32::Clipboard;
$CLIP = Win32::Clipboard();
my $r = shift;
my $g = shift;
my $b = shift;
my $modify = shift || 1;
my $largest = $r;
($largest = $g) if ($g > $largest);
($largest = $b) if ($b > $largest);
$largest = ($largest * $modify);
$r = $largest/$r;
$g = $largest/$g;
$b = $largest/$b;
my $avs_line = "RGBAdjust($r,$g,$b,1)";
$CLIP ->Set($avs_line);
return $avs_line;
}

to use this, you must have ActiveState Perl installed. you use this by taking the RGB values of a pixel from the clip that should be white:

wb(R, G, B);
or
wb(R, G, B, L);

L is for adjusting brightness of final image. below 1 lowers brightness above 1 increases, 1 has no effect.

The program will put an RGBAdjust command into the windows clipboard for you to paste into your script.

Example:

wb(224, 205, 184);
gives:
RGBAdjust(1,1.09268292682927,1.21739130434783,1)

and

wb(224, 205, 184, 0.95)
gives:
RGBAdjust(0.95,1.0380487804878,1.15652173913043,1)

Without using the L value to adjust brightness, you will find oversaturation common, I find a value from .8-1 works well.

I have included before and after images, in this case I gave the sub the color fo a pixel on the table and it gave me:
RGBAdjust(1,1.17058823529412,2.09473684210526,1)

Before:
http://adserton.crackerjack.net/~rambler/before.jpg
After:
http://adserton.crackerjack.net/~rambler/after.jpg

If you are using a version of perl other than ActiveState, the Win32::Clipboard module may not be available, to make the script work on your perl interpreter simply remove the following lines:

use Win32::Clipboard;
$CLIP = Win32::Clipboard();
$CLIP ->Set($avs_line);


The sub's return value will be the avisynth code.

HighInBC
19th June 2005, 00:33
Oh, for those not familiar at all with perl, once you install it, just make a file called 'whitebalance.pl', or any name, but with a .pl extension.

paste the subroutine in the file and at the top put the wb(R,G,B[,L]); command.

run the script and the answer will be in the clipboard.

scharfis_brain
19th June 2005, 16:16
hmmm. this should also be achievable using some frameevaluating within AVS.

can you describe the maths behind the perl-script, please?

HighInBC
19th June 2005, 16:28
sure, you take the largest value of R,G or B then multiply it by the brightness modifier, and call it 'X'.

You then divide each channel by X and then place those results into the RGBAdjust command.

RGBAdjust then multiplies each pixel with those numbers.

if the RGB values you gave it were of a pixel that should have been white, the RGBAdjust values will make it white and adjust everything else accordingly.

I just posted a pure avisynth version of the routine.

Thanks for looking into this.