Page 1 of 1

php imagemagick statistics usage

Posted: 2013-10-27T01:21:32-07:00
by dams
Hello

I'm trying to compute the average brightness of an image using Imagemagick's getImageChannelStatistics function. I will then use modulateImage to decrease the brightness if it reaches a given threshold.

Code: Select all

array Imagick::getImageChannelStatistics ( void )
1st question: The returned mean value of each channel is greater than 255, although the color depth is 8. How to interpret these values ?

Code: Select all

Array ( [mean] => 27510.293108724 [minima] => 0 [maxima] => 65535 [standardDeviation] => 23761.909802897 [depth] => 8 )

Array ( [mean] => 22654.046931424 [minima] => 0 [maxima] => 65535 [standardDeviation] => 21085.309916751 [depth] => 8 )

Array ( [mean] => 21137.418988715 [minima] => 0 [maxima] => 65535 [standardDeviation] => 20369.810455127 [depth] => 8 )
2nd question: What is the relation between the mean value and brightness of an image ?

Code: Select all

bool Imagick::modulateImage ( float $brightness , float $saturation , float $hue )

Re: php imagemagick statistics usage

Posted: 2013-10-27T03:53:15-07:00
by snibgo
1. You are using Q16, so when an image is read into memory, each channel takes 16 bits, and values range from 0 to 65535.

2. See http://www.imagemagick.org/script/comma ... p#modulate , but shifting the mean is perhaps easier with gamma, see http://www.imagemagick.org/script/comma ... .php#gamma and thread viewtopic.php?f=1&t=23389&p=98263 .

Re: php imagemagick statistics usage

Posted: 2013-10-27T06:56:11-07:00
by dams
Here is my code:

Code: Select all

function doIt($file) {
	
	$im = new Imagick( $file );
	// resize by 200 width and keep the ratio
	$stats = $im->getImageChannelStatistics();
	
	// Print Statistics
	// ---------------------------------------------
	$maxima = $stats[imagick::CHANNEL_RED]['maxima'];
	
	$lum_i = ( $stats[imagick::CHANNEL_RED]['mean'] + $stats[imagick::CHANNEL_RED]['mean'] + $stats[imagick::CHANNEL_RED]['mean'] ) / (3 * $maxima);
	
	print_r('     =>' . $file . '      :     ');
	print_r($lum_i);
	
	$lum_max = 0.4;
	
	if ($lum_i > $lum_max) {
	
		$factor =$lum_max /  $lum_i;
		print_r("   /!\ correction: ");
		print_r($factor * 100);
		
		//$im->negateimage(false);
		$im->modulateimage($factor * 100);
		//$im->negateimage(false);
	}
	$im->writeImage();
	
}
The correction factor seems to work, but modulateimage does nothing, even if I manually set $factor to 0.
What is the correct usage of modulateimage ?

Re: php imagemagick statistics usage

Posted: 2013-10-27T12:55:04-07:00
by snibgo
Sorry, I only use command-line. I would think modulateimage() is like the "-modulate" command, but it might not be.