php imagemagick statistics usage

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
dams
Posts: 2
Joined: 2013-10-27T01:18:19-07:00
Authentication code: 6789

php imagemagick statistics usage

Post 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 )
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: php imagemagick statistics usage

Post 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 .
snibgo's IM pages: im.snibgo.com
dams
Posts: 2
Joined: 2013-10-27T01:18:19-07:00
Authentication code: 6789

Re: php imagemagick statistics usage

Post 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 ?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: php imagemagick statistics usage

Post by snibgo »

Sorry, I only use command-line. I would think modulateimage() is like the "-modulate" command, but it might not be.
snibgo's IM pages: im.snibgo.com
Post Reply