Results 1 to 9 of 9

Thread: comparing images [ got the code need explination]

  1. #1

    Thread Starter
    Hyperactive Member demon.KILER's Avatar
    Join Date
    Jul 2006
    Location
    I cannot remember !?
    Posts
    408

    Question comparing images [ got the code need explination]

    hi every one

    my question is same as my topic I want to compare two images and deside which one are same .

    I got no clue where to start

    thx for future help
    Last edited by demon.KILER; Oct 28th, 2006 at 01:15 PM.
    VS 2005 .....Framework SDK 3.0

    "Its mostly the brave one who choose to not fight"

  2. #2

    Thread Starter
    Hyperactive Member demon.KILER's Avatar
    Join Date
    Jul 2006
    Location
    I cannot remember !?
    Posts
    408

    Re: comparing images

    Code:
    Private Sub btnGo_Click(ByVal sender As System.Object, _
        ByVal e As System.EventArgs) Handles btnGo.Click
        Me.Cursor = Cursors.WaitCursor
        Application.DoEvents()
    
        ' Get the threshold.
        Dim threshold As Integer = _
            Integer.Parse(txtThreshold.Text)
    
        ' Load the images.
        Dim bm1 As Bitmap = Image.FromFile(txtFile1.Text)
        Dim bm2 As Bitmap = Image.FromFile(txtFile2.Text)
    
        ' 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 r1, g1, b1, r2, g2, b2, r3, g3, b3 As Integer
        Dim color1, color2 As Color
        Dim eq_color As Color = Color.White
        Dim ne_color As Color = Color.Red
        Dim dr, dg, db, diff As Integer
        For x As Integer = 0 To wid - 1
            For y As Integer = 0 To hgt - 1
                color1 = bm1.GetPixel(x, y)
                color2 = bm2.GetPixel(x, y)
                dr = CInt(color1.R) - color2.R
                dg = CInt(color1.G) - color2.G
                db = CInt(color1.B) - color2.B
                diff = dr * dr + dg * dg + db * db
                If diff <= threshold Then
                    bm3.SetPixel(x, y, eq_color)
                Else
                    bm3.SetPixel(x, y, ne_color)
                    are_identical = False
                End If
            Next y
        Next x
    
        ' Display the result.
        picResult.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")
        Else
            MessageBox.Show("The images are different")
        End If
    
        bm1.Dispose()
        bm2.Dispose()
    End Sub
    http://www.vb-helper.com/howto_net_i...threshold.html

    I found this code and I need some explination

    thx for the help
    Last edited by demon.KILER; Oct 28th, 2006 at 01:21 PM.
    VS 2005 .....Framework SDK 3.0

    "Its mostly the brave one who choose to not fight"

  3. #3
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: comparing images [ got the code need explination]

    It compares the difference of the color of the pixels. If it falls under some threshold, the pixels are considered similar, if it doesn't the pixels are considered not similar. It also outputs an image of the same size that gives you a visual indicator of where the difference lies.

    If you are trying to determine if they are identical, you don't need to do this much work. You could check the sizes of the files and also compute an MD5 hash of the file.

  4. #4

    Thread Starter
    Hyperactive Member demon.KILER's Avatar
    Join Date
    Jul 2006
    Location
    I cannot remember !?
    Posts
    408

    Re: comparing images [ got the code need explination]

    I got every thing u said execpt the this part

    can u pls expalin this part again

    also compute an MD5 hash of the file.
    thx for ut help
    VS 2005 .....Framework SDK 3.0

    "Its mostly the brave one who choose to not fight"

  5. #5

    Thread Starter
    Hyperactive Member demon.KILER's Avatar
    Join Date
    Jul 2006
    Location
    I cannot remember !?
    Posts
    408

    Re: comparing images [ got the code need explination]

    is this like this ??


    Code:
    Imports System.Text
    Imports System.Security.Cryptography
    
    Private Function GenerateHash(ByVal SourceText As String) As String
    'Create an encoding object to ensure the encoding standard for the source text
    Dim Ue As New UnicodeEncoding()
    'Retrieve a byte array based on the source text
    Dim ByteSourceText() As Byte = Ue.GetBytes(SourceTStext)
    'Instantiate an MD5 Provider object
    Dim Md5 As New MD5CryptoServiceProvider()
    'Compute the hash value from the source
    Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)
    'And convert it to String format for return
    Return Convert.ToBase64String(ByteHash)
    End Function
    source :
    http://www.a1vbcode.com/vbtip-149.asp


    how do i do that with image
    Last edited by demon.KILER; Oct 29th, 2006 at 10:05 AM.
    VS 2005 .....Framework SDK 3.0

    "Its mostly the brave one who choose to not fight"

  6. #6
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: comparing images [ got the code need explination]

    You can get the bytes of the image and pass it to the MD5.ComputeHash function.

    VB Code:
    1. MessageBox.Show(Convert.ToBase64String(Md5.ComputeHash(New System.IO.StreamReader("C:\Temp\2006_01_07\IMG_2809.JPG").BaseStream)))

    Now if the MD5 hashes are identical, it does not guarantee that the files are exactly the same, however, it is a pretty good indicator, especially if the files are the same size.

  7. #7

    Thread Starter
    Hyperactive Member demon.KILER's Avatar
    Join Date
    Jul 2006
    Location
    I cannot remember !?
    Posts
    408

    Re: comparing images [ got the code need explination]

    Is it posible for md5 hash to show same string even thow it not same
    VS 2005 .....Framework SDK 3.0

    "Its mostly the brave one who choose to not fight"

  8. #8
    PowerPoster 2.0 Negative0's Avatar
    Join Date
    Jun 2000
    Location
    Southeastern MI
    Posts
    4,367

    Re: comparing images [ got the code need explination]

    Technically, yes. However, very similar files (i.e. off by only a few bytes) will have drastically different MD5 hashes.

    The only way to be 100% sure that it is the exact same file, would be comparing every single byte in the files.

  9. #9

    Thread Starter
    Hyperactive Member demon.KILER's Avatar
    Join Date
    Jul 2006
    Location
    I cannot remember !?
    Posts
    408

    Re: comparing images [ got the code need explination]

    Okay Thx I Will Try That Code And Ask If Any Help Is Needed
    VS 2005 .....Framework SDK 3.0

    "Its mostly the brave one who choose to not fight"

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width