图像处理之基础---很好的一个快速比较两副图片是否相同的code 可用于公安鉴别

转自Codeproject 

http://www.codeproject.com/dotnet/comparingimages.asp

    Public Enum CompareResult

        ciCompareOk

        ciPixelMismatch

        ciSizeMismatch

    End Enum

 

 

    Public Shared Function Compare(ByVal bmp1 As Bitmap, ByVal bmp2 As Bitmap) As CompareResult

 

        '首先检查两副图片大小是否完全相等

        If Not bmp1.Size.Equals(bmp2.Size) Then

            Return CompareResult.ciSizeMismatch

        Else

            '把每个图片转成一字节数组

            Dim ic As New System.Drawing.ImageConverter

            Dim btImage1(1) As Byte

            btImage1 = CType(ic.ConvertTo(bmp1, btImage1.GetType()), Byte())

            Dim btImage2(1) As Byte

            btImage2 = CType(ic.ConvertTo(bmp2, btImage2.GetType()), Byte())

            Debug.WriteLine(UBound(btImage1))

            '计算每个图片的hash值

            Dim shaM As New SHA256Managed

 

            Dim hash1 As Byte() = shaM.ComputeHash(btImage1)

            Dim hash2 As Byte() = shaM.ComputeHash(btImage2)

 

            '比较hash值

            Dim i As Integer

            For i = 0 To Math.Min(hash1.Length, hash2.Length) - 1

                If hash1(i) <> hash2(i) Then

                    Return CompareResult.ciPixelMismatch

                End If

            Next

        End If

        Return CompareResult.ciCompareOk

    End Function

 

http://blog.csdn.net/laviewpbt/article/details/754653

posted @ 2014-10-16 18:29  midu  阅读(398)  评论(0编辑  收藏  举报