Page 1 of 1

PHP code to get the most common colour in a block

Posted: Thu Apr 01, 2010 4:19 pm
by DrPL
Hi,
I'm trying to write a bit of code that will read in an image file, and break it into user defined squares; the squares will
be analysed to find the most common colour, and these will drawn to a corresponding square in another image.

I've got the code that reads in the image and outputs it OK, but here are the parts that are causing problems:

Code: Select all

for($row=0; $row<$max_dim; $row++)  // max_dim is the number of squares in each direction
{
	for($col=0; $col<$max_dim; $col++)
	{
		// Chop original image into chunks, of size (width/max_dim) and (height/max_dim)
		//get image details from col to col+1 and row to row+1
		// int imagecolorat  ( resource $image  , int $x  , int $y  )

		if($col<$max_dim-1 && $row<$max_dim-1)
		{	
			$colourfound = commoncolour($imgresized, $row, $col, $dimension, $max_dim);

			$rgb = imagecolorsforindex($imgresized, $colourfound);
			$colorcalc = imagecolorallocate( $img, $rgb["red"], $rgb["green"], $rgb["blue"] );
                        // get the individual red, green and blue components from the imagecolorallocatefunction

			imagefilledrectangle($img, ($offset+$col*$distance),
		 	($offset+$row*$distance), ($offset+$col*$distance),
		 	($offset+$row*$distance), $colourfound); // colour in the square

		}
	}
}

The function that finds the most common colour is as follows:

function commoncolour($img, $row, $col, $dimension, $max_dim)
{
// $row goes from 0 to $max_dim-1 ---> location is ($row)/($max_dim-1) to $row+1/($max_dim-1)

	$colourlist = array();

	for($irow=floor(($row*$dimension/($max_dim-1))); $irow<floor((($row+1)*$dimension/($max_dim-1))); $irow++)
	{

		for($icol=floor(($col*$dimension/($max_dim-1))); $icol<floor((($col+1)*$dimension/($max_dim-1))); $icol++)
		{
		
			$colourval = imagecolorat ($img, $icol, $irow );
			// Now add $colourval to an array

                        // For each pixel in the square, we use a hash table to keep a record of each found colour and the
                       // number of times it is found

			if (!array_key_exists($colourval, $colourlist))
			{
				$colourlist[$colourval]=1;
			}
			else
			{

			// if it already exists, increment counter
				$colourlist[$colourval]++;
			}
		}
	}	
			// when finished sort values

			asort($colourlist);   // The first value will be the most common colour; now we need to get
                         // the value of the colour (ie, the "key")

			reset($colourlist); // make sure array pointer is at first element

			$colfound= key($colourlist);			
			return $colfound;  // return most common colour			
}


The problem is a mystery; I've gone through the code line by line and put echo statements in to interrogate the values
of the variables. The value of $colfound in the function is typically something small, like 0, 1, 57, 232 etc., and the
test image that I am inputting has a variety of colours such as white, black, red, orange, yellow, so the most common
"found colour" is obviously wrong. can some kind soul please help?

best wishes

Paul

Re: PHP code to get the most common colour in a block

Posted: Thu Apr 01, 2010 5:06 pm
by requinix
asort sorts in ascending order.

Re: PHP code to get the most common colour in a block

Posted: Fri Apr 02, 2010 6:33 am
by DrPL
Ah, thanks, I should have popped the last item off the sorted array.
When I run the program I get the following error (with echo statements included to show row and column number):

row= 0 col= 0 Most common colour = 1904
Warning: imagecolorsforindex() [function.imagecolorsforindex]: Color index 1904 out of range in C:\wamp\www\cs_process2.php on line 107
this leads to the following code:

Code: Select all


			$colourfound = commoncolour($imgresized, $row, $col, $dimension, $max_dim);
			$rgb = imagecolorsforindex($imgresized, $colourfound);
$colourfound is the 1904 etc. value; I can't see whats up, can anyone see what is up?

Thanks

Paul