efficient way to compare 2 images?
#11
Quote:That's what I'm wondering. 
Maybe there's some magic "compare object" command that I don't know about. 
Evidently comparing it pixel-by-pixel, or a representative sampling of pixels, is a way to do it. 
But see my reply to Pete above to see why I'm asking this, there might be a better approach than comparing images...

Your image comparison is a core problem of the AI: Pattern recognition/comparison, or something. I don't know the exact name at the moment.
The problem was already described by Hubert L. Dreyfus in his first book in 1972: "What Computers Can't Do: The Limits of Artificial Intelligence".

This pattern recognition is essential for the survival of all living beings. For example: A deer must recognize at first glance whether what it sees is harmless or dangerous (such as a wolf).
All the best! Really! - Show the eggheads what basic programmers can do.  Big Grin
Reply
#12
Or, very simply, MEM both images and compare the data within for equality by putting the data into a string and checking image1$ = image2$
Ask me about Windows API and maybe some Linux stuff
Reply
#13
(11-18-2022, 09:08 PM)Kernelpanic Wrote: A deer must recognize at first glance whether what it sees is harmless or dangerous (such as a wolf)
I don't know, man. Deer are quite retarded. A deer will jump at the slightest twig movement but will watch a 2,000 pound metal machine roar towards it and not budge an inch.
Ask me about Windows API and maybe some Linux stuff
Reply
#14
(11-18-2022, 09:10 PM)Spriggsy Wrote:
(11-18-2022, 09:08 PM)Kernelpanic Wrote: A deer must recognize at first glance whether what it sees is harmless or dangerous (such as a wolf)
I don't know, man. Deer are quite retarded. A deer will jump at the slightest twig movement but will watch a 2,000 pound metal machine roar towards it and not budge an inch.

Because the machine doesn't know it and therefore can't classify it (first time): dangerous or not

PS:
There is a video (but where now), about twelve years old, that shows the German Bundeswehr during target practice with howitzers - and about 200 to 300 meters away deer are grazing peacefully. Why? Because they've realized by now that the banging isn't aimed at them.
Reply
#15
squish the two images down to a really small image. If they don't match when they are 2 by 2 pixels they sure aren't going to match when they are say 200 by 200 pixels. 
if it matches keep comparing pixels by pixel in slightly larger scaled image until back up to original size.


crude example:
Code: (Select All)
Screen _NewImage(800, 500, 32)
i1& = _NewImage(200, 200, 32)
i2& = _NewImage(200, 200, 32)
Dim p1 As _Unsigned Long
Dim p2 As _Unsigned Long
Randomize Timer
_Dest i1&
'x1 = Int(Rnd * _Width / 2): x2 = Int(Rnd * _Width / 2 + x1)
'y1 = Int(Rnd * _Height / 2): y2 = Int(Rnd * _Height / 2 + y1)
'Line (x1, y1)-(x2, y2), _RGB32(100, 100, Int(Rnd * 8) + 100), BF
Line (0, 0)-(10, 10), _RGB32(100, 100, 100), BF
_Dest i2&
'x1 = Int(Rnd * _Width / 2): x2 = Int(Rnd * _Width / 2 + x1)
'y1 = Int(Rnd * _Height / 2): y2 = Int(Rnd * _Height / 2 + y1)
'Line (x1, y1)-(x2, y2), _RGB32(100, 100, Int(Rnd * 8) + 100), BF
Line (0, 0)-(10, 11), _RGB32(100, 100, 100), BF
_Dest 0
_PutImage (0, 201), i1&
_PutImage (201, 201), i2&

flagcount = 0
threshold = 2
pass = 0
For xmax = 2 To _Width(i1&) Step 10
    pass = pass + 1
    Locate 1, 1: Print pass 'this really just gives you something to look at
    For ymax = 2 To _Height(i1&) Step 10
        _Limit 100
        a& = _NewImage(xmax, ymax)
        b& = _NewImage(xmax, ymax)
        _PutImage (0, 0)-(xmax, ymax), i1&, a&, (0, 0)-(_Width(i1&), _Height(i1&))
        _PutImage (0, 0)-(xmax, ymax), i2&, b&, (0, 0)-(_Width(i2&), _Height(i2&))
        For py = 1 To ymax
            For px = 1 To xmax
                _Source a&
                p1 = Point(px, py)
                _Source b&
                p2 = Point(px, py)
                If p1 <> p2 Then flagcount = flagcount + 1
                If flagcount >= threshold Then GoTo notthesame
            Next
        Next

    Next
Next
Locate 2, 2
Print "IMAGES are IDENTICAL"
End
notthesame:
Locate 2, 2
Print "IMAGES ARE NOT IDENTICAL"
Reply
#16
Code: (Select All)
Option Explicit
$NoPrefix
$Console:Only

Dim As String file1, file2, hash1, hash2

file1 = "C:\Users\zspriggs\Documents\QB64\qb64pe.exe"
'file2 = "C:\Users\zspriggs\Documents\QB64\pipecomqb64.bas"
file2 = file1 'You can comment this line and uncomment the one above. Obviously, you'll replace the paths with ones you have.


hash1 = GetHash(file1)
hash2 = GetHash(file2)

If hash1 = hash2 Then Print "Same" Else Print "Different"


Function GetHash$ (filename As String)
    Dim As String filecopy: filecopy = "\" + Chr$(34) + filename + "\" + Chr$(34)
    GetHash = pipecom_lite("PowerShell -NoProfile Get-FileHash -Path " + filecopy + " ^| foreach {$_.Hash}")
End Function

'$Include:'pipecomqb64.bas'
This might work for ya.


Attached Files
.bas   pipecomqb64.bas (Size: 8.84 KB / Downloads: 78)
Ask me about Windows API and maybe some Linux stuff
Reply
#17
(11-18-2022, 09:08 PM)Spriggsy Wrote: Or, very simply, MEM both images and compare the data within for equality by putting the data into a string and checking image1$ = image2$

So far, this sounds pretty workable. 
Thanks! 

(I could just manually include fill points in the shape data, but it's fun to solve problems like this!)
Reply
#18
If the images are in a file, just open each into a variable with BINARY.

If the variables are equal, the images are the same....

Pete
If eggs are brain food, Biden takes his scrambled.
Reply
#19
An example with 720p images at play:

Code: (Select All)
Randomize Timer

Screen _NewImage(1280, 720, 32)

'let's make this an unique and pretty image!
For i = 1 To 100
    Line (Rnd * _Width, Rnd * _Height)-(Rnd * width, Rnd * _Height), &HFF000000 + Rnd * &HFFFFFF, BF
Next

image2 = _CopyImage(0) 'identical copies for testing
image3 = _CopyImage(0) 'identical copy...  BUT
_Dest image3
PSet (Rnd * _Width, Rnd * _Height), &HFF000000 + Rnd * &HFFFFFF 'We've just tweaked it so that there's no way in hell it's the same as the other two now!
_Dest 0 'image3 is EXACTLY one pixel different from the other two.  Can we detect that?
image4 = _CopyImage(0) 'an identical copy once again, because 0 will change once we print the results

result1 = CompareImages(0, image2)
result2 = CompareImages(0, image3)
result3 = CompareImages(image2, image3)

Print "Current Screen and Image 1 Compare:  "; result1
Print "Current Screen and Image 2 Compare:  "; result2
Print "Image1 and Image 2 Compare        :  "; result3

Print
Print "Press <ANY KEY> for a speed test!"
Sleep

t# = Timer
Limit = 1000
For i = 1 To Limit
    result = CompareImages(image2, image3)
    result = CompareImages(image2, image4)
Next
Print
Print Using "####.####### seconds to do"; Timer - t#;
Print Limit * 2; "comparisons."


Function CompareImages (handle1 As Long, handle2 As Long)
    $Checking:Off
    Static m(1) As _MEM
    Dim s(1) As String
    m(0) = _MemImage(handle1): m(1) = _MemImage(handle2)
    If m(0).SIZE <> m(1).SIZE Then Exit Function 'not identical
    If m(0).ELEMENTSIZE <> m(1).ELEMENTSIZE Then Exit Function 'not identical
    s(0) = Space$(m(0).SIZE): s(1) = Space$(m(1).SIZE)
    _MemGet m(0), m(0).OFFSET, s(0): _MemGet m(1), m(1).OFFSET, s(1)
    If s(0) = s(1) Then CompareImages = -1
    $Checking:On
End Function
Reply
#20
(11-18-2022, 09:30 PM)Pete Wrote: If the images are in a file, just open each into a variable with BINARY.

If the variables are equal, the images are the same....

Pete

This may not work for a variety of reasons.   Different formats of the same image.  One was saved compressed, the other wasn't.  One has additional file attributes attached to it, that the other doesn't.  You'd want to load the files into memory and compare the contents of the image once they're loaded.  After all, you want to know if the IMAGES are identical; not the files which contain them.  Wink
Reply




Users browsing this thread: 5 Guest(s)