SRL Archive - FAQ

Tolerance Algorithms?


any_one

2010-12-11 10:13:32

Is there any info on the actual code (or a conceptual description of what) simba uses to compare colours at different tolerance speeds? I tried a quick search for "tolerance" but only found a discussion on reverse-engineering CTS 3 (which appears to have been done?)
Sorry if I missed something obvious. Thanks :)

Gu3st...

2010-12-11 10:15:55

The max tolerance of CTS1 is 255
The other color tolerance speeds is a bit more complicated, and there is no real 'tolerance' afaik

Cazax

2010-12-11 11:44:01

There are four color-spaces, CTS 0(Color32), 1(RGB), 2(HSL) and 3(XYZ). The max tolerance for cts 0 is 255, 1 is 442, 2 is 100 and 3 is 227. IIRC, XYZ hasn't been implemented in Simba yet.
As default, the cts is 1(RGB). More info : http://villavu.com/forum/showthread.php?t=40686

any_one

2010-12-11 18:29:29

Yeah I have read that, thanks. What I was more looking for is how it uses the tolerance to determine if colors are similar. Someone said that for CTS1 it uses distance in the RGB cube, which makes sense and for CTS 0 it just does if (r - r1) < tol and (g - g1) < tol etc.
How does CTS 2 work though? As far as I can tell it does something like if (l - l1) < tol and (h - h1) < tol * hmod and (s - s1) < tol * smod, is this correct?

mastaraymond

2010-12-12 05:56:26

http://git.villavu.com/?p=simba.git;a=blob;f=Units/MMLCore/finder.pas;h=7de3bad2effa640bf9f2980a45750010b5c31ba8;hb=HEAD

Search for "SimilarColors".

CTS 3, no one has a clue how the algorithm really works. Freddy's secret stuff, ya know.

any_one

2010-12-12 06:14:08

Ah ok thank you raymond! They worked as I thought. For XYZ he could just be using the distance between xyz coordinates like simba does for CTS 1 - the XYZ colour space is apparently more "perceptual" so 3D distance between colours corresponds better to perceptual difference between them.

Apparently LAB is better for this though (it's somewhat based on XYZ but the parameters better simulate the response of human vision - thus it should be better than XYZ in using 3D distance between colours.) This is according to wikipedia by the way :p I wrote copied from some website some functions to convert XYZ to LAB and back:


procedure XYZToLAB(X,Y,Z : Extended; var L, A, B : Extended);
var
var_x, var_y, var_z : Extended;
begin
var_X := X / 95.047; //ref_X = 95.047 Observer= 2°, Illuminant= D65
var_Y := Y / 100; //ref_Y = 100.000
var_Z := Z / 108.883; //ref_Z = 108.883

if ( var_X > 0.00885645 ) then var_X := pow(var_X, 0.333333)
else var_X := ( 7.787037 * var_X ) + ( 4 / 29 );

if ( var_Y > 0.00885645 ) then var_Y := pow(var_Y, 0.333333 )
else var_Y := ( 7.787037 * var_Y ) + ( 4 / 29 );

if ( var_Z > 0.00885645 ) then var_Z := pow(var_Z, 0.333333 )
else var_Z := ( 7.787037 * var_Z ) + ( 4 / 29 );

L := ( 116 * var_Y ) - 16;
A := 500 * ( var_X - var_Y );
B := 200 * ( var_Y - var_Z );
end;

procedure LABToXYZ(L, A, B : Extended; var X, Y, Z : Extended);
var
var_y, var_x, var_z : Extended;
begin
var_Y := ( L + 16 ) / 116;
var_X := A / 500 + var_Y;
var_Z := var_Y - B / 200;

if ( pow(var_Y, 3) > 0.00885645 ) then var_Y := pow(var_y, 3)
else var_Y := ( var_Y - 4 / 29 ) / 7.787037;

if ( pow(var_X, 3) > 0.00885645 ) then var_X := pow(var_X, 3)
else var_X := ( var_X - 4 / 29 ) / 7.787037;

if ( pow(var_Z, 3) > 0.00885645 ) then var_Z := pow(var_Z, 3)
else var_Z := ( var_Z - 4 / 29 ) / 7.787037;

X := 95.047 * var_X; //ref_X = 95.047 Observer= 2°, Illuminant= D65
Y := 100 * var_Y; //ref_Y = 100.000
Z := 108.883 * var_Z; //ref_Z = 108.883
end;

Tested and they are the inverse of each other ;)

mastaraymond

2010-12-12 06:22:27

EasyRGB calculations :p.

Might be possible that CTS 3 actually uses LAB.. Would be cool if you could check if it's true :-).

i luffs yeww

2010-12-12 06:24:18

Test accuracy. Trees and grass. Draynor or something. Test CTS(0) vs xyz/lab, if you can.

any_one

2010-12-12 06:31:14

Yep, it's from easyrgb :) I did make an LABSimilarColors, haven't tested much yet cause I got preoccupied doing something else :p From the few I did do it worked pretty well (didn't require much tolerance) but I didn't compare it to HSL or RGB.

For getting coloured areas HSL is probably more flexible because of the hmod and smod, I was thinking of making a LABSimilarColors that used ellipsoids instead of a straight distance comparison (sphere) but it's a lot harder than just using HSL.

Edit: And yes I will test, give me some time :p

Markus

2010-12-12 06:37:15

We tried to reverse CTS3 and we got close, but there still was some difference between them.
From my local archive:

Two colors: (X1, Y1, Z1) (X2, Y2, Z2)
NX1 = (X1 + Y1 + Z1) / X1 * (CTS3mod * 125)
NY1 = (X1 + Y1 + Z1) / Y1 * (CTS3mod * 125)
NX2 = (X2 + Y2 + Z2) / X2 * (CTS3mod * 125)
NY2 = (X2 + Y2 + Z2) / Y2 * (CTS3mod * 125)
Value = (NX1 - NX2)^2 + (NY1 - NY2)^2
Result = Value^2 <= Tol^2

any_one

2010-12-12 11:24:50

OK, I did a few comparisons in a foresty area around draynor and here are some conclusions:

cts (0) : tolerance has a large effect, often finds ground as well as trees if you pick a wrong colour.

cts(1) : not too bad, tolerance has a fairly strong effect relative to others.

cts(2) : tolerance doesn't have as much effect (at least without changing the modifiers), only very rarely finds ground if you pick a tree colour, probably can be improved to almost never if you set hmod and smod.

xyz using markus' function : I couldn't get it to find ground when picking a tree colour. Tolerance seems to have very little effect. Not as many points found as the other methods usually but is very reliable.

LAB using my function (below) : Finds a slightly different set of colours than the other functions. Colours it finds tend to look more visually similar than the other methods (eg. on trees, it might find the light side and not the back/top which is darker, where the others (especially hsl and xyz) have points in the darker area also). In terms of finding ground, it is between cts(1) and cts(2) in reliability with equal tolerance. However, tolerance has a strong effect on how many points it finds so it requires a lower tolerance than other methods to get the same number of points. Seems to work best with LRatio between 2 and 5.

Images to come.


// LRatio is a ratio of how much L (lightness) can change compared to a and b -
// allowing a greater range of luminance compared to chroma is similar to the
// hmod and smod of HSL and seems to work better for colour finding
Function LABSimilarColours(l,a,b,l1,a1,b1 : Extended; tol, lratio : Integer ) : Boolean;
begin
Result:=false;
if sqrt((pow(l-l1, 2) / lratio) + pow(a-a1, 2) + pow(b-b1, 2)) < (tol / (0.7 * ln(lratio) + 2))
// reason for divisor on tol is to try and get the number of points
// down to be similar to other methods at the same tolerance
then Result:=true;
end;

Shuttleu

2010-12-12 11:33:33

From: any_one
OK, I did a few comparisons in a foresty area around draynor and here are some conclusions:

cts (0) : tolerance has a large effect, often finds ground as well as trees if you pick a wrong colour.

cts(1) : not too bad, tolerance has a fairly strong effect relative to others.

cts(2) : tolerance doesn't have as much effect (at least without changing the modifiers), only very rarely finds ground if you pick a tree colour, probably can be improved to almost never if you set hmod and smod.

xyz using markus' function : I couldn't get it to find ground when picking a tree colour. Tolerance seems to have very little effect. Not as many points found as the other methods usually but is very reliable.

LAB using my function (below) : Finds a slightly different set of colours than the other functions. Colours it finds tend to look more visually similar than the other methods (eg. on trees, it might find the light side and not the back/top which is darker, where the others (especially hsl and xyz) have points in the darker area also). In terms of finding ground, it is between cts(1) and cts(2) in reliability with equal tolerance. However, tolerance has a strong effect on how many points it finds so it requires a lower tolerance than other methods to get the same number of points. Seems to work best with LRatio between 2 and 5.

Images to come.


// LRatio is a ratio of how much L (lightness) can change compared to a and b -
// allowing a greater range of luminance compared to chroma is similar to the
// hmod and smod of HSL and seems to work better for colour finding
Function LABSimilarColours(l,a,b,l1,a1,b1 : Extended; tol, lratio : Integer ) : Boolean;
begin
Result:=false;
if sqrt((pow(l-l1, 2) / lratio) + pow(a-a1, 2) + pow(b-b1, 2)) < (tol / (0.7 * ln(lratio) + 2))
// reason for divisor on tol is to try and get the number of points
// down to be similar to other methods at the same tolerance
then Result:=true;
end;


hmmm… nice test, please do add pics

~shut

Gu3st...

2010-12-12 11:37:16

Pics would be great! :)
How long time does each test take?

any_one

2010-12-12 11:48:39

Each time takes ~1min of processing (full main screen), around 5-10 seconds is cts 0,1,2 and the rest xyz and lab, presumably because those are custom SimilarColors functions and so are not as fast as the others (plus a custom conversion to LAB, obviously). I'll add pictures tomorrow, I've already stayed up too late as it is :p Have a look at the simba file, you should be able to just pick a client and run it (tol=tolerance, have a look for other stuff like hmod/lratio/xyzmod etc), will save bitmaps to C:\ at the moment.

Wizzup?

2010-12-12 11:51:49

If you are looking to make a new, good CTS mode, don't try to copy SCAR's secret CTS 3. Instead, create your own. It'll probably be better and we don't really have the compatibility burden.