Return to Snippet

Revision: 47299
at June 4, 2011 09:09 by pablobablo


Updated Code
class Hamming
{
	private function _resizeImage($from_file, $to_file, $width, $height, $quality)
	{
		$img1 = imagecreatefromjpeg($from_file);
		$img2 = imagecreatetruecolor($width, $height);
		
		imagecopyresampled($img2, $img1, 0, 0, 0, 0, $width, $height, imagesx($img1), imagesy($img1));
		
		imagejpeg($img2, $to_file, $quality);
		
		imagedestroy($img1);
		imagedestroy($img2);
	}
	
	private function _grayScaleImage($filename)
	{
		$img_size 	= getimagesize($filename);
		$width 		= $img_size[0];
		$height		= $img_size[1];
		$img 		= imagecreate($width,$height);
		
		for ($c = 0; $c < 256; $c++)
		{
			imagecolorallocate($img, $c, $c, $c);
		}
		
		$img2 = imagecreatefromjpeg($filename);
		imagecopymerge($img,$img2,0,0,0,0, $width, $height, 100);
		imagejpeg($img, $filename);
		imagedestroy($img);
	}
	
	private function _getPixelValue($filename, $x, $y)
	{
		$image 	= imagecreatefromjpeg($filename);
		$color 	= imagecolorat($image, $x, $y);
		$r 		= ($color >> 16) & 0xFF;
		$g 		= ($color >> 8) & 0xFF;
		$b 		= $color & 0xFF;
		
		imagedestroy($image);
		
		return array('r'=>$r, 'g'=>$g, 'b'=>$b);
	}
	
	private function _getChainOfBits($image)
	{
		$tmpfile = sys_get_temp_dir() . uniqid(md5(rand()), true) . '.jpg';
		$this->_resizeImage($image,  $tmpfile, 8, 8, 100);
		$this->_grayScaleImage($tmpfile);
		
		$a = array();
		for($i=0; $i<8; $i++)
		{
			for($j=0; $j<8; $j++)
			{
				$rgb = $this->_getPixelValue($tmpfile, $i, $j);
				$a[] = $rgb['r'];
			}
		}
		$avrg = (int)(array_sum($a) / count($a));
		
		$s = '';
		foreach ($a as $value)
		{
			$s .= ($value > $avrg ? '0' : '1');
		}
		unlink($tmpfile);
		
		return $s;
	}
	
	public function getDistance($image1, $image2)
	{
		$bits1 = $this->_getChainOfBits($image1);
		$bits2 = $this->_getChainOfBits($image2);
		
		$distance = 0;
		for($i=0; $i<64; $i++)
		{
			$bit1 = $bits1[$i];
			$bit2 = $bits2[$i];
			
			if ( $bit1 != $bit2 )
			{
				$distance++;
			}
		}
		
		return $distance;
	}
}


$hamming = new Hamming();
$distance = $hamming->getDistance('image-1.jpg', 'image-2.jpg');

echo $distance;

Revision: 47298
at June 4, 2011 08:59 by pablobablo


Updated Code
class Hamming
{
	private function _resizeImage($from_file, $to_file, $width, $height, $quality)
	{
		$img1 = imagecreatefromjpeg($from_file);
		$img2 = imagecreatetruecolor($width, $height);
		
		imagecopyresampled($img2, $img1, 0, 0, 0, 0, $width, $height, imagesx($img1), imagesy($img1));
		
		imagejpeg($img2, $to_file, $quality);
		
		imagedestroy($img1);
		imagedestroy($img2);
	}
	
	private function _grayScaleImage($filename)
	{
		$img_size 	= getimagesize($filename);
		$width 		= $img_size[0];
		$height		= $img_size[1];
		$img 		= imagecreate($width,$height);
		
		for ($c = 0; $c < 256; $c++)
		{
			imagecolorallocate($img, $c, $c, $c);
		}
		
		$img2 = imagecreatefromjpeg($filename);
		imagecopymerge($img,$img2,0,0,0,0, $width, $height, 100);
		imagejpeg($img, $filename);
		imagedestroy($img);
	}
	
	private function _getPixelValue($filename, $x, $y)
	{
		$image 	= imagecreatefromjpeg($filename);
		$color 	= imagecolorat($image, $x, $y);
		$r 		= ($color >> 16) & 0xFF;
		$g 		= ($color >> 8) & 0xFF;
		$b 		= $color & 0xFF;
		
		imagedestroy($image);
		
		return array('r'=>$r, 'g'=>$g, 'b'=>$b);
	}
	
	private function _getChainOfBits($image)
	{
		$tmpfile = sys_get_temp_dir() . uniqid(md5(rand()), true) . '.jpg';
		$this->_resizeImage($image,  $tmpfile, 8, 8, 100);
		$this->_grayScaleImage($tmpfile);
		
		$a = array();
		for($i=0; $i<8; $i++)
		{
			for($j=0; $j<8; $j++)
			{
				$rgb = $this->_getPixelValue($tmpfile, $i, $j);
				$a[] = $rgb['r'];
			}
		}
		$avrg = (int)(array_sum($a) / count($a));
		
		$s = '';
		foreach ($a as $value)
		{
			$s .= ($value > $avrg ? '0' : '1');
		}
		unlink($tmpfile);
		
		return $s;
	}
	
	public function getDistance($image1, $image2)
	{
		$bits1 = $this->_getChainOfBits($image1);
		$bits2 = $this->_getChainOfBits($image2);
		
		$distance = 0;
		for($i=0; $i<64; $i++)
		{
			$bit1 = $bits1[$i];
			$bit2 = $bits2[$i];
			
			if ( $bit1 != $bit2 )
			{
				$distance++;
			}
		}
		
		return $distance;
	}
}


// How to use?

$hamming = new Hamming();
$distance = $hamming->getDistance('image-1.jpg', 'image-2.jpg');

echo $distance;

Revision: 47297
at June 4, 2011 08:58 by pablobablo


Updated Code
class Hamming
{
	private function _resizeImage($from_file, $to_file, $width, $height, $quality)
	{
		$img1 = imagecreatefromjpeg($from_file);
		$img2 = imagecreatetruecolor($width, $height);
		
		imagecopyresampled($img2, $img1, 0, 0, 0, 0, $width, $height, imagesx($img1), imagesy($img1));
		
		imagejpeg($img2, $to_file, $quality);
		
		imagedestroy($img1);
		imagedestroy($img2);
	}
	
	private function _grayScaleImage($filename)
	{
		$img_size 	= getimagesize($filename);
		$width 		= $img_size[0];
		$height		= $img_size[1];
		$img 		= imagecreate($width,$height);
		
		for ($c = 0; $c < 256; $c++)
		{
			imagecolorallocate($img, $c, $c, $c);
		}
		
		$img2 = imagecreatefromjpeg($filename);
		imagecopymerge($img,$img2,0,0,0,0, $width, $height, 100);
		imagejpeg($img, $filename);
		imagedestroy($img);
	}
	
	private function _getPixelValue($filename, $x, $y)
	{
		$image 	= imagecreatefromjpeg($filename);
		$color 	= imagecolorat($image, $x, $y);
		$r 		= ($color >> 16) & 0xFF;
		$g 		= ($color >> 8) & 0xFF;
		$b 		= $color & 0xFF;
		
		imagedestroy($image);
		
		return array('r'=>$r, 'g'=>$g, 'b'=>$b);
	}
	
	private function _getChainOfBits($image)
	{
		$tmpfile = sys_get_temp_dir() . uniqid(md5(rand()), true) . '.jpg';
		$this->_resizeImage($image,  $tmpfile, 8, 8, 100);
		$this->_grayScaleImage($tmpfile);
		
		$a = array();
		for($i=0; $i<8; $i++)
		{
			for($j=0; $j<8; $j++)
			{
				$rgb = $this->_getPixelValue($tmpfile, $i, $j);
				$a[] = $rgb['r'];
			}
		}
		$avrg = (int)(array_sum($a) / count($a));
		
		$s = '';
		foreach ($a as $value)
		{
			$s .= ($value > $avrg ? '0' : '1');
		}
		unlink($tmpfile);
		
		return $s;
	}
	
	public function getDistance($image1, $image2)
	{
		$bits1 = $this->_getChainOfBits($image1);
		$bits2 = $this->_getChainOfBits($image2);
		
		$distance = 0;
		for($i=0; $i<64; $i++)
		{
			$bit1 = $bits1[$i];
			$bit2 = $bits2[$i];
			
			if ( $bit1 != $bit2 )
			{
				$distance++;
			}
		}
		
		return $distance;
	}
}


$hamming = new Hamming();
$distance = $hamming->getDistance('image-1.jpg', 'image-2.jpg');

echo $distance;

Revision: 47296
at June 4, 2011 08:57 by pablobablo


Initial Code
class Hamming
	{
		private function _resizeImage($from_file, $to_file, $width, $height, $quality)
		{
			$img1 = imagecreatefromjpeg($from_file);
			$img2 = imagecreatetruecolor($width, $height);
			
			imagecopyresampled($img2, $img1, 0, 0, 0, 0, $width, $height, imagesx($img1), imagesy($img1));
			
			imagejpeg($img2, $to_file, $quality);
			
			imagedestroy($img1);
			imagedestroy($img2);
		}
		
		private function _grayScaleImage($filename)
		{
			$img_size 	= getimagesize($filename);
			$width 		= $img_size[0];
			$height		= $img_size[1];
			$img 		= imagecreate($width,$height);
			
			for ($c = 0; $c < 256; $c++)
			{
				imagecolorallocate($img, $c, $c, $c);
			}
			
			$img2 = imagecreatefromjpeg($filename);
			imagecopymerge($img,$img2,0,0,0,0, $width, $height, 100);
			imagejpeg($img, $filename);
			imagedestroy($img);
		}
		
		private function _getPixelValue($filename, $x, $y)
		{
			$image 	= imagecreatefromjpeg($filename);
			$color 	= imagecolorat($image, $x, $y);
			$r 		= ($color >> 16) & 0xFF;
			$g 		= ($color >> 8) & 0xFF;
			$b 		= $color & 0xFF;
			
			imagedestroy($image);
			
			return array('r'=>$r, 'g'=>$g, 'b'=>$b);
		}
		
		private function _getChainOfBits($image)
		{
			$tmpfile = sys_get_temp_dir() . uniqid(md5(rand()), true) . '.jpg';
			$this->_resizeImage($image,  $tmpfile, 8, 8, 100);
			$this->_grayScaleImage($tmpfile);
			
			$a = array();
			for($i=0; $i<8; $i++)
			{
				for($j=0; $j<8; $j++)
				{
					$rgb = $this->_getPixelValue($tmpfile, $i, $j);
					$a[] = $rgb['r'];
				}
			}
			$avrg = (int)(array_sum($a) / count($a));
			
			$s = '';
			foreach ($a as $value)
			{
				$s .= ($value > $avrg ? '0' : '1');
			}
			unlink($tmpfile);
			
			return $s;
		}
		
		public function getDistance($image1, $image2)
		{
			$bits1 = $this->_getChainOfBits($image1);
			$bits2 = $this->_getChainOfBits($image2);
			
			$distance = 0;
			for($i=0; $i<64; $i++)
			{
				$bit1 = $bits1[$i];
				$bit2 = $bits2[$i];
				
				if ( $bit1 != $bit2 )
				{
					$distance++;
				}
			}
			
			return $distance;
		}
	}
	

	$hamming = new Hamming();
	$distance = $hamming->getDistance('image-1.jpg', 'image-2.jpg');
	
	echo $distance;

Initial URL
http://habrahabr.ru/blogs/image_processing/120562/

Initial Description
Класс для сравнения двух изображений. Возвращает расстояние Хэмминга. Подробнее можно почитать здесь: http://habrahabr.ru/blogs/image_processing/120562/

Initial Title
Как определить сходство двух изображений

Initial Tags

                                

Initial Language
PHP