Click here to Skip to main content
15,881,172 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm working on Visual Inspection System. One of my key function is to compare an image captured with an image from a data base. The comparison would reveal missing parts or damaged part. I have tried using pixel comparison, but this method is not reliable as it needs exactly similar image captured every time. Is there a way to improve this function to be more versatile. In a way it has to detect the difference in image even if the image captured is slightly offset or rotated. Please guide me using VB.Net. Below is my current code.
Private Sub btnGo_Click(ByVal sender As System.Object, _
 ByVal e As System.EventArgs) Handles btnGo.Click
    Me.Cursor = Cursors.WaitCursor

    ' Load the images.
    Dim bm1 As Bitmap = Image.FromFile("C:\Users\pnasguna\Desktop\A56.jpg")
    Dim bm2 As Bitmap = Image.FromFile("C:\Users\pnasguna\Desktop\A54.jpg")

    ' Make a difference image.
    Dim wid As Integer = Math.Min(bm1.Width, bm2.Width)
    Dim hgt As Integer = Math.Min(bm1.Height, bm2.Height)
    Dim bm3 As New Bitmap(wid, hgt)

    ' Create the difference image.
    Dim are_identical As Boolean = True
            Dim eq_color As Color = Color.White
    Dim ne_color As Color = Color.Red
    For x As Integer = 0 To wid - 1
        For y As Integer = 0 To hgt - 1
            If bm1.GetPixel(x, y).Equals(bm2.GetPixel(x, _
                y)) Then
                bm3.SetPixel(x, y, eq_color)
                bm3.SetPixel(x, y, ne_color)
                are_identical = False
            End If
        Next y
    Next x

    ' Display the result.
    PictureBox1.Image = bm3

    Me.Cursor = Cursors.Default
    If (bm1.Width <> bm2.Width) OrElse (bm1.Height <> _
        bm2.Height) Then are_identical = False
    If are_identical Then
        MessageBox.Show("The images are identical")
        MessageBox.Show("The images are different")
    End If

End Sub

I don't know how to use neither key point matching nor histogram method.

1 solution

Take the XnaFan ImageComparison library to inspectionate the sourcecode as an example for your needs

It reveals the difference between pixels and can compare images with a Similarity coefficient, Here is a basic example of both:

Imports XnaFan.ImageComparison

' ===================================================
' Get percentage difference value between two images:
' ===================================================

Dim img1 As Image = Image.FromFile("C:\Image1.jpg")
Dim img2 As Image = Image.FromFile("C:\Image2.jpg")

Dim PercentageDifference As Single =
ImageTool.PercentageDifference(img1:=img1, img2:=img2, threshold:=3)

MessageBox.Show(String.Format("Percentage Difference: {0}%",
CSng(PercentageDifference * 100I).ToString("n1")))

' ========================================
' Get difference image between two images:
' ========================================

Dim img1 As Image = Image.FromFile("C:\Image1.jpg")
Dim img2 As Image = Image.FromFile("C:\Image2.jpg")

Dim DifferenceBitmap As Bitmap =

PictureBox1.Image = DifferenceBitmap

If you want something more complex you could use AForge (Imaging) library to do the similarity comparison
This solution is obtained from :[^]
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900