efficient way to compare 2 images?
#33
(11-18-2022, 09:21 PM)James D Jarvis Wrote: 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:
...

Hey, this works! (See test below...)

(11-18-2022, 11:40 PM)bplus Wrote: Well my idea didn't take so long for one compare, just blink and there's your answer:
...

^^^
Will need to compare the performance against bplus' method.

(11-18-2022, 11:40 PM)bplus Wrote: Not nearly as quick as memory methods though.

What was that memory method again?

(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$

I'm not too familiar with _MEM, will have to look it up. 

It would be interesting to see how the performance compares against what we have so far! 

Dinner time! I'll have to check back later...
Thanks guys!

Code: (Select All)
' ?????????????????????????????????????????????????????????????????????????????
' HOW MIGHT WE EFFICIENTLY COMPARE TWO IMAGES?
' ?????????????????????????????????????????????????????????????????????????????

Const FALSE = 0
Const TRUE = Not FALSE

' DO TEST
CompareImagesSquishTest

' CLEAR IMAGES
Screen 0
System


' /////////////////////////////////////////////////////////////////////////////

Sub CompareImagesSquishTest ()
    Dim image1&
    Dim image2&
    Dim bEqual%

    image1& = _NewImage(100, 100, 32)
    image2& = _NewImage(100, 100, 32)

    Screen _NewImage(1024, 768, 32)

    ' -----------------------------------------------------------------------------
    ' TEST #1
    _Dest 0: Cls , cBlack

    ' CREATE IMAGES (NOT EQUAL)
    DrawSquare image1&, 10, 10, 80, cRed, cBlue
    DrawSquare image2&, 10, 10, 80, cRed, cYellow

    ' COMAPRE IMAGES
    bEqual% = CompareImagesSquish%(image1&, image2&)

    ' SHOW IMAGES
    _Dest 0
    _PutImage (100, 100), image1&, 0
    _PutImage (300, 100), image2&, 0

    ' SHOW RESULTS
    If bEqual% = TRUE Then
        DrawEqual 200, 100
    Else
        DrawNot 200, 100
        DrawEqual 200, 100
    End If

    ' WAIT FOR USER
    Locate 1, 1: Print "PRESS ANY KEY TO CONTINUE": Sleep
   
    ' -----------------------------------------------------------------------------
    ' TEST #2
    _Dest 0: Cls , cBlack
   
    ' UPDATE image2 TO MATCH image1
    _Dest image2&
    Paint (55, 85), cBlue, cRed
   
    ' COMAPRE IMAGES
    bEqual% = CompareImagesSquish%(image1&, image2&)
   
    ' SHOW IMAGES
    _Dest 0
    _PutImage (100, 100), image1&, 0
    _PutImage (300, 100), image2&, 0
   
    ' SHOW RESULTS
    If bEqual% = TRUE Then
        DrawEqual 200, 100
    Else
        DrawNot 200, 100
        DrawEqual 200, 100
    End If
   
    ' WAIT FOR USER
    Locate 1, 1: Print "PRESS ANY KEY TO CONTINUE": Sleep
   
    ' -----------------------------------------------------------------------------
    ' TEST #3
    _Dest 0: Cls , cBlack
   
    ' UPDATE image1 TO DIFF image2
    _Dest image1&
    DrawSquare image1&, 20, 20, 10, cLime, cBlue
   
    ' COMAPRE IMAGES
    bEqual% = CompareImagesSquish%(image1&, image2&)
   
    ' SHOW IMAGES
    _Dest 0
    _PutImage (100, 100), image1&, 0
    _PutImage (300, 100), image2&, 0
   
    ' SHOW RESULTS
    If bEqual% = TRUE Then
        DrawEqual 200, 100
    Else
        DrawNot 200, 100
        DrawEqual 200, 100
    End If
   
    ' WAIT FOR USER
    Locate 1, 1: Print "PRESS ANY KEY TO CONTINUE": Sleep
   
    ' -----------------------------------------------------------------------------
    ' TEST #4
    _Dest 0: Cls , cBlack
   
    ' UPDATE image1 TO MATCH image2
    _Dest image1&
    DrawSquare image1&, 20, 20, 10, cBlue, cBlue
   
    ' COMAPRE IMAGES
    bEqual% = CompareImagesSquish%(image1&, image2&)
   
    ' SHOW IMAGES
    _Dest 0
    _PutImage (100, 100), image1&, 0
    _PutImage (300, 100), image2&, 0
   
    ' SHOW RESULTS
    If bEqual% = TRUE Then
        DrawEqual 200, 100
    Else
        DrawNot 200, 100
        DrawEqual 200, 100
    End If
   
    ' WAIT FOR USER
    Locate 1, 1: Print "PRESS ANY KEY TO CONTINUE": Sleep
   
    ' -----------------------------------------------------------------------------
    ' TEST #5
    _Dest 0: Cls , cBlack
   
    ' UPDATE image2 TO DIFF image1 (ONE PIXEL)
    _Dest image2&
    Line (50, 50)-(50, 50), cWhite, , 65535
   
    ' COMAPRE IMAGES
    bEqual% = CompareImagesSquish%(image1&, image2&)
   
    ' SHOW IMAGES
    _Dest 0
    _PutImage (100, 100), image1&, 0
    _PutImage (300, 100), image2&, 0
   
    ' SHOW RESULTS
    If bEqual% = TRUE Then
        DrawEqual 200, 100
    Else
        DrawNot 200, 100
        DrawEqual 200, 100
    End If
   
    ' WAIT FOR USER
    Locate 1, 1: Print "PRESS ANY KEY TO CONTINUE": Sleep
   
    ' -----------------------------------------------------------------------------
    ' TEST #6
    _Dest 0: Cls , cBlack
   
    ' UPDATE image2 TO MATCH image1
    _Dest image2&
    Line (50, 50)-(50, 50), cBlue, , 65535
   
    ' COMAPRE IMAGES
    bEqual% = CompareImagesSquish%(image1&, image2&)
   
    ' SHOW IMAGES
    _Dest 0
    _PutImage (100, 100), image1&, 0
    _PutImage (300, 100), image2&, 0
   
    ' SHOW RESULTS
    If bEqual% = TRUE Then
        DrawEqual 200, 100
    Else
        DrawNot 200, 100
        DrawEqual 200, 100
    End If
   
    ' WAIT FOR USER
    Locate 1, 1: Print "PRESS ANY KEY TO CONTINUE": Sleep
   
    ' -----------------------------------------------------------------------------
    ' CLEANUP
    If image1& < -1 Then _FreeImage image1&
    If image2& < -1 Then _FreeImage image2&
End Sub ' CompareImagesSquishTest

' /////////////////////////////////////////////////////////////////////////////

' James D Jarvis
' https://staging.qb64phoenix.com/showthread.php?tid=1151&page=2

' 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.

Function CompareImagesSquish% (image1&, image2&)
    Dim bResult%
    Dim iDiffCount%
    Dim iThreshold%
    Dim iPassNum%
    Dim xMax%
    Dim yMax%
    Dim pic1&
    Dim pic2&
    Dim px%
    Dim py%
    Dim color1~&
    Dim color2~&
    Dim bFinished%
   
    bResult% = TRUE
    bFinished% = FALSE
    iDiffCount% = 0
    iThreshold% = 2
    iPassNum% = 0
    For xMax% = 2 To _Width(image1&) Step 10
        iPassNum% = iPassNum% + 1
        'Locate 1, 1: Print iPassNum% ' this really just gives you something to look at
        For yMax% = 2 To _Height(image1&) Step 10
            pic1& = _NewImage(xMax%, yMax%)
            pic2& = _NewImage(xMax%, yMax%)
            _PutImage (0, 0)-(xMax%, yMax%), image1&, pic1&, (0, 0)-(_Width(image1&), _Height(image1&))
            _PutImage (0, 0)-(xMax%, yMax%), image2&, pic2&, (0, 0)-(_Width(image2&), _Height(image2&))
            For py% = 1 To yMax%
                For px% = 1 To xMax%
                    _Source pic1&: color1 = Point(px%, py%)
                    _Source pic2&: color2 = Point(px%, py%)
                    If color1 <> color2 Then
                        iDiffCount% = iDiffCount% + 1
                        If iDiffCount% >= iThreshold% Then
                            bResult% = FALSE
                            bFinished% = TRUE
                            Exit For
                        End If
                    End If
                Next px%
                If bFinished% = TRUE Then Exit For
            Next py%
            If bFinished% = TRUE Then Exit For
        Next yMax%
    Next xMax%
   
    If pic1& < -1 Then _FreeImage pic1&
    If pic2& < -1 Then _FreeImage pic2&
   
    CompareImagesSquish% = bResult%
End Function ' CompareImagesSquish%

' /////////////////////////////////////////////////////////////////////////////

Sub DrawSquare (img&, x1%, y1%, size%, fgcolor~&, bgcolor~&)
    Dim x2%, y2%
    If img& < -1 Then
        _Dest img& ': Cls , cEmpty

        x2% = (x1% + size%) - 1
        y2% = (y1% + size%) - 1

        Line (x1%, y1%)-(x2%, y1%), fgcolor~&, , 65535
        Line (x2%, y1%)-(x2%, y2%), fgcolor~&, , 65535
        Line (x2%, y2%)-(x1%, y2%), fgcolor~&, , 65535
        Line (x1%, y2%)-(x1%, y1%), fgcolor~&, , 65535

        If bgcolor~& <> cEmpty Then
            'PAINT [STEP] (column%, row%), fillColor[, borderColor%]
            Paint (x1% + 1, y1% + 1), bgcolor~&, fgcolor~&
        End If
    End If
End Sub ' Draw Square

' /////////////////////////////////////////////////////////////////////////////

Sub DrawEqual (x%, y%)
    Dim x1%, y1%, x2%, y2%
    _Dest 0
   
    ' = (equal)
    x1% = (x% + 30) + 0: y1% = (y% + 30) + 0
    x2% = (x% + 70) - 1: y2% = (y% + 70) - 1
    Line (x1%, y1%)-(x2%, y1%), cWhite, , 65535
    Line (x1%, y2%)-(x2%, y2%), cWhite, , 65535
End Sub ' DrawEqual

' /////////////////////////////////////////////////////////////////////////////

Sub DrawNot (x%, y%)
    Dim x1%, y1%, x2%, y2%
    _Dest 0
   
    ' ! (not)
    x1% = (x% + 10) + 0: y1% = (y% + 10) + 0
    x2% = x1%: y2% = (y% + 75) - 1
    Line (x1%, y1%)-(x2%, y2%), cWhite, , 65535
    x1% = (x% + 10) + 0: y1% = (y% + 85) + 0
    x2% = x1%: y2% = (y% + 90) - 1
    Line (x1%, y1%)-(x2%, y2%), cWhite, , 65535
   
End Sub ' DrawNotEqual

' /////////////////////////////////////////////////////////////////////////////

Function cBlack~& ()
    cBlack = _RGB32(0, 0, 0)
End Function ' cBlack~&
Function cGray~& ()
    cGray = _RGB32(128, 128, 128)
End Function ' cGray~&
Function cWhite~& ()
    cWhite = _RGB32(255, 255, 255)
End Function ' cWhite~&
Function cRed~& ()
    cRed = _RGB32(255, 0, 0)
End Function
Function cOrange~& ()
    cOrange = _RGB32(255, 165, 0)
End Function ' cOrange~&
Function cYellow~& ()
    cYellow = _RGB32(255, 255, 0)
End Function ' cYellow~&
Function cLime~& ()
    cLime = _RGB32(0, 255, 0)
End Function ' cLime~&
Function cCyan~& ()
    cCyan = _RGB32(0, 255, 255)
End Function ' cCyan~&
Function cBlue~& ()
    cBlue = _RGB32(0, 0, 255)
End Function ' cBlue~&
Function cPurple~& ()
    cPurple = _RGB32(128, 0, 255)
End Function ' cPurple~&
Function cMagenta~& ()
    cMagenta = _RGB32(255, 0, 255)
End Function ' cMagenta~&
Function cEmpty~& ()
    cEmpty = _RGB32(0, 0, 0, 0)
End Function ' cEmpty~&
Reply


Messages In This Thread
efficient way to compare 2 images? - by madscijr - 11-18-2022, 07:51 PM
RE: efficient way to compare 2 images? - by bplus - 11-18-2022, 08:10 PM
RE: efficient way to compare 2 images? - by Pete - 11-18-2022, 08:26 PM
RE: efficient way to compare 2 images? - by Pete - 11-18-2022, 08:55 PM
RE: efficient way to compare 2 images? - by Pete - 11-18-2022, 09:30 PM
RE: efficient way to compare 2 images? - by Pete - 11-18-2022, 09:52 PM
RE: efficient way to compare 2 images? - by Pete - 11-18-2022, 11:08 PM
RE: efficient way to compare 2 images? - by bplus - 11-18-2022, 11:40 PM
RE: efficient way to compare 2 images? - by madscijr - 11-19-2022, 12:05 AM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 12:22 AM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 01:22 AM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 05:39 PM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 06:28 PM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 10:54 PM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 11:54 PM
RE: efficient way to compare 2 images? - by bplus - 11-19-2022, 11:57 PM
RE: efficient way to compare 2 images? - by bplus - 11-20-2022, 12:02 AM
RE: efficient way to compare 2 images? - by bplus - 11-20-2022, 12:14 AM
RE: efficient way to compare 2 images? - by bplus - 11-20-2022, 12:36 AM
RE: efficient way to compare 2 images? - by bplus - 11-20-2022, 02:37 AM



Users browsing this thread: 19 Guest(s)