Jump to content

Archived

This topic is now archived and is closed to further replies.

wolves

Image comparison

Recommended Posts

someone knows how to compare one image to another one?
like

how many percent dog.gif is equal to dog1.gif ?

tks

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
[code]<?php

if(imagesx($pic1)==imagesx($pic2) && imagesy($pic1)==imagesy($pic2))
{
echo("Equal");
}

?>[/code]

Something like this??

Orio.

Share this post


Link to post
Share on other sites
image 1 has 4 squares and 2 circles
image 2 has 3 circles and 4 triangle
image 3 has 3 oval formas and 3 squares

so image 1 seems more to image 3 of image 2


 

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
[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 ?

Share this post


Link to post
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.

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
it is creating a new image with all diferences between img 1 and img 2
but always 0% of diference,

I will try to fix

tks

Share this post


Link to post
Share on other sites
If it's very small % changes, change

$pcdiff = number_format(floor($diff/$total_pixels * 100), 0) . '%';

to

$pcdiff = number_format($diff/$total_pixels * 100, 2) . '%';

Share this post


Link to post
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  :-\

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites
[quote=php manual imagecolorat()]
$rgb = ImageColorAt($im, 100, 100);
$r = ($rgb >> 16) & 0xFF;
$g = ($rgb >> 8 ) & 0xFF;
$b = $rgb & 0xFF;
[/quote]

applies to truecolor images, GD2

Share this post


Link to post
Share on other sites
now I need to compare ".pgm" files

there is a away to do this in php?

i looked at docs, but I found nothing about .pgm

Share this post


Link to post
Share on other sites

×

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.