Can this bitmap compare be speed-tweaked?
Posted on 2006-03-23
Hey, I have to compare a series of bitmaps to portions of a larger, stored bitmap to see if they match, and I've got code that works, but it would be better if it worked faster. My code is below... I've tried a couple of things but right now that's best I can do. Has anyone done this a different way that might be faster (I'll benchmark it)?
One weird thing is that my bitmaps seem to be not identical matches, what seems to work is if I give it a threshold of +-6 on the R, G, and B compare... thus the strange compare.
Private Function ComparePixels(ByVal ox As Integer, ByVal oy As Integer, ByVal CompareTo As Bitmap) As Integer
' simple: got a match, add one; mismatch, subtract two
Dim c1 As Color, c2 As Color, result As Integer = 0
For x As Integer = 0 To CompareTo.Width - 1
For y As Integer = 0 To CompareTo.Height - 1
c1 = m_bmp.GetPixel(ox + x, oy + y)
c2 = CompareTo.GetPixel(x, y)
if CompareColor(c1, c2) then
result += 1
result -= 2
ComparePixels = result
Private Function CompareColor(ByVal c1 As Color, ByVal c2 As Color) As Boolean
Const T As Integer = 6
If c1.R > c2.R - T Then
If c1.R < c2.R + T Then
If c1.G > c2.G - T Then
If c1.G < c2.G + T Then
If c1.B > c2.B - T Then
If c1.B < c2.B + T Then