How to check if two images are the same
Blitz3D Forums/Blitz3D Beginners Area/How to check if two images are the same
| ||
How can I check that two images are the same? If imgA = imgB doesn't seem to work. |
| ||
Heh, if only life was that simple! :) You'll need to compare every pixel of both images. See: ReadPixelFast(). |
| ||
Really?? That seems like an awfully over the top method. Could this be done in real time, such as simply checking the image handles? |
| ||
Really?? 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.That seems like an awfully over the top method. 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? |
| ||
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... |
| ||
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. |
| ||
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 :) |
| ||
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 |