I'm working on image recognition which needs to be fast. From what I know the speed of using a memory stream is much faster than using .GetPixel for bitmaps
But when I use the memory stream I'm getting different results. I'm sure it's to do with how I'm creating and reading the stream. Can anyone assist with the following code:
But when I use the memory stream I'm getting different results. I'm sure it's to do with how I'm creating and reading the stream. Can anyone assist with the following code:
Code:
Private Sub ScanImageShape(ByRef bMap As Bitmap)
Dim lOffset As Long 'The length of the bmp header
Dim lPos As Long
Dim x As Integer
Dim y As Integer
'Get a memory stream into a byte array.
Dim imgByteArray() As Byte
Dim msBitmap As New IO.MemoryStream
Dim bRed As Byte
Dim bGreen As Byte
Dim bBlue As Byte
Dim cPixel As Color
'Put the bitmap into the memory stream
bmap.Save(msBitmap, ImageFormat.Bmp)
'Load memory stream into byte array
imgByteArray = msBitmap.GetBuffer
'This is the starting byte position of the bitmap data, after the header (???)
lOffset = 54
For y = 0 To bmap.Height - 1 '240
For x = 0 To bmap.Width - 1 ' 320
'Assume that there are 4 bytes per pixel
lPos = (y * 96) + (x * 4) + lOffset
cPixel = bmap.GetPixel(x, y)
bBlue = imgByteArray(lPos).ToString
bGreen = imgByteArray(lPos + 1).ToString
bRed = imgByteArray(lPos + 2).ToString
'Insert BYTE values into a multiline text box
txtDebugCoords.AppendText(lPos.ToString + ", " + _
x.ToString + ", " + _
y.ToString + ", " + _
bBlue.ToString + ", " + _
bGreen.ToString + ", " + _
bRed.ToString + Environment.NewLine)
'In another multiline text box compare the "getpixel" values. Should be the same.
txtDebugBMP.AppendText(bmap.GetPixel(x, y).B.ToString + ", " + _
bmap.GetPixel(x, y).G.ToString + ", " + _
bmap.GetPixel(x, y).R.ToString + Environment.NewLine)
Next
Next
msBitmap = Nothing
End Sub