Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- set_time_limit(0);
- error_reporting(E_ALL);
- date_default_timezone_set("Europe/Amsterdam");
- /*
- $image1 = new imagick("image1.png");
- $image2 = new imagick("image2.png");
- $result = $image1->compareImages($image2, Imagick::METRIC_MEANSQUAREERROR);
- $result[0]->setImageFormat("png");
- header("Content-Type: image/png");
- echo $result[0];
- exit;
- */
- $enter = "\r\n";
- class compareImages
- {
- private function mimeType($i) {
- /*returns array with mime type and if its jpg or png. Returns false if it isn't jpg or png*/
- $mime = getimagesize($i);
- $return = array($mime[0],$mime[1]);
- switch ($mime['mime']) {
- case 'image/jpeg':
- $return[] = 'jpg';
- return $return;
- case 'image/png':
- $return[] = 'png';
- return $return;
- default:
- return false;
- }
- }
- private function createImage($i) {
- /*retuns image resource or false if its not jpg or png*/
- $mime = $this->mimeType($i);
- if($mime[2] == 'jpg') {
- return imagecreatefromjpeg ($i);
- }
- else if ($mime[2] == 'png') {
- return imagecreatefrompng ($i);
- }
- else {
- return false;
- }
- }
- private function resizeImage($i,$source) {
- /*resizes the image to a 8x8 squere and returns as image resource*/
- $mime = $this->mimeType($source);
- $t = imagecreatetruecolor(8, 8);
- $source = $this->createImage($source);
- imagecopyresized($t, $source, 0, 0, 0, 0, 8, 8, $mime[0], $mime[1]);
- return $t;
- }
- private function colorMeanValue($i) {
- /*returns the mean value of the colors and the list of all pixel's colors*/
- $colorList = array();
- $colorSum = 0;
- for($a = 0;$a<8;$a++) {
- for($b = 0;$b<8;$b++) {
- $rgb = imagecolorat($i, $a, $b);
- $colorList[] = $rgb & 0xFF;
- $colorSum += $rgb & 0xFF;
- }
- }
- return array($colorSum/64,$colorList);
- }
- private function bits($colorMean) {
- /*returns an array with 1 and zeros. If a color is bigger than the mean value of colors it is 1*/
- $bits = array();
- foreach($colorMean[1] as $color){$bits[]= ($color>=$colorMean[0])?1:0;}
- return $bits;
- }
- public function compare($a,$b) {
- /*main function. returns the hammering distance of two images' bit value*/
- $i1 = $this->createImage($a);
- $i2 = $this->createImage($b);
- if(!$i1 || !$i2){return false;}
- $i1 = $this->resizeImage($i1,$a);
- $i2 = $this->resizeImage($i2,$b);
- imagefilter($i1, IMG_FILTER_GRAYSCALE);
- imagefilter($i2, IMG_FILTER_GRAYSCALE);
- $colorMean1 = $this->colorMeanValue($i1);
- $colorMean2 = $this->colorMeanValue($i2);
- $bits1 = $this->bits($colorMean1);
- $bits2 = $this->bits($colorMean2);
- $hammeringDistance = 0;
- for($a = 0;$a<64;$a++) {
- if($bits1[$a] != $bits2[$a]) {
- $hammeringDistance++;
- }
- }
- return $hammeringDistance;
- }
- }
- class imagediff {
- private $image1, $image2;
- private $rectX, $rectY;
- private $diference = array();
- private $different_pixels = array();
- private $image1src;
- private $image2src;
- private $rectZone;
- function __construct($img1, $img2, $rectX = 50, $rectY = 50) {
- $this->image1['path'] = realpath($img1);
- $this->image2['path'] = realpath($img2);
- $this->image1src = $img1;
- $this->image2src = $img2;
- $this->rectX = $rectX;
- $this->rectY = $rectY;
- if ($this->image1['path'] === false || $this->image2['path'] === false) {
- throw new Exception('Image "' . htmlspecialchars($this->image1 ? $img2 : $img1) . '" not found!<br />');
- }
- else {
- $this->image1['type'] = $this->imagetyte($this->image1['path']);
- $this->image2['type'] = $this->imagetyte($this->image2['path']);
- }
- }
- private function imagetyte($imgname) {
- $file_info = pathinfo($imgname);
- if (!empty($file_info['extension'])) {
- $filetype = strtolower($file_info['extension']);
- $filetype = $filetype == 'jpg' ? 'jpeg' : $filetype;
- $func = 'imagecreatefrom' . $filetype;
- if (function_exists($func)) {
- return $filetype;
- } else {
- throw new Exception('File type "' . htmlspecialchars($filetype) . '" not supported!<br />');
- }
- }
- else {
- throw new Exception('File type not supported! <br />');
- }
- }
- public function createDiffImage($i1, $i2) {
- /* http://www.phpied.com/image-diff/ */
- $this->rectZone[0] = $this->rectX;
- $this->rectZone[1] = $this->rectY;
- list($src_W, $src_H) = getimagesize($i1);
- $ratio_src = $src_W / $src_H;
- ($this->rectZone[0] / $this->rectZone[1] > $ratio_src) ? $this->rectZone[0] = $this->rectZone[1] * $ratio_src : $this->rectZone[1] = $this->rectZone[0] / $ratio_src;
- $i1 = @imagecreatefromjpeg($i1);
- $i2 = @imagecreatefromjpeg($i2);
- $zone1 = imagecreate($this->rectZone[0], $this->rectZone[1]);
- imagecopyresampled($zone1, $i1, 0, 0, 0, 0, $this->rectZone[0], $this->rectZone[1], $src_W, $src_H);
- $zone2 = imagecreate($this->rectZone[0], $this->rectZone[1]);
- imagecopyresampled($zone2, $i2, 0, 0, 0, 0, $this->rectZone[0], $this->rectZone[1], $src_W, $src_H);
- imagefilter($zone1, IMG_FILTER_GRAYSCALE);
- imagefilter($zone2, IMG_FILTER_GRAYSCALE);
- /*
- $gaussian = array(array(1.0, 2.0, 1.0), array(2.0, 4.0, 2.0), array(1.0, 2.0, 1.0));
- imageconvolution($zone1, $gaussian, 16, 0);
- imageconvolution($zone2, $gaussian, 16, 0);
- imagefilter($zone1,IMG_FILTER_PIXELATE,round($this->rectZone[0]/5,0));
- imagefilter($zone2,IMG_FILTER_PIXELATE,round($this->rectZone[0]/5,0));
- */
- $sx1 = imagesx($zone1);
- $sy1 = imagesy($zone1);
- $diffi = imagecreatetruecolor($sx1, $sy1);
- $red = imagecolorallocate($diffi, 255, 0, 0);
- imagefill($diffi, 0, 0, imagecolorallocate($diffi, 0, 0, 0));
- $different_pixels = 0;
- $sumtotal = 0;
- for ($x = 0; $x < $sx1; $x++) {
- for ($y = 0; $y < $sy1; $y++) {
- $rgb1 = imagecolorat($zone1, $x, $y);
- $pix1 = imagecolorsforindex($zone1, $rgb1);
- $rgb2 = imagecolorat($zone2, $x, $y);
- $pix2 = imagecolorsforindex($zone2, $rgb2);
- $mediapixels = pow($pix1['red'] - $pix2['red'], 2) +
- pow($pix1['green'] - $pix2['green'], 2) +
- pow($pix1['blue'] - $pix2['blue'], 2);
- $sumtotal = $sumtotal + $mediapixels;
- if ($mediapixels > 1500) {
- $different_pixels++;
- imagesetpixel($diffi, $x, $y, $red);
- }
- }
- }
- $total = $sx1 * $sy1;
- $percent = number_format(100 * $different_pixels / $total, 2);
- array_push($this->different_pixels, $different_pixels, $percent, ($this->rectZone[0] * $this->rectZone[1]));
- return $this->different_pixels;
- }
- private function imagehex($image) {
- error_reporting(0);
- ini_set("gd.jpeg_ignore_warning", true);
- $this->rectZone[0] = $this->rectX;
- $this->rectZone[1] = $this->rectY;
- $size = getimagesize($image['path']);
- $func = 'imagecreatefrom' . $image['type'];
- $imageres = $func($image['path']);
- $zone = imagecreate($this->rectZone[0], $this->rectZone[1]);
- imagecopyresized($zone, $imageres, 0, 0, 0, 0, $this->rectZone[0], $this->rectZone[1], $size[0], $size[1]);
- $colormap = array();
- $average = 0;
- $result = array();
- for ($x = 0; $x < $this->rectZone[0]; $x++) {
- for ($y = 0; $y < $this->rectZone[1]; $y++) {
- $color = imagecolorat($zone, $x, $y);
- $color = imagecolorsforindex($zone, $color);
- $colormap[$x][$y] = 0.212671 * $color['red'] +
- 0.715160 * $color['green'] +
- 0.072169 * $color['blue'];
- $average += $colormap[$x][$y];
- }
- }
- $average /= ($this->rectZone[0] * $this->rectZone[1]);
- for ($x = 0; $x < $this->rectZone[0]; $x++) {
- for ($y = 0; $y < $this->rectZone[1]; $y++) {
- $result[] = ($x < 10 ? $x : chr($x + 97)) . ($y < 10 ? $y : chr($y + 97)) . round(2 * $colormap[$x][$y] / $average);
- }
- }
- return $result;
- }
- public function diff() {
- $hex1 = $this->imagehex($this->image1);
- $hex2 = $this->imagehex($this->image2);
- $result = (count($hex1) + count($hex2)) - count(array_diff($hex2, $hex1)) - ($this->rectZone[0] * $this->rectZone[1]);
- return $result / ((count($hex1) + count($hex2)) / 2);
- }
- }
- class imagediffOld
- {
- private $image1;
- private $image2;
- function __construct($img1, $img2) {
- $this->image1['path'] = realpath($img1);
- $this->image2['path'] = realpath($img2);
- if($this->image1['path'] === false || $this->image2['path'] === false) {
- throw new Exception('Image "'.htmlspecialchars( $this->image1 ? $img2 : $img1 ).'" not found!');
- }
- else {
- $this->image1['type'] = $this->imagetyte($this->image1['path']);
- $this->image2['type'] = $this->imagetyte($this->image2['path']);
- }
- }
- private function imagetyte($imgname) {
- $file_info = pathinfo($imgname);
- if(!empty ($file_info['extension'])) {
- $filetype = strtolower($file_info['extension']);
- $filetype = $filetype == 'jpg' ? 'jpeg' : $filetype;
- $func = 'imagecreatefrom' . $filetype;
- if(function_exists($func)) {
- return $filetype;
- }
- else {
- throw new Exception('File type "'.htmlspecialchars( $filetype ).'" not supported!');
- }
- }
- else {
- throw new Exception('File type not supported!');
- }
- }
- private function imagehex($image) {
- $size = getimagesize($image['path']);
- $func = 'imagecreatefrom'.$image['type'];
- $imageres = $func($image['path']);
- $zone = imagecreate(20, 20);
- imagecopyresized($zone, $imageres, 0, 0, 0, 0, 20, 20, $size[0], $size[1]);
- $colormap = array();
- $average = 0;
- $result = array();
- for($x=0; $x<20; $x++) {
- for($y=0; $y<20; $y++) {
- $color = imagecolorat($zone, $x, $y);
- $color = imagecolorsforindex($zone, $color);
- $colormap[$x][$y]= 0.212671 * $color['red'] + 0.715160 * $color['green'] + 0.072169 * $color['blue'];
- $average += $colormap[$x][$y];
- }
- }
- $average /= 400;
- for($x=0; $x<20; $x++) {
- for($y=0; $y<20; $y++) {
- $result[]=($x<10?$x:chr($x+97)) . ($y<10?$y:chr($y+97)) . round(2*$colormap[$x][$y]/$average);
- }
- }
- return $result;
- }
- public function diff() {
- $hex1 = $this->imagehex($this->image1) ;
- $hex2 = $this->imagehex($this->image2);
- $result=(count($hex1) + count($hex2)) - count(array_diff($hex2,$hex1))-400;
- return $result / ( ( count($hex1) + count($hex2) ) / 2 );
- }
- }
- $photos = glob("d:/photos/snapshots/20140301/*.jpg");
- usort($photos, function($a, $b) {
- return strcmp($b, $a);
- });
- $current = array_shift($photos);
- $cnt = 0;
- echo '<table>';
- function microtime_float() {
- list($usec, $sec) = explode(" ", microtime());
- return ((float)$usec + (float)$sec);
- }
- $best = array();
- $missed = array();
- foreach ($photos AS $next) {
- $start = microtime_float();
- $diff = new imagediff($current, $next, 64, 64, true);
- $tmp = $diff->diff() * 100;
- $endTime = microtime_float() - $start;
- $cnt++;
- $array = array();
- $best_cur = 999;
- $best_cur_id = 999;
- if ($tmp < 80) {
- $comp = new compareImages();
- echo '<tr><td>' . $enter;
- echo '<img src="file:///' . str_replace("\\", "/", $current) . '" width="256" height="256"></td><td><img src="file:///' . str_replace("\\", "/", $next) . '" width="256" height="256"></td><td>' . $enter;
- echo 'Time: ' . date("H:i:s") . '<br />';
- echo 'Item: ' . $cnt . $enter . '<br />';
- $tmp = number_format($tmp, 2);
- $array[] = array($tmp, $endTime);
- $start = microtime_float();
- $tmp = $comp->compare($current, $next);
- $endTime = number_format(microtime_float() - $start,2);
- $array[] = array($tmp, $endTime);
- $start = microtime_float();
- $diffy = new imagediffOld($current, $next);
- $tmp = ($diffy->diff() * 100);
- $endTime = number_format(microtime_float() - $start,2);
- $array[] = array($tmp, $endTime);
- $start = microtime_float();
- $tmp = number_format(exec('"c:\Program Files\ImageMagick-6.8.8-Q16\compare.exe" -metric PSNR ' . $current . ' ' . $next . ' NULL: 2>&1'),2);
- $endTime = number_format(microtime_float() - $start,2);
- $array[] = array($tmp, $endTime);
- $start = microtime_float();
- $tmp = number_format(exec('"c:\Program Files\ImageMagick-6.8.8-Q16\compare.exe" -metric NCC ' . $current . ' ' . $next . ' NULL: 2>&1'),2);
- $endTime = number_format(microtime_float() - $start,2);
- $array[] = array($tmp, $endTime);
- foreach ($array AS $k=>$v) {
- if ($best_cur > $v[1]) { $best_cur = $v[1]; $best_cur_id = $k; }
- echo $k . ': ' . $v[0] . ' (' . number_format($v[1], 2) . ')<br />';
- }
- $best[$best_cur_id]++;
- // echo $current . ';' . '</td><td>';
- // echo $next . ';' . $enter;
- echo '</td></tr>';
- }
- else {
- $missed[] = $next;
- }
- $current = $next;
- flush();
- }
- print_r($best);
- print_r($missed);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement