Page 1 of 2

Fuzz culculator

Posted: 2013-08-15T04:38:22-07:00
by VanGog
Do you know about some site/page where I could find algorithm for calculation of fuzz of color? Are there some basic rules, which I have to fallow when I want to create algorithm for fuzz calculation? I am working on graphical program, it is not based on imagemagick, however some basic knowledge I need.

First ideas which came to me, about problem with fuzz calculation is, that I realized that it is something completely different to calculate colors on real world painting than we do in IT world. For example yellow: is defined as rgb(255,255,0) but in real world if you take 1 part red and 1 part green and mix it... So it is like you would take 1 part red, 1/2 yellow and just a little bit of blue. But this surely will not result in yellow color (but some kind of green)! Because yellow is one of 5 basic colors in real world (Y,R,Blu,W,Black).

I thought I could calculated fuzz from the numbers of color ratios, so if I have green 100% (255), I don't have to change fuzz number. But if I have red 50% (127) and blue 50% (127) so I should discrease the fuzz numbers /2. But then I realized, that it will be yet more complicated because 1) 127 127 0 definitely isn't near to green or red, because it's violet. Actually what is it fuzz 100%? Is it the value, when yellow is still yellow, or color near to yellow, or it can be black too? As I see photoshop. there fuzz 25% (50 points in PS) means that the color is still similar to the original color, it only little changes tones or darkness/lightness.
E.g. if I select skin color dark pink - light brown and make fuzz cca 75% (132 points in PS) so I selected all the pink, light pink, light brown, orange... Brown is not pink, but here on the image it looks similar because this all are colors of skin. 180 points / 200 it selected much of light areas, but those in shadow are not selected, so skin in shadow is still not selected because that is darker brown-green or midtone. And 200/200 is still similar. So this looks like the fuzz calculates not only with color but aso with amount of light.

Re: Fuzz culculator

Posted: 2013-08-15T07:11:53-07:00
by snibgo
ImageMagick is open source, so you can see how it works.

I suggest you read up on colorspaces. In IM, fuzz is a distance within a colorspace. A colorspace typically has 3 dimensions: RGB, CMY or Lab. One has 4 dimensions: CMYK.

If you paint, you probably work with more colours. If you are writing your own code, you can devise your own colorspace that corresponds to the paints you use, and has the same number of dimensions.

Re: Fuzz culculator

Posted: 2013-08-15T07:40:57-07:00
by GreenKoopa
I second the suggestion to read up on colorspaces. This is a huge topic we couldn't do justice to here. There are no simple answers, so be prepared to commit some time and learn a lot.
VanGog wrote:I realized that it is something completely different to calculate colors on real world painting than we do in IT world.
I think you are referring to additive verses subtractive color. Screens are additive, like RGB. Painting is subtractive, like CMY. By real world you could also look into how the eye perceives color (Lab and Luv), and in the deeper physics reality it's all just photons of different frequencies.

Re: Fuzz culculator

Posted: 2013-08-15T09:18:52-07:00
by VanGog
Does Image Magick have some concrete function for fuzz calculation? Or Class? Can you refer me to some file, line, etc in your code (and link where to download version to check)?

Re: Fuzz culculator

Posted: 2013-08-15T09:25:10-07:00
by GreenKoopa
IM does have -fuzz, but it is only one method of calculating such things and may not be what you want. It will be affected by your -colorspace.

Source code is available:
http://www.imagemagick.org/script/install-source.php

Re: Fuzz culculator

Posted: 2013-08-15T13:35:15-07:00
by VanGog
Thanks for link. I would need some reference to a certain file or line in source to see how the fuzz works. Because I am not c/c++ developer, but using different language, but I want to check the algorithm. There is many files so I do not know where to look for. I will use RGB name space.

Re: Fuzz culculator

Posted: 2013-08-15T13:49:23-07:00
by fmw42
It is likely the rmse difference

sqrt((reddiff^2+greendiff^2+bluediff^2)/3)



see color.c IsMagickColorSimilar

line 1770

This implements the equivalent of...
fuzz < sqrt( color_distance^2 * u.a*v.a + alpha_distance^2 )

line 1776

For example for an RGB
color_distance^2 = ( (u.r-v.r)^2 + (u.g-v.g)^2 + (u.b-v.b)^2 ) / 3


so fuzz=color_distance if only RGB and no alpha

Re: Fuzz culculator

Posted: 2013-08-15T14:30:41-07:00
by VanGog
fmw42 wrote: see color.c IsMagickColorSimilar

line 1770

This implements the equivalent of...
fuzz < sqrt( color_distance^2 * u.a*v.a + alpha_distance^2 )

line 1776

For example for an RGB
color_distance^2 = ( (u.r-v.r)^2 + (u.g-v.g)^2 + (u.b-v.b)^2 ) / 3


so fuzz=color_distance if only RGB and no alpha
Wow this is exactly the first file I checked by a luck. But I did not understand it because I overlooked there is commented help above. I am reading about C language on Wiki, is it C language?

Re: Fuzz culculator

Posted: 2013-08-15T14:42:43-07:00
by snibgo
VanGog wrote:... so skin in shadow is still not selected because that is darker brown-green or midtone. And 200/200 is still similar. So this looks like the fuzz calculates not only with color but aso with amount of light.
Yes. Remember that fuzz uses all the channels. It measures distance in 3-dimensional space.

I don't use fuzz much because the result in binary: it's a match or it isn't. I usually select more explicitly, and I want to know how far each pixel is from my reference. For a skin, you might find the distance in just the hue channel.

Re: Fuzz culculator

Posted: 2013-08-15T15:43:21-07:00
by VanGog
I read about different namespaces and some basics about chromacity
http://en.wikipedia.org/wiki/Chromaticity
Physiology of color perception
http://en.wikipedia.org/wiki/Color_vision
CIE 1931 color space
http://en.wikipedia.org/wiki/CIE_1931_color_space
CIELUV / CIE 1976
http://en.wikipedia.org/wiki/CIELUV

What I don't understand is the difference between "cube root" transformation - CIE 1976 (L*, a*, b*) and "square root" transformation - (Hunter 1948 L, a, b ). However which transformation do you use?

I have yet found two references so will read and maybe will be more wise tomorrow:
http://www.cg.tuwien.ac.at/research/the ... ode79.html
http://en.wikipedia.org/wiki/Root-mean-square_deviation

Re: Fuzz culculator

Posted: 2013-08-15T22:20:42-07:00
by snibgo
Yes, ImageMagick is written in C.

I believe IM colorspaces "Lab and "CIELab" are the common version, CIELAB, CIE 1976 (L*, a*, b*). See http://en.wikipedia.org/wiki/CIELAB . However, values in IM Q16 are integers from 0 to Quantum (which is 65535 for Q16), and the zero or neutral values for a and b channels are at 50%.

Re: Fuzz culculator

Posted: 2013-08-15T23:25:45-07:00
by VanGog
I am reading about lab colorspace or CIELab

Re: Fuzz culculator

Posted: 2013-08-16T06:34:09-07:00
by VanGog
Also, you use lab space to find fuzz, but do you also use different colorspace like RGB to define fuzz? I believe it must be less performance then to calculate fuzz in RGB (if possible) because I found lab is more precise than RGB. Also I would need to convert from RGB to lab which can slower the calculation process.

For my program I am going to work with simple RGB output displayed on screen --> meaning that I am going to capture RGB colors from simple graphics e.g. from website or map like this http://www.mapy.cz/... The idea is to capture some color with a fuzz e.g 20% but I think it is not necessary to work with lab (maybe I am wrong). Maybe I could find some other formula for RGB colorspace but hard to find for me.

But now I read, you wrote me solution for RGB so I could go right now and start writing my function :-)
fmw42 wrote: line 1776

For example for an RGB
color_distance^2 = ( (u.r-v.r)^2 + (u.g-v.g)^2 + (u.b-v.b)^2 ) / 3

so fuzz=color_distance if only RGB and no alpha
Now I need to know what value can be u.r, u.g, u.b? (Is it brightness value with maximum integer 100 or maximum float 1 ?)
What value can be v.r, v.g, v.b? ( is it allowed 0-225? )

Re: Fuzz culculator

Posted: 2013-08-16T07:25:31-07:00
by GreenKoopa
u and v are the two images (pixels/colors) being compared. .r, .g, .b are the three channels. Red, green, blue for RGB. Or, somewhat confusingly, Lightness, a, b for Lab. Or hue, saturation, value for HSV. Etc.
VanGog wrote:I found lab is more precise than RGB.
Lab matches human perception better. But which colorspace is "best" depends on your goals. There are many useful colorspaces.

That fuzz formula is purely mathematical. It gives you a measure of distance. Colorspace defines how the colors are laid out, so this impacts the distance.

Re: Fuzz culculator

Posted: 2013-08-16T08:32:23-07:00
by VanGog
I am still not sure about the values and distances. Because I checked many different schemas (graphs) and I don't understand the graph of CIELAB. So what are the rules for input values r,g,b? Is it range of 0-225?

But if I do this in RGB colorspace so I need 1971 xyz namespace, right? So I should use this graphs to understand the topic we are talking about:
Image
Image

I understand the x wave length, but I am still confused from the wiki definition of "CIE xy chromaticity diagram and the CIE xyY color space" where I read
"The CIE XYZ color space was deliberately designed so that the Y parameter was a measure of the brightness or luminance of a color. " because I don't see there any dark/light values (Black near or white near). So not sure what they mean about y axis. I thought it could be Intensity of the color.

Much less I understend the term "distance". Where on the RGB graph can I see it? And do you mean distance as value readable or calculate-able from y axis? How would you read distance on the graph?