YOUR ACCOUNT

Login or Register to post new topics or replies
George Larsen (Flynn)
George Larson (Flynn)

Posts: 2
Filters: 3
Hi! smile:) These are a couple of color averaging scrips I wrote, and that I'd like to share.

Controls:
M: Color map
D: Int slider



Method One:

Recursive averaging

D dictates how many times the image gets slit into quarters. So, for instance: 1 means it will just sample the center of the image and return that.

2 means it will split the image into quarters and return the average of the centers of those quarters.

3 means it will split those quarters into MORE quarters, and measure all of the centers of those.

This goes on until the max value of ten, where, at which point, you are sampling 3.5 hundred thousand points

All those samples are added up and then averaged. Usually I keep it on D = 5. The code is:

Code
local cr, cg, cb, ca;
local beenDone;
local dd;
function prepare()
   beenDone = -1;
   dd = get_intslider_input(D);
end;

function get_sample(x, y)
   local cols = {};
   
   return doAvg(dd);
end;


function doAvg(detail)
   if (beenDone < 0) then
   local r,g,b,a,c = getAvg(0, detail, 0,1,0,1);
   cr, cg, cb, ca = r/c, g/c, b/c, a/c;
   beenDone = 1;
   end
   return cr, cg, cb, ca;
end;

function getAvg(level, mlevel, minx, maxx, miny, maxy)

      local midx = midp(minx,maxx);
      local midy = midp(miny,maxy);
      
      local c1 = 0;
      local r1, g1, b1, a1 = get_sample_map(midx,midy,M);
      local r, g, b, a, c = 0, 0, 0, 0, 0;

      r = r + r1; g = g + g1; b = b + b1; a = a + a1; c = c + 1;

      if (level ~= mlevel) then
         r1, g1, b1, a1, c1 = getAvg(level+1, mlevel, minx, midx, miny, midy);
         r = r + r1; g = g + g1; b = b + b1; a = a + a1; c = c + c1;
         r1, g1, b1, a1, c1 = getAvg(level+1, mlevel, midx, maxx, miny, midy);
         r = r + r1; g = g + g1; b = b + b1; a = a + a1; c = c + c1;
         r1, g1, b1, a1, c1 = getAvg(level+1, mlevel, minx, midx, midy, maxy);
         r = r + r1; g = g + g1; b = b + b1; a = a + a1; c = c + c1;
         r1, g1, b1, a1, c1 = getAvg(level+1, mlevel, midx, maxx, midy, maxy);
         r = r + r1; g = g + g1; b = b + b1; a = a + a1; c = c + c1;
      end;
      return r, g, b, a, c;
end;


function midp(minn,maxn)
   return ((maxn-minn)/2)+minn;
end;

function merget(a,b)
   for k,v in pairs(b) do a[k] = v end
end;



Method Two:

This method is simple, faster, but far less reliable for results. It samples D number of random points on the image, and averages those. I recommend keeping d at around 1000 for best results.

The code is:
Code

local pts = {};
local beenDone;
function prepare()
   beenDone = -1;
   dd = get_intslider_input(D);
   for i = 0, dd*2, 2 do
         pts[i] = math.random();
         pts[i+1] = math.random();
   end;
end;

function get_sample(x, y)
   return doAvg(dd);
end;

function doAvg(detail)
   if (beenDone < 0) then
      local r, g, b, a = 0, 0, 0, 1;
         for i = 0, dd * 2, 2 do
            local r1, b1, g1, a1 = get_sample_map(pts[i], pts[i+1], M);
            r, g, b, a = r + r1, g + g1, b + b1, a + a1;
         end;
      beenDone = 1;
      cr, cg, cb, ca = r/dd, g/dd, b/dd, a/dd;
   end;
   return cr, cg, cb, ca;
end;



Either of these methods are only run ONCE, so it is very scalable, and shouldn't lag at all for decent detail/accuracy.

Personally, I recommend method 2.
Simplicity is beauty
  Details E-Mail
George Larsen (Flynn)
George Larson (Flynn)

Posts: 2
Filters: 3
The lines:
Code
local dd;
local cr, cg, cb, ca;

got cut off at the top of the second code block during copy and paste, sorry.


Also, in second method, the line:
Code
local r1, b1, g1, a1 = get_sample_map(pts[i], pts[i+1], M);

should be:
Code
local r1, g1, b1, a1 = get_sample_map(pts[i], pts[i+1], M);


(Got R, and B confused)
Simplicity is beauty
  Details E-Mail
Skybase
2D/3D Generalist

Posts: 4025
Filters: 76
Thanks so much!! I'll be playing with these. smile;) Always nice to see code. I basically want to learn lua though have no place to start. I have some experience with Python though.
  Details E-Mail
Ali Baba
Posts: 1
Filters: 3
Thanks, thanks, thanks! Very useful script. I use it in now. By the way, the first filter works better and give flat response. Second filter dose not give flat response and on picture I sometimes have dark or light square.
Also, both filters have small difference between Average Color filter in FF.
You can see it on bright pictures.

Thank you ones again. Very good filters.
  Details E-Mail

Join Our Community!

Filter Forge has a thriving, vibrant, knowledgeable user community. Feel free to join us and have fun!

33,711 Registered Users
+18 new in 30 days!

153,531 Posts
+36 new in 30 days!

15,347 Topics
+72 new in year!

Create an Account

Online Users Last minute:

16 unregistered users.