Jump to content

Image comparison


wolves

Recommended Posts

I *think* he means go through them pixel by pixel and adding one to a counter for every (x, y) pixel that corresponds to the other image. Then produce a percentage of pixels that match to the total of pixels in the image.

I haven't worked with images in php yet, so I'm not sure the code you would use, but the above would be the concept.
Link to comment
Share on other sites

[quote author=FrOzeN link=topic=109181.msg439944#msg439944 date=1159022622]
I *think* he means go through them pixel by pixel and adding one to a counter for every (x, y) pixel that corresponds to the other image. Then produce a percentage of pixels that match to the total of pixels in the image.
[/quote]

yes, I think that this is the idea.

someone knows a script for it? tks
Link to comment
Share on other sites

[quote author=wolves link=topic=109181.msg440106#msg440106 date=1159048916]
so image 1 seems more to image 3 of image 2
[/quote]

To a human, yes.  Comparing one image to another pixel by pixel assigns no relevance to the shapes made by all the pixels or their relative spatial arrangement.  For example: I have an image with four large squares in a row, and another image that's a copy of the first but half the width and half the size.  To a human those have some degree of 'match'. On a pixel-by-pixel basis those probably don't match at all.
Link to comment
Share on other sites

[quote author=wolves link=topic=109181.msg440111#msg440111 date=1159049540]
[code]
those probably don't match at all.
[/code]
ok but, the image seem more with for large squares of another image with 6 small circles ?
[/quote]

'Seem' is the human intuitive analysis result.  A php script in entirely logical, i.e. non-intuitive.  Here's another example. Image 1 is a coin; image 2 is the same image rotated 1 degree.  Human says they're the same, computer says they're different.  I don't know what you're trying/hoping to do, but it isn't going to happen with a logical language.
Link to comment
Share on other sites

"Seem" sorry but english is not my native language so it is bad :(

ok I will put my problem..

I need to analyze images of pieces of wood, small pieces but good resolution

Im my database there are lots of diferent images

Now, I have a new image, so I have to compare to the others to see if I have this one or not.

Why not a script that look the all the images and return me some images that looks like to my image
not a perfect comparaison, but a aproximate
Link to comment
Share on other sites

This will do a pixel-by-pixel comparison of 2 same-size gif images and produce a mask of those pixels where the color is different. It also reports the % different.

[code]
<?php
$filea = $_GET['f1'];
$fileb = $_GET['f2'];

$im1 = imagecreatefromgif($filea);
$im2 = imagecreatefromgif($fileb);

$w1 = imagesx($im1);
$w2 = imagesx($im2);
$h1 = imagesx($im1);
$h2 = imagesx($im2);
$w = min($w1, $w2);
$h = min($h1, $h2);
 
$im3 = imagecreate($w,$h+25);
$bg3 = imagecolorallocate($im3,0xff,0xff,0xff);
$fg3 = imagecolorallocate($im3,0xff,0x00,0xff);
$fg3t = imagecolorallocate($im3,0x00,0x00,0x00);

$total_pixels = $w*$h;
$diff = 0;

for ($x=0; $x<$w; $x++) {
    for ($y=0; $y<$h; $y++) {
        if (imagecolorat($im1, $x, $y) != imagecolorat($im2, $x, $y)) {
            $diff++;
            imagesetpixel($im3, $x, $y, $fg3);
        }
    }
}
imagerectangle($im3,0,0,$w-1, $h-1, $fg3t);
$pcdiff = number_format(floor($diff/$total_pixels * 100), 0) . '%';
imagestring($im3, 3, 4, $h+3, $pcdiff, $fg3t);

imagedestroy($im1);
imagedestroy($im2);

header("content-type: image/png");
imagepng($im3);
imagedestroy($im3);
?>[/code]

Call with
<img src='compareImage.php?f1=a.gif&f2=b.gif'>

where a.gif and b.gif are the images to be compared.
Link to comment
Share on other sites

it is working but
see this images

http://www.set.eesc.usp.br/lamem/Texturas/Angelim.JPG
http://www.set.eesc.usp.br/lamem/Texturas/Cedrilho.JPG

they are total diferent analizing it by pixels, but not really total diferent
the number of pixel is the number of different pixels

so if u compare this other image
http://www.set.eesc.usp.br/lamem/Texturas/PauBrasil.JPG

will be 100% diferent too . . .

complex images will always return 100% of diference  :-\
Link to comment
Share on other sites

try this version which converts to high contrast grayscale. Small adjustments to contrast have large effect on result.
[code]
<?php
$filea = $_GET['f1'];
$fileb = $_GET['f2'];

$im1 = imagecreatefromjpeg($filea);
$im2 = imagecreatefromjpeg($fileb);

$w1 = imagesx($im1);
$w2 = imagesx($im2);
$h1 = imagesx($im1);
$h2 = imagesx($im2);
$w = min($w1, $w2);
$h = min($h1, $h2);
 
$im3 = imagecreate($w,$h+25);
$bg3 = imagecolorallocate($im3,0xff,0xff,0xff);
$fg3 = imagecolorallocate($im3,0xff,0x00,0xff);
$fg3t = imagecolorallocate($im3,0x00,0x00,0x00);

$total_pixels = $w*$h;
$diff = 0;

imagefilter($im1, IMG_FILTER_GRAYSCALE);
imagefilter($im2, IMG_FILTER_GRAYSCALE);
imagefilter($im1,IMG_FILTER_CONTRAST, 88);
imagefilter($im2,IMG_FILTER_CONTRAST, 88);

for ($x=0; $x<$w; $x++) {
    for ($y=0; $y<$h; $y++) {
        if (imagecolorat($im1, $x, $y) != imagecolorat($im2, $x, $y)) {
            $diff++;
            imagesetpixel($im3, $x, $y, $fg3);
        }
    }
}
imagerectangle($im3,0,0,$w-1, $h-1, $fg3t);
$pcdiff = number_format(floor($diff/$total_pixels * 100), 0) . '%';
imagestring($im3, 3, 4, $h+3, $pcdiff, $fg3t);

imagedestroy($im1);
imagedestroy($im2);

header("content-type: image/png");
imagepng($im3);
imagedestroy($im3);
?>[/code]
Link to comment
Share on other sites

  • 2 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.