## Fuzz culculator

Discuss digital image processing techniques and algorithms. We encourage its application to ImageMagick but you can discuss any software solutions here.
VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

### Fuzz culculator

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.

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

### Re: Fuzz culculator

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.
snibgo's IM pages: im.snibgo.com

GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

### Re: Fuzz culculator

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.

VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

### Re: Fuzz culculator

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)?

GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

### Re: Fuzz culculator

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

VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

### Re: Fuzz culculator

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.

fmw42
Posts: 26383
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

### Re: Fuzz culculator

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

VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

### Re: Fuzz culculator

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?

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

### Re: Fuzz culculator

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.
snibgo's IM pages: im.snibgo.com

VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

### Re: Fuzz culculator

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

snibgo
Posts: 13034
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

### Re: Fuzz culculator

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%.
snibgo's IM pages: im.snibgo.com

VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

### Re: Fuzz culculator

VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

### Re: Fuzz culculator

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? )

GreenKoopa
Posts: 457
Joined: 2010-11-04T17:24:08-07:00
Authentication code: 8675308

### Re: Fuzz culculator

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.

VanGog
Posts: 308
Joined: 2012-02-05T02:46:58-07:00
Authentication code: 8675308

### Re: Fuzz culculator

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:

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?