# Image comparison

17 replies to this topic

### #1 wolves

wolves
• Members
• Advanced Member
• 65 posts

Posted 23 September 2006 - 02:15 PM

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

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

tks

### #2 shocker-z

shocker-z
• Members
• Advanced Member
• 864 posts
• LocationNottingham

Posted 23 September 2006 - 02:32 PM

huh??  ???

you mean like size of image or what?

Regards
Liam
www: www.ukchat.ws | irc: irc.ukchat.ws chan: #blufudge

### #3 FrOzeN

FrOzeN
• Members
• Advanced Member
• 70 posts

Posted 23 September 2006 - 02:43 PM

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.

### #4 wolves

wolves
• Members
• Advanced Member
• 65 posts

Posted 23 September 2006 - 06:56 PM

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.

yes, I think that this is the idea.

someone knows a script for it? tks

### #5 Orio

Orio
• Staff Alumni
• Advanced Member
• 2,491 posts

Posted 23 September 2006 - 07:08 PM

```<?php

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

?>```

Something like this??

Orio.
Think you're smarty?

(Gone until 20 to November)

### #6 wolves

wolves
• Members
• Advanced Member
• 65 posts

Posted 23 September 2006 - 10:01 PM

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

### #7 AndyB

AndyB
• Staff Alumni
• Advanced Member
• 5,465 posts
• LocationToronto

Posted 23 September 2006 - 10:09 PM

so image 1 seems more to image 3 of image 2

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.
Legend has it that reading the manual never killed anyone.
My site

### #8 wolves

wolves
• Members
• Advanced Member
• 65 posts

Posted 23 September 2006 - 10:12 PM

```those probably don't match at all.
```
ok but, the image seem more with for large squares of another image with 6 small circles ?

### #9 AndyB

AndyB
• Staff Alumni
• Advanced Member
• 5,465 posts
• LocationToronto

Posted 23 September 2006 - 10:16 PM

```those probably don't match at all.
```
ok but, the image seem more with for large squares of another image with 6 small circles ?

'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.
Legend has it that reading the manual never killed anyone.
My site

### #10 wolves

wolves
• Members
• Advanced Member
• 65 posts

Posted 23 September 2006 - 10:23 PM

"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

### #11 Barand

Barand
• Moderators
• Sen . ( ile || sei )
• 18,021 posts

Posted 23 September 2006 - 10:50 PM

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.

```<?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);
?>```

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

where a.gif and b.gif are the images to be compared.
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received

 |baaGrid| easy data tables - and more|baaChart| easy line, column and pie charts

### #12 wolves

wolves
• Members
• Advanced Member
• 65 posts

Posted 23 September 2006 - 11:19 PM

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

### #13 Barand

Barand
• Moderators
• Sen . ( ile || sei )
• 18,021 posts

Posted 23 September 2006 - 11:27 PM

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) . '%';

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received

 |baaGrid| easy data tables - and more|baaChart| easy line, column and pie charts

### #14 wolves

wolves
• Members
• Advanced Member
• 65 posts

Posted 24 September 2006 - 12:10 AM

it is working but
see this images

http://www.set.eesc....ras/Angelim.JPG
http://www.set.eesc....as/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....s/PauBrasil.JPG

will be 100% diferent too . . .

complex images will always return 100% of diference  :-\

### #15 Barand

Barand
• Moderators
• Sen . ( ile || sei )
• 18,021 posts

Posted 24 September 2006 - 12:42 AM

try this version which converts to high contrast grayscale. Small adjustments to contrast have large effect on result.
```<?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);
?>```

If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received

 |baaGrid| easy data tables - and more|baaChart| easy line, column and pie charts

### #16 Barand

Barand
• Moderators
• Sen . ( ile || sei )
• 18,021 posts

Posted 24 September 2006 - 01:31 AM

\$rgb = ImageColorAt(\$im, 100, 100);
\$r = (\$rgb >> 16) & 0xFF;
\$g = (\$rgb >> 8 ) & 0xFF;
\$b = \$rgb & 0xFF;

applies to truecolor images, GD2
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received

 |baaGrid| easy data tables - and more|baaChart| easy line, column and pie charts

### #17 wolves

wolves
• Members
• Advanced Member
• 65 posts

Posted 25 September 2006 - 11:09 AM

tks all especially Barand =)

### #18 wolves

wolves
• Members
• Advanced Member
• 65 posts

Posted 08 December 2006 - 10:53 AM

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

#### 0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users