Jump to content

Compare Pictures


Jex
 Share

Recommended Posts

Recording all pixels colors in pictures one by one and comparing how many pixel colors match. Giving percentage of how many true pixel color match and true pixel color match count. ( Sorry for my bad script description but my English not enough for explain that :"> ) By the way im curious about how can i capture pixel colors in picture without open in gui, so that process will be more fast.

$Test = Compare ( First Picture, Second Picture, Picture Width, Picture Height, Search Step )

$Test[0] = Total searched pixels in picture

$Test[1] = Percentage of equal pixel color

$Test[2] = Equal pixel color count

$Test[3] = Not equal pixel color count

#include <GUIConstants.au3>
Opt("PixelCoordMode", 2)
Global $Data

For $i = 1 To 4
    $Test = Compare("Test1.jpg", "Test" & $i & ".jpg", 75, 75, 10)
    If $Data = "" Then
        $Data = "Test" & $i & ".jpg  " & $Test[1] & "% Total : " & $Test[0] & " Equal : " & $Test[2] & " Not Equal : " & $Test[3]
    Else
        $Data = $Data & @CRLF & "Test" & $i & ".jpg  " & $Test[1] & "% Total : " & $Test[0] & " Equal : " & $Test[2] & " Not Equal : " & $Test[3]
    EndIf
    If $Test[1] = 100 Then $Found = "Test" & $i & ".jpg"
Next

MsgBox("", "", $Data)
If $Found <> "" Then MsgBox("", "", $Found & " 100% match!")

Func Compare($Pic1, $Pic2, $Width, $Height, $Step = 1)
    Local $Color[$Width + 1][$Height + 1], $Color2[$Width + 1][$Height + 1], $Var = 0, $Var2 = 0
    $Compare = GUICreate("", $Width, $Height, -1, -1, $WS_POPUP, $WS_EX_TOPMOST)
    GUISetState(@SW_SHOW, $Compare)
    $Pic = GUICtrlCreatePic($Pic1, "", "", $Width, $Height)
    For $y = 1 To $Height Step $Step
        For $x = 1 To $Width
            $Color[$x][$y] = PixelGetColor($x, $y)
        Next
    Next
    GUICtrlSetImage($Pic, $Pic2)
    For $y = 1 To $Height Step $Step
        For $x = 1 To $Width
            $Color2[$x][$y] = PixelGetColor($x, $y)
            If $Color[$x][$y] = $Color2[$x][$y] Then
                $Var += 1
            Else
                $Var2 += 1
            EndIf
        Next
    Next
    $Need = $Var + $Var2
    $Result = ($Var / $Need) * 100
    GUIDelete($Compare)
    Dim $Return[4] = [$Need, Round($Result, 1), $Var, $Var2]
    Return $Return
EndFunc   ;==>Compare

Picture.rar

Edited by Jex
Link to comment
Share on other sites

Hey, I made this cos I really liked the idea. I'm planning on working more on it though.

#include <GUIConstants.au3>

Opt("PixelCoordMode", 2)

Global $Color=0xEF0000
Global $Pic
Global $U32
Global $GDI

GUICreate("", 313, 201)
$Path1 = GUICtrlCreateInput("Muestra2.bmp", 24, 48, 193, 21)
$Path2 = GUICtrlCreateInput("Muestra1.bmp", 24, 92, 193, 21)
$Browse1 = GUICtrlCreateButton("Browse", 226, 48, 61, 23, 0)
$Browse2 = GUICtrlCreateButton("Browse", 226, 92, 61, 23, 0)
$compare = GUICtrlCreateButton("&Compare", 95, 152, 127, 31, $BS_DEFPUSHBUTTON)
GUICtrlCreateGroup(" Paths ", 8, 14, 293, 123)
GUICtrlCreateGroup("", -99, -99, 1, 1)
GUISetState(@SW_SHOW)

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit
        Case $Browse1
            $newPath=FileOpenDialog("",@MyDocumentsDir,"(*.jpg;*bmp)") 
            if Not @error Then
                GUICtrlSetData($Path1,$newPath)
            EndIf
        Case $Browse2
            $newPath=FileOpenDialog("",@MyDocumentsDir,"(*.jpg;*bmp)") 
            if Not @error Then
                GUICtrlSetData($Path2,$newPath)
            EndIf
        Case $compare
            $Pic1Path=GUICtrlRead($Path1)
            $Pic2Path=GUICtrlRead($Path2)
            $U32=DllOpen("user32.dll")
            $GDI=DllOpen("GDI32.dll")
            $Test = Compare($Pic1Path,$Pic2Path, 200,200)
            DllClose($U32)
            DllClose($U32)
            MsgBox(4144,"",$Test)
    EndSwitch
WEnd

Func Compare($Pic1, $Pic2, $Width, $Height)
    Local $Color[$Width + 1][$Height + 1], $Color2[$Width + 1][$Height + 1], $Var = 0
    $Test = GUICreate("", $Width, $Height, -1, -1, $WS_POPUP,$WS_EX_TOPMOST)
    GUISetState()
    
    $Pic = GUICtrlCreatePic($Pic1, "", "", $Width, $Height)
    
    For $y = 1 To $Height
        For $x = 1 To $Width
            $Color[$x][$y] = PixelGetColor($x, $y)
        Next
    Next
    GUICtrlSetImage($Pic, $Pic2)
    For $y = 1 To $Height
        For $x = 1 To $Width
            $Color2[$x][$y] = PixelGetColor($x, $y)
            If $Color[$x][$y] = $Color2[$x][$y] Then 
                $Var += 1
            Else
                dibujar($x,$y)
            EndIf
        Next
    Next
    $Need = $Height * $Width
    $Result = ($Var / $Need) * 100
;~     GUIDelete($Test)
    Return Round($Result, 1) & "%  " & $Var
EndFunc   ;==>Compares

Func dibujar($nX,$nY)
    $hand = DllCall($U32,"int", "GetDC", "hwnd",GUICtrlGetHandle($Pic)); 0)
    $pen = DllCall($GDI, "int", "CreatePen", "int", 0 , "int",2, "int", $Color)
    DllCall($GDI, "int", "SelectObject", "int", $hand[0], "int", $pen[0])
    DllCall($GDI, "int", "MoveToEx", "hwnd", $hand[0], "int",$nX, "int", $nY, "int", 0)
    DllCall($GDI, "int", "LineTo", "hwnd", $hand[0], "int", $nX, "int", $nY)
EndFunc

You can browse to select two pictures you want to compare. When it finds a different pixel, it will spot it. The very annoying problem is that the pic gui needs to have focus always and it gets REALLY slow, obviously, when trying to compare two pics that are totally different.

Download these two images and place them in the same directory as the script to test it, you'll see clearly what it does.

http://nahueljose.googlepages.com/Muestra1.bmp

http://nahueljose.googlepages.com/Muestra2.bmp

Link to comment
Share on other sites

By the way im curious about how can i capture pixel colors in picture without open in gui, so that process will be more fast.

Yes, I've been wondering about that too.

And maybe add a parameter controlling the Step in the For..Next loop so the user has the option to skip every X pixel (for speed, of course).

Kurt

Awaiting Diablo III..

Link to comment
Share on other sites

Link to comment
Share on other sites

  • 3 years later...

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...