Sign in to follow this  
Followers 0
cxs

compare 2 jpg pictures

13 posts in this topic

Hi,

I'd like a simple function that can compare 2 jpg images and return a true or false.

something like

If all pixels in c:\1.jpg = all pixels in c:\2.jpg then

ToolTip("image 1 and 2 are the same", 0, 0, "Image Found", 1)

Else

ToolTip("image 1 and 2 are not the same", 0, 0, "Image Not Found", 1)

EndIf

I have spent several hours trying to find out how to do this and searched the forums etc, but I haven't been able to make anything that works.

Share this post


Link to post
Share on other sites



cxs,

Search harder - there are lot of examples out there - I found threads suggesting many different approaches on the first page of responses to ""compare+image"!

Hints: Look at PixelGetSum. Or you could use PixelGetColor and create 2 arrays. Or you could create suitable hashes to compare and never even have to display the .jpgs.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

Just came up with a rough idea.. u can use it.. just go for the concept.. change whatever u need to make it better..

$hwnd1=WinGetHandle("Image1.jpg","")
$hwnd2=WinGetHandle("Image2.jpg","")

For $x=0 to 5000
;WinGetHandle("Image1.jpg","")
    $image1=PixelGetColor($x,$x,$hwnd1)
    $image2=PixelGetColor($x,$x,$hwnd2)
    
    If @error Then ExitLoop
        
    If $image1<>$image2 Then
        mismatch()
        ExitLoop
    EndIf
    $count=$count+1
Next
if $count=$ImageDimension Then
    MsgBox(4096,"Image match??","Complete match.. success")
EndIf

Func mismatch()
    MsgBox(4096,"Image match??","Failed.. Mismatch")
EndFunc

[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com

Share this post


Link to post
Share on other sites

cxs,

Search harder - there are lot of examples out there - I found threads suggesting many different approaches on the first page of responses to ""compare+image"!

Hints: Look at PixelGetSum. Or you could use PixelGetColor and create 2 arrays. Or you could create suitable hashes to compare and never even have to display the .jpgs.

M23

the .jpgs are not meant to be displayed, the function need to check the actual jpg files.

I've looked at pixelchecksum, but haven't found a way to load and compare 2 images.

I've tried alot of different things but the actual solution have eluded me.

So I hoped someone could help me with this one.

I'll try searching again, but if someone know the solution to this one I'd appreciate it.

Share this post


Link to post
Share on other sites

the .jpgs are not meant to be displayed, the function need to check the actual jpg files.

Not very sure that u can do that.. U'll have to display the images.. as far as my knowledge goes..

I mean the image files shud be opened..


[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com

Share this post


Link to post
Share on other sites

cxs,

As you do want to display the .jpgs, a search for "compare+files", rather than "compare+image" would have thrown up this thread.

As I suggested earlier, it proposes creating hashes to compare.

M23


Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind._______My UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Share this post


Link to post
Share on other sites

$hwnd1=WinGetHandle("Image1.jpg","")
$hwnd2=WinGetHandle("Image2.jpg","")
$ycount=$xcount=0
For $x=0 to $imagewidth
    for $y=0 to $imageheight
;WinGetHandle("Image1.jpg","")
    $image1=PixelGetColor($x,$x,$hwnd1)
    $image2=PixelGetColor($x,$x,$hwnd2)
    
    If @error Then ExitLoop
        
    If $image1<>$image2 Then
        mismatch()
        ExitLoop
    EndIf
    $ycount=$ycount+1
Next
$xcount=$xcount+1
Next
if $xcount=$imagewidth And $ycount=$imageheight Then
    MsgBox(4096,"Image match??","Complete match.. success")
EndIf

Func mismatch()
    MsgBox(4096,"Image match??","Failed.. Mismatch")
EndFunc

As far as i see this should do the required job..


[font="Garamond"]Manjish Naik[/font]Engineer, Global Services - QPSHoneywell Automation India LimitedE-mail - Manjish.Naik@honeywell.com

Share this post


Link to post
Share on other sites

Thanks to the both of you for your help. I'll go through your example and search some more and come up with a solution.

Even if I didn't get what I needed I'm closing this topic.

thanks

Share this post


Link to post
Share on other sites

; Retarded code here

As far as i see this should do the required job..

Seriously? Why don't you test that "code" and see how it work out. I mean, it seem so logical to compare two images by using WinGetHandle().

Here's something that actually works, and not only that, also work even if the images are in different formats and have different tags etc.

#include <GDIPlus.au3>



_GDIPlus_Startup()
$fname1=FileOpenDialog("First image","","All images(*.bmp;*.jpg;*.png;)")
If $fname1="" Then Exit
$fname2=FileOpenDialog("Second image image","","All images(*.bmp;*.jpg;*.png;)")
If $fname2="" Then Exit
$bm1 = _GDIPlus_ImageLoadFromFile($fname1)
$bm2 = _GDIPlus_ImageLoadFromFile($fname2)

MsgBox(0, "bm1==bm2", CompareBitmaps($bm1, $bm2))
_GDIPlus_ImageDispose($bm1)
_GDIPlus_ImageDispose($bm2)
_GDIPlus_Shutdown()

Func CompareBitmaps($bm1, $bm2)
    
    $Bm1W = _GDIPlus_ImageGetWidth($bm1)
    $Bm1H = _GDIPlus_ImageGetHeight($bm1)
    $BitmapData1 = _GDIPlus_BitmapLockBits($bm1, 0, 0, $Bm1W, $Bm1H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    $Stride = DllStructGetData($BitmapData1, "Stride")
    $Scan0 = DllStructGetData($BitmapData1, "Scan0")
    
    $ptr1 = $Scan0
    $size1 = ($Bm1H - 1) * $Stride + ($Bm1W - 1) * 4
    
    
    $Bm2W = _GDIPlus_ImageGetWidth($bm2)
    $Bm2H = _GDIPlus_ImageGetHeight($bm2)
    $BitmapData2 = _GDIPlus_BitmapLockBits($bm2, 0, 0, $Bm2W, $Bm2H, $GDIP_ILMREAD, $GDIP_PXF32RGB)
    $Stride = DllStructGetData($BitmapData2, "Stride")
    $Scan0 = DllStructGetData($BitmapData2, "Scan0")
    
    $ptr2 = $Scan0
    $size2 = ($Bm2H - 1) * $Stride + ($Bm2W - 1) * 4
    
    $smallest = $size1
    If $size2 < $smallest Then $smallest = $size2
    $call = DllCall("msvcrt.dll", "int:cdecl", "memcmp", "ptr", $ptr1, "ptr", $ptr2, "int", $smallest)


    
    _GDIPlus_BitmapUnlockBits($bm1, $BitmapData1)
    _GDIPlus_BitmapUnlockBits($bm2, $BitmapData2)
    
    Return ($call[0]=0)
    
    
EndFunc  ;==>CompareBitmaps

:)

1 person likes this

Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

quote name='monoceres' date='Feb 15 2009, 09:56 PM' post='644671'

Thanks alot Monoceres :)

Share this post


Link to post
Share on other sites

#11 ·  Posted (edited)

Hi Monoceres, I am working on to compare some webcam images with minor differences due to noise. May i know how this code is doing the compare? You mentioned that it works on images with different formats. Is there a tolerance in the comparison?

Edited by VAG

Share this post


Link to post
Share on other sites

My code is just doing a byte for byte comparison of the image data using the visual c runtime function memcmp. So the images have the exactly match. If you wish to compare the data yourself you will have to loop through the data. Search the forums for _GDIPlus_BitmapLockBits for examples.


Broken link? PM me and I'll send you the file!

Share this post


Link to post
Share on other sites

#13 ·  Posted (edited)

I successfuly used Prospeed and its CompareBytes() function for comparing images also with tolerance parameter. It returns number of different bytes.

concept:

LoadFileImage()

InitFX()

CompareBytes()

Search Prospeed in Examples forum.

BTW: very nice function CompareBitmaps() monoceres!!

Edited by Zedna

Share this post


Link to post
Share on other sites

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
Sign in to follow this  
Followers 0