calculate fuzzyness

IMagick is a native PHP extension to create and modify images using the ImageMagick API. ImageMagick Studio LLC did not write nor does it maintain the IMagick extension, however, IMagick users are welcome to discuss the extension here.
Post Reply
hampleman

calculate fuzzyness

Post by hampleman »

Dear image enchanting people,

For my online collage machine I need to make transparent a range of colors between colour X and color Y.

Code: Select all

$image->paintTransparentImage  ( $color_x  , 0  , $fuzzyness );
Before that I need to calculate the fuzzyness between these colors. The common function to calculate the color distance does not work. It gives too high values.

Code: Select all

$col_x = $color_x->getColor();
$col_y = $color_y->getColor();
$fuzzyness = sqrt( pow ($col_x['r'] - $col_y['r'] , 2) + pow( $col_x['g'] - $col_y['g'] , 2) + pow( $col_x['b'] - $col_y['b'] , 2) ) * 256;
Strangely enough the Q16 compiled Imagick gives 8 bits RGB values in the ImagickPixel class, while the fuzzyness needs a 16 bit value, but well, that isn't the question here.
I then tried a rather boorish function that also gave to high values (according to some visual tests) and took too long to calculate the fuzzyness between a whole histogram of colors.

Code: Select all

function fuzz($a,$b){
	//$a:ImagickPixel
	//$b:ImagickPixel	
	//returns:float
	if (is_object($a) && is_object($b)){
		$i =0;
		$hit = false;
		while (!$hit){
			$hit = $a->isSimilar ($b, $i);
			$i ++;
		}		
		return $i;
	}
	else{
		return 0;
	}
}
Please don't laugh!
Does anyone know how Image Magick determines the fuzzyness between two colors?

Thanks in advance,
Fabian
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: calculate fuzzyness

Post by fmw42 »

It has changed just recently to handle transparency differently. see viewtopic.php?f=1&t=17584&hilit=fuzz
hampleman

Re: calculate fuzzyness

Post by hampleman »

Thanks a lot! That link is just what I needed.
I was almost there with my colorDist function, but I forgot to divide the whole mim under the sqaure root by 3...
Now I can use this function and forget that boorish and slow function fuzz (that does give correct results by the way)

Code: Select all

/**
* calculate color distance betweeen two colors   
* @param $a:ImagickPixel
* @param $b:ImagickPixel	
* @return float */
function colDist($a, $b){
	//@param $a:ImagickPixel
	//@param $b:ImagickPixel	
	//returns a normalized float [0..1]
	if (is_object($a) && is_object($b)){
		//get normalized colors
		$col_a = $a->getColor(1);
		$col_b = $b->getColor(1);				
		return sqrt( ( pow($col_a['r']-$col_b['r'],2) + pow($col_a['g']-$col_b['g'],2) + pow($col_a['b']-$col_b['b'],2))/3); 
	}		
	else{
		return 0;
	}
}
Post Reply