Home
Search
 
What's New
Index
Books
Links
Q & A
Newsletter
Banners
 
Feedback
Tip Jar
 
C# Helper...
 
XML RSS Feed
Follow VBHelper on Twitter
 
 
 
MSDN Visual Basic Community
 
 
 
 
 
TitleFind the difference between two images
DescriptionThis example shows how to find the difference between two images in Visual Basic 6.
Keywordsimage, bitmap, compare, difference, imagediff
CategoriesGraphics
 
When you select two images to compare and click the Go button, the program executes the following code. It loads the two image files into hidden PictureBoxes. It finds the smaller of the Bitmaps' widths and heights (all measurements are in pixels), and gives the results PictureBox that size.

Next the program loops over the pixels in the smaller area. It gets the red, green, and blue components of the pixels in one image and subtracts them from the corresponding values in the other image. It divides the result by 2 and adds 128. This makes the result lie between 1 and 255, and pixels that are the same in both images become gray. When it has examined all of the pixels, the program displays the result image.

You can use this code to perform edge enhancement. Take an image, shift it one pixel to the right and down, and then use the program to compare the images. Areas with large changes in color (edges) are enhanced. Areas where the color is relatively stable are gray in the result.

 
Private Sub cmdGo_Click()
Dim wid As Integer
Dim hgt As Integer
Dim are_identical As Boolean
Dim r1 As Integer: Dim g1 As Integer: Dim b1 As Integer
Dim r2 As Integer: Dim g2 As Integer: Dim b2 As Integer
Dim r3 As Integer: Dim g3 As Integer: Dim b3 As Integer
Dim x As Integer
Dim y As Integer

    Screen.MousePointer = vbHourglass
    DoEvents

    ' Load the images.
    pic1.Picture = LoadPicture(txtFile1.Text)
    pic2.Picture = LoadPicture(txtFile2.Text)

    ' Size the difference image.
    wid = Min(pic1.ScaleWidth, pic2.ScaleWidth)
    hgt = Min(pic1.ScaleHeight, pic2.ScaleHeight)
    picResult.Width = wid + (picResult.Width - _
        picResult.ScaleWidth)
    picResult.Height = wid + (picResult.Height - _
        picResult.ScaleHeight)

    ' Create the difference image.
    are_identical = True
    For x = 0 To wid - 1
        For y = 0 To hgt - 1
            UnRGB pic1.Point(x, y), r1, g1, b1
            UnRGB pic2.Point(x, y), r2, g2, b2

            r3 = 128 + (r1 - r2) \ 2
            g3 = 128 + (g1 - g2) \ 2
            b3 = 128 + (b1 - b2) \ 2

            picResult.PSet (x, y), RGB(r3, g3, b3)

            If (r1 <> r2) Or (g1 <> g2) Or (b1 <> b2) Then _
                are_identical = False
        Next y
    Next x

    If (pic1.ScaleWidth <> pic2.ScaleWidth) Or _
        (pic1.ScaleHeight <> pic2.ScaleHeight) Then _
        are_identical = False
    If are_identical Then
        MsgBox "The images are identical"
    Else
        MsgBox "The images are different"
    End If

    Screen.MousePointer = vbDefault
End Sub
 
For more information on graphics programming in Visual Basic 6, see my acclaimed book Ready-to-Run Visual Basic Graphics Programming.
 
 
Copyright © 1997-2010 Rocky Mountain Computer Consulting, Inc.   All rights reserved.
  Updated