Victor Image Processing Library How-to Tips

Compare Two Images, Looking for Changes

Comparison of images is easy with Victor. Determine the size of the images and allocate space for them in memory, load the files, and compare them.

This procedure supports grayscale and color images. In general, the steps are:

compare images compare images compare images
Reference image Test image Final image

Notice that the final image is black except where the test image differed from the reference.

Compare Two Images - the Visual Basic Source Code

Requires Victor Image Processing Library for 32-bit Windows v 5 or higher.
' Data type definitions ...............................................
' Image descriptor
Type imgdes
   ibuff As Long
   stx As Long
   sty As Long
   endx As Long
   endy As Long
   buffwidth As Long
   palette As Long
   colors As Long
   imgtype As Long
   bmh As Long
   hBitmap As Long
End Type
Type JpegData
    ftype As Long
    width As Long
    length As Long
    comps As Long
    precision As Long
    sampfac0 As Long
    sampfac1 As Long
    sampfac2 As Long
    sampfac3 As Long
    vbitcount As Long
End Type
' Function Declarations ........................................... 
Declare Function allocimage Lib "VIC32.DLL" (image As imgdes, ByVal wid As Long, ByVal leng As Long, ByVal BPPixel As Long) As Long
Declare Sub freeimage Lib "VIC32.DLL" (image As imgdes)
Declare Function jpeginfo Lib "VIC32.DLL" (ByVal filename As String, jdat As JpegData) As Long
Declare Function loadjpg Lib "VIC32.DLL" (ByVal filename As String, resimg As imgdes) As Long
Declare Function pixelcount Lib "VIC32.DLL" (ByVal min As Long, ByVal max As Long, redct As Long, grnct As Long, bluct As Long, srcimg As imgdes) As Long
Declare Function savejpg Lib "VIC32.DLL" (ByVal filename As String, srcimg As imgdes, ByVal quality As Long) As Long
' The Subroutine .................................................. 
Private Sub mnucompare2pics_Click()
   Dim refimage As imgdes
   Dim testimage As imgdes
   Dim finalimage As imgdes
   Dim ref_fname As String
   Dim test_fname As String
   Dim final_fname As String
   Dim ref_fileinfo As JpegData
   Dim test_fileinfo As JpegData
   Dim rcode1 As Long
   Dim rcode2 As Long
   Dim vbitcount As Long
   Dim ared As Long
   Dim agrn As Long
   Dim ablu As Long
   ref_fname = "refc.jpg"
   test_fname = "testc.jpg"
   final_fname = "finalc.jpg"
    rcode1 = jpeginfo(ref_fname, ref_fileinfo)    
    rcode2 = jpeginfo(test_fname, test_fileinfo)  
    If ((rcode1 = NO_ERROR) And (rcode2 = NO_ERROR)) Then
        rcode1 = allocimage(refimage, ref_fileinfo.width, ref_fileinfo.length, ref_fileinfo.vbitcount)
        rcode2 = allocimage(testimage, test_fileinfo.width, test_fileinfo.length, test_fileinfo.vbitcount)
        If ((rcode1 = NO_ERROR) And (rcode2 = NO_ERROR)) Then
            rcode1 = loadjpg(ref_fname, refimage)    ' Load the reference image 
            rcode2 = loadjpg(test_fname, testimage)  ' Load the test image      
            If ((rcode1 = NO_ERROR) And (rcode2 = NO_ERROR)) Then
            ' Allocate space to hold the final image      
                rcode1 = allocimage(finalimage, test_fileinfo.width, test_fileinfo.length, test_fileinfo.vbitcount)
                If (rcode1 = NO_ERROR) Then
            ' Compare the images       
                    rcode1 = xorimage(refimage, testimage, finalimage)        ' Turns all identical pixels to zero in the final image
                    rcode2 = pixelcount(1, 255, ared, agrn, ablu, finalimage) ' Count the pixels that are nonzero      
                    If (rcode1 = NO_ERROR) And (rcode2 = NO_ERROR) And (ared > 0 Or agrn > 0 Or ablu > 0) Then
                        MsgBox ("pictures are different, do something")
                    End If
                End If
           End If
        End If
            ' Save the final image       
        rcode1 = savejpg(final_fname, finalimage, 75)
            ' Free the image buffers    
        freeimage finalimage   
        freeimage testimage
        freeimage refimage
    End If
End Sub
Victor Image Processing Library

Victor Image Processing Library homepage | Victor Product Summary | more source code

Copyright © 1995 - 2002 Catenary Systems Inc. All rights reserved. Victor Image Processing Library is a trademark of Catenary Systems.