Hallo,
mit dieser Klasse ist es möglich, zwei Bilder extrem schnell zu vergleichen. Genutzt wird die Bitmap.LockBits-Methode, anschließend werden die RGB-Werte per Marshal.Copy in ein Bytearray kopiert. Dasselbe passiert mit der zweiten Bitmap und die Arrays werden verglichen.
(Source wurde am 22.12. überarbeitet und nutzt nun nicht mehr GetPixel -> beste Performance)
LG
mit dieser Klasse ist es möglich, zwei Bilder extrem schnell zu vergleichen. Genutzt wird die Bitmap.LockBits-Methode, anschließend werden die RGB-Werte per Marshal.Copy in ein Bytearray kopiert. Dasselbe passiert mit der zweiten Bitmap und die Arrays werden verglichen.
(Source wurde am 22.12. überarbeitet und nutzt nun nicht mehr GetPixel -> beste Performance)
VB.NET-Quellcode
- Public Class FastPixelCompare
- Public Structure FastPixelCompareResult
- Dim TotalValuesCompared As Int32
- Dim TotalDifference As Int32
- Dim DifferenceInPercent As Double
- End Structure
- Public Shared Function Compare(ByVal bmp1 As Bitmap, ByVal bmp2 As Bitmap, ByVal faktor As Integer) As FastPixelCompareResult
- Dim bmpdata1 As Imaging.BitmapData = bmp1.LockBits(New Rectangle(0, 0, bmp1.Width, bmp1.Height), Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format24bppRgb)
- Dim bmpdata2 As Imaging.BitmapData = bmp2.LockBits(New Rectangle(0, 0, bmp2.Width, bmp2.Height), Imaging.ImageLockMode.ReadOnly, Imaging.PixelFormat.Format24bppRgb)
- Dim p1 As IntPtr = bmpdata1.Scan0
- Dim p2 As IntPtr = bmpdata2.Scan0
- Dim buffer1(bmp1.Height * bmpdata1.Width) As Byte
- Dim buffer2(bmp2.Height * bmpdata2.Width) As Byte
- System.Runtime.InteropServices.Marshal.Copy(p1, buffer1, 0, buffer1.Length)
- System.Runtime.InteropServices.Marshal.Copy(p2, buffer2, 0, buffer2.Length)
- bmp1.UnlockBits(bmpdata1)
- bmp2.UnlockBits(bmpdata2)
- Dim result As New FastPixelCompareResult With {.TotalValuesCompared = buffer1.Length + buffer2.Length}
- Dim diff As Int32 = 0
- For i As Integer = 0 To If(buffer1.Length > buffer2.Length, buffer2.Length - 1, buffer1.Length - 1)
- diff += If(buffer1(i) > buffer2(i) - faktor And buffer1(i) < buffer2(i) + faktor, 0, 1)
- Next
- result.TotalDifference = diff
- Dim pxs As Integer= If(bmp1.Width > bmp2.Width, bmp2.Width, bmp1.Width) * If(bmp1.Height > bmp2.Height, bmp2.Height, bmp1.Height)
- result.DifferenceInPercent = (100 / pxs) * (diff / 3)
- Return result
- End Function
- End Class
LG
Dieser Beitrag wurde bereits 10 mal editiert, zuletzt von „kevin89“ ()