Page 1 of 1

can't find function for this

Posted: 2009-09-16T10:34:45-07:00
by vonix
hello! :)

i want to shift every color channel from a source image towards the color of a destination image by a specific factor (0..1), in order to make the source image look a little more like the destination image.

out = src + fac * (dest - src)

i couldn't find anything in the spare documentation and i didn't even achieve to manipulate the image manually on per pixel basis.

can someone help me please?

Re: can't find function for this

Posted: 2009-09-16T11:10:38-07:00
by magick
See http://www.imagemagick.org/Usage/transform/#evaluate. Hopefully iMagick has a EvaluateImage() method.

Re: can't find function for this

Posted: 2009-09-16T11:12:19-07:00
by vonix
it has, but only for single images
btw: what happens if i subtract white from grey (127 - 255)? are negative values just clamped? and if i multiply them (255 * 127)? are the mapped to 255*255?

Re: can't find function for this

Posted: 2009-09-16T11:38:26-07:00
by magick
Yes, color components are clamped unless you use the HDRI version of ImageMagick. See http://www.imagemagick.org/script/high- ... -range.php.

Re: can't find function for this

Posted: 2009-09-16T11:59:48-07:00
by vonix
hm, i don't think my host supports HDRI image magick and the color correction should be achievable with standard methods somehow...

all i want to do is move a green pixel slightly (30%) towards a red pixel for example, to get a red-ish green :) ... i just don't know how

corresponding to compose/#math: "add" compared to "plus" does a modulo if it would be clamp. so if it is viewed binary, it should be negative?

my idea was:
dest = dest SUBTRACT src
diff = dest MULTIPLY light gray (=30% or 0.3)
src = src ADD diff

but all i get are random colors... :?

Re: can't find function for this

Posted: 2009-09-16T20:54:02-07:00
by fmw42
have you tried the Imagick equivalent of -modulate to shift the hue?

see http://www.imagemagick.org/script/comma ... p#modulate

Re: can't find function for this

Posted: 2009-09-17T07:10:22-07:00
by vonix
i think i haven't made it clear enough what my objective is. i just want to move every color of a source image a little more towards the color of a destination image (source and destination image have the same dimensions)

Re: can't find function for this

Posted: 2009-09-17T09:27:52-07:00
by vonix
i implemented this in GD, however it takes about 4seconds to calculate on 1024x768. and i found that COMPOSITE_THRESHOLD provides exactly this functionality.

does anyone know how i can vary the factor for compositeImage()?

i tried setImageCompose() and setOption(), with every permutation of 'option:', 'compose', 'composite', 'compose:args', 'composite:args' and factors of 0.1, 0.5, 1, 50, 255, 500 but the factor is always 50%.


Code: Select all

$f = 0.3;

$srcImg = imagecreatefromjpeg( 'src.jpg' );

$destImg = imagecreatefromjpeg( 'dest.jpg' );

$width = imagesx( $srcImg );
$height = imagesy( $srcImg );
$outImg = imagecreatetruecolor( $width, $height );

for( $y = 0; $y < $height; $y++ )
{
	for( $x = 0; $x < $width; $x++ )
	{
		$srcRGB = imagecolorat( $srcImg, $x, $y );
		$srcR = ($srcRGB >> 16) & 0xFF; 
		$srcG = ($srcRGB >>  8) & 0xFF; 
		$srcB = ($srcRGB >>  0) & 0xFF;
		 
		$destRGB = imagecolorat( $destImg, $x, $y );
		$destR = ($destRGB >> 16) & 0xFF; 
		$destG = ($destRGB >>  8) & 0xFF; 
		$destB = ($destRGB >>  0) & 0xFF;
		
		$r = (int)($srcR + $f * ($destR - $srcR));  
		$g = (int)($srcG + $f * ($destG - $srcG));  
		$b = (int)($srcB + $f * ($destB - $srcB));
		
		$rgb = (int)(($r << 16) | ($g << 8) | $b);
		imagesetpixel( $outImg, $x, $y, $rgb);
	}
}

imagejpeg( $outImg, 'corrected.jpg' );
[/size]

Re: can't find function for this

Posted: 2009-09-17T16:42:45-07:00
by magick
This is an ImageMagick forum. You'll need to find a GD forum for help with GD.

Re: can't find function for this

Posted: 2009-09-18T01:00:56-07:00
by vonix
my actual question is IMagick related, i just wanted to show what i wanna do in GD

Re: can't find function for this

Posted: 2009-09-30T21:41:00-07:00
by fmw42
again

have you tried the Imagick equivalent of -modulate to shift the hue?

see http://www.imagemagick.org/script/comma ... p#modulate
and http://www.imagemagick.org/Usage/color/#modulate


see modulateImage at http://us.php.net/imagick