Thursday, March 11, 2010
 

Finding similar pictures using php

Hi just small function witch calculates image Euclidean distance:

  1. http://forum.coppermine-gallery.net/index.php/topic,62619.0.html
  2. http://en.wikipedia.org/wiki/Euclidean_distance

So how to calculate in php all of this for image?

Just simple as this:

function average($imgPath) {

       try {
             $image = new ezcImageAnalyzer( $imgPath  );
             switch ($image->mime) {
             	case 'image/jpeg':     		
             	      $img = imagecreatefromjpeg($imgPath);
             		break;
             		
             	case 'image/gif':     		
             	      $img = imagecreatefromgif($imgPath);
             		break;
             			
             	case 'image/png':     		
             	      $img = imagecreatefrompng($imgPath);
             		break;
             
             	default:
             	    echo $image->mime;exit;
             		break;
             }
       } catch (Exception $e){
           return 0;
       }
       
       
    $w = imagesx($img);
    $h = imagesy($img);
    $r = $g = $b = 0;
    for($y = 0; $y < $h; $y++) {
        for($x = 0; $x < $w; $x++) {
            $rgb = imagecolorat($img, $x, $y);
            $r += $rgb >> 16;
            $g += $rgb >> 8 & 255;
            $b += $rgb & 255;
        }
    }
    $pxls = $w * $h;
    
    $r = round($r / $pxls);
    $g = round($g / $pxls);
    $b = round($b / $pxls);
    $data = array('r' => $r,'g' => $g,'b' => $b);
    
    $euclideanDistance = pow(pow($data['r'],2)+pow($data['g'],2)+pow($data['b'],2),1/5)*10000000;
    return round($euclideanDistance);
}

Currently this feature I will try to implement in one of my gallerys and see what results it produces and then i will update this article :). Perhaps anyone else have better algorithm for comparing images similarity?

Back »

Comments: 0

Leave a reply »

 
  • Leave a Reply
    Your gravatar
    Your Name
     
     
     
     
     
 
About Remdex site

Simple site for simple peoples.

Get in touch

E-mail: remdex@gmail.com