compare if two images are equal

Blitz3D Forums/Blitz3D Programming/compare if two images are equal

vivaigiochi(Posted 2011) [#1]
Pixel per pixel solution? or there are other algorithm?


Matty(Posted 2011) [#2]
Pixel by pixel....can't think of any other algorithm.

However do you want to know if two images are exactly equal, or if the color differences are less than a certain amount....ie if two images are 'similar'.


_PJ_(Posted 2011) [#3]
If you know what possibility differences may be, i.e. if they're restricted to certain colour values or resolution you can check just for those differences.

If the images are loaded from a file, you might get somewhere with an MD5 check

Otherwise, I guess pixel by pixel is the only real wayto ensure complete parity.


MCP(Posted 2011) [#4]
Pixel by pixel comparison isn't really a feasible solution when dealing with this kind of problem. As an example imagine taking two photos of the same scene...
One is taken during the day. The other at night. Both show the same image but all the lighting has changed.
The pixel comparitor will fail even with a generous tolerance setting. This will be true even for rendered scenes.

The only workable way I can think of is to run both images through an edge detection algorythm. The edges can be easily converted to 2d vectors. These vectors can be rescaled and repositioned so that they can be 'overlayed' over the vector positions of the 2nd image.
A comparision an then be made with a tolerence setting to see how many these vectors match each other in terms of position an orientation ( Even the 'shape' these vector sets create can be taken into consideration ).
With this technique it should be possible for the computer to match two images showing the same scene with differing light levels and will even eliminate camera shake.... in theory.


dynaman(Posted 2011) [#5]
MCP - those two picture would not be "equal" in this sense they might be of the same thing but would not be equal.

The original question needs more detail, what do you mean by equal?


jfk EO-11110(Posted 2011) [#6]
I agree, night and day pictures are really something diffrent. There may be a need for such a detection in certain programs, but basicly I think what makes sense is an image comparation that is able to detect higly similar images, eg: one is JPG and one is a PNG from the same original: pixels won't be the same, but it might look almost identic to the human eye. A tolerance setting is required.

I would suggest to sample the pixels blockwise, eg. in bocks of 8x8 pixels. then add the total red, green and blue of such a bock. Finally compare the results with a certain tolerance. You may also use 2 images with a diffrent size, just adjust the block size. This way an image will be recognized even if it was sharpened or resized before.
To make this faster you may use it as a texture on a quad, position it in front of the camera to get the right size after a renderworld (regardless of image size), then compare the two renders, again with tolerance.

If you only want to test if they are the same files with a diffrent name, load them as a bank and do a checksum test, that's much faster than readpixelfast.


dynaman(Posted 2011) [#7]
> If you only want to test if they are the same files with a diffrent name, load them as a bank and do a checksum test, that's much faster than readpixelfast.

Or open them as bytestreams and compare the bytes, but the checksum test is 99.999% (or more) accurate.


[edit] - if the files are different types (bmp/gif] then checking pixel by pixel is probably required - they would have very different checksums or byte stream info.

Last edited 2011

Last edited 2011


D4NM4N(Posted 2011) [#8]
If you compare the memory sizes of the images in ram wouldn't they be the same?


vivaigiochi(Posted 2011) [#9]
no! i have only image of the same size but it can be useful if it's work with several size.

My real need is if the image are similar, but i want use the size that is equal because it's for this reason that are equal...(almost the first reason).

but i must say this, i have several thousands of images, i need to cut from this, some type of image that i don't see with eyes because it is need much time (have like 80000 image for examples, windows is very slowly when open only the directory)..

yes i need a computer's eye..

Last edited 2011


jfk EO-11110(Posted 2011) [#10]
Note: you could use the faster Resize Image source that I posted some years ago in the code archives, then scale the images down to a very small size (in Ram) then compare the small images, this is faster. Unfort my resize function works only with images up to the screen size. But there are further resize image samples, although some of them without filtering.

I have to say, IF you need to test any picture against any other picture (?), no matter how you do it, with 80000 images this is going to take a while.
BTW. it may be suboptimal to have 80'000 images in one directrory, because windows does a lot of file (and esp. images) checking when you open the directory. Maybe put them in 8 sub-folders? Example: My Foto camera is creating a new folder for every 1000 images, there must be a reason why.

fast resize image:

mine, with the mentioned limits:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1305

by sswift:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1303

by filax:
http://www.blitzbasic.com/codearcs/codearcs.php?code=1503

So, if you must compare each image aginst any other image, you maybe best create a list of reference images. Every further image that you will test will then have to be compared against this list only.

Pseudo code:
list_len=0
for n=0 to number_of_images-1
 already_got_this=0
 read image n
 for l=0 to list_len-1
 if image n similar list image l
  already_got_this=1 : exit()
 endif
 next 
 if already_got_this=0
  add image n to list
   list_len=list_len+1
 endif
next 


Additionally you could write an info file that contains some information about the images, esp. how similar a certain image is, compared to the similar one in the refrence list.


D4NM4N(Posted 2011) [#11]
Perhaps using size as the first test, if it passes that then it takes every size/10 pixel from x & y and analyses it using readpixel fast on some locked image buffers. The chances of two images passing this are pretty slim, -especially- if the images are loaded as JPGs.