How to check if two images are the same

Blitz3D Forums/Blitz3D Beginners Area/How to check if two images are the same

PoliteProgrammer(Posted 2007) [#1]
How can I check that two images are the same?

If imgA = imgB doesn't seem to work.


big10p(Posted 2007) [#2]
Heh, if only life was that simple! :)

You'll need to compare every pixel of both images. See: ReadPixelFast().


PoliteProgrammer(Posted 2007) [#3]
Really??

That seems like an awfully over the top method.

Could this be done in real time, such as simply checking the image handles?


big10p(Posted 2007) [#4]
Really??

That seems like an awfully over the top method.
I don't see how else it could be done. There's no magic way of seeing if the pixels match, without actually looking at them all.

Could this be done in real time, such as simply checking the image handles?
Image handles are simply the pointers to the struct in mem that's used by blitz to handle the images. Realtime? Depends on how often you want to compare images, how big they are etc.

P.S. What do you need to do this for, exactly? Are the images created in-code or loaded in?


OJay(Posted 2007) [#5]
if you really need it realtime and can live with a chance of 0.01% of failing (e.g. declaring 2 images equal although they arent) then it may be enough to check, e.g. 50 or 100 random pixels of each image...


Buggy(Posted 2007) [#6]
As long as the images are pre-made, have a type field called "pic" or something. If "pic" is 1, the image is a cow. If "pic" is 2, the image is a horse. Then just test whether the "pic" variables are the same.


jfk EO-11110(Posted 2007) [#7]
You may also use a checksum. After loading the image you'll create the checksum. In further comparations you only have to compare the checksums of the images.

; Create 32 bit checksum of an image:
ck=0
for y=0 to imageheight(img)-1
for x=0 to imagewidth(img)-1
ck=ck+readpixel(x,y)
next
next

That's highly reliable.


EDIT
you may even use a modified version to detect a SIMILAR image, eg: when an image was saved with slightly modified pixels (eg. jpg artefacts, brightness, smoothing):

ck=0
for y=0 to imageheight(img)-1
for x=0 to imagewidth(img)-1
;dropping 3 least significant bits of each rgb channel
rgb=readpixel(x,y) and %00000000111110001111100011111000
ck=ck+rgb
next
next

well, at least the theory works :)


BlackJumper(Posted 2007) [#8]
JFKs checksum will not determine between rotated or flipped versions of the same image - if this is needed you will have to apply a weighting based on pixel position before adding to the checksum