EDN Admin
Well-known member
Hello I went and made a sample for image recognition with 2 images 1 small image and 1 large image and supposed to give a success rate if the smaller image is found in the larger image. Unfortunately my brain is fried right now and cannot get it to
work when I condensed down the code to make up the sample. Can you tell me where the code is wrong?
(need 2 buttons and 4 picture boxes and 1 openfiledialog to work with the sample.)
below is the sample
<pre class="prettyprint lang-vb Imports System
Imports System.Drawing
Imports System.Drawing.Graphics
Imports System.Drawing.Imaging
Imports System.Runtime
Imports System.Runtime.InteropServices
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Threading
Imports System.Drawing.Bitmap
Public Class Form1
Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
Public Const MOUSEEVENTF_LEFTDOWN = &H2 left button down
Public Const MOUSEEVENTF_LEFTUP = &H4 left button up
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 middle button down
Public Const MOUSEEVENTF_MIDDLEUP = &H40 middle button up
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 right button down
Public Const MOUSEEVENTF_RIGHTUP = &H10 right button up
Public Bitmap Variables for Both Images Original and SceenCaputre(large)
Dim Original_Bitmap As Bitmap
Dim Large_Bitmap As Bitmap
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim Fild As New OpenFileDialog
Fild.Filter = ""
Fild.Multiselect = False
If Fild.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Original Image
Dim Img As Bitmap = Image.FromFile(Fild.FileName)
PictureBox1.BackgroundImage = (Img)
large image
Dim Bmg As Bitmap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(Bmg)
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size)
g.Dispose()
PictureBox2.BackgroundImage = Bmg
assigning Public Bitmaps
Original_Bitmap = Img
Large_Bitmap = Bmg
End If
Catch
End Try
End Sub
Private Sub FindImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindImage.Click
Try
Small Image
Dim Img As Bitmap = Original_Bitmap
Dim MatchCount As Integer = 0
Large Image
Dim Bmg As Bitmap = Large_Bitmap
to check to make sure both pictures are showing up
PictureBox3.BackgroundImage = Img
PictureBox4.BackgroundImage = Bmg
Thread.Sleep(2000)
original image is img array
Dim oRect As New Rectangle(0, 0, Bmg.Width, Bmg.Height)
Dim oBmpData As BitmapData = Bmg.LockBits(oRect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim oPtr As IntPtr = oBmpData.Scan0
Dim oPixels(99) As Integer
Dim oMaxPix As Integer = Bmg.Width + Bmg.Height
Marshal.Copy(oPtr, oPixels, 0, oMaxPix)
Dim smWidth As Integer
smWidth = Bmg.Width - 1
Large image array
Dim lRect As Rectangle = New Rectangle(0, 0, Img.Width, Img.Height)
Dim lBmpData As BitmapData = Img.LockBits(lRect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim lPtr As IntPtr = lBmpData.Scan0
Dim PixelCount As Integer = Img.Width * Img.Height
Dim lPixels(PixelCount - 1) As Integer
Marshal.Copy(lPtr, lPixels, 0, PixelCount)
Math variables
Dim MathScore As Integer
Dim MaxScore As Integer = bmg.Height
beginning of Marshal Loop
For i As Integer = 0 To lPixels.GetUpperBound(0)
If oPixels(0) = lPixels(i) Then
we have a match for top left pixel - so compare the other pixels
Dim PixelsToLeft As Integer = (i Mod Img.Width) - 1
Dim PixelsToRight As Integer = Img.Width - PixelsToLeft - smWidth
Dim d As Integer = PixelsToLeft + PixelsToRight
For y As Integer = 0 To 9
For x As Integer = 0 To 9
Dim oIndex As Integer = (y * 10) + x
Dim lIndex As Integer = (i + (y * (d + smWidth))) + x
If oPixels(oIndex) = lPixels(lIndex) Then
MathScore = MathScore + 1
This is where I cannot find the Match
End If
Next
Next
If MathScore >= Val(MaxScore / 2.5) Then
Dim percent As String
Dim myDec As Decimal
inttemp = (intData2 * 100) / intData1
myDec = Val((MathScore * 100) / MaxScore)
myDec = FormatNumber(myDec, 0)
percent = myDec & "%"
Label1.Text = "Match Score: " & percent
Label2.Text = "Math Score: " & MathScore & " out of " & MaxScore
Me.ToolStripStatusLabel2.Text = "Completed"
Me.Button1.Enabled = True
GoTo Foundit
End If
End If
Next
Img.UnlockBits(oBmpData)
bmg.UnlockBits(lBmpData)
Foundit:
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Catch
End Try
End Sub
End Class[/code]
<br/>
View the full article
work when I condensed down the code to make up the sample. Can you tell me where the code is wrong?
(need 2 buttons and 4 picture boxes and 1 openfiledialog to work with the sample.)
below is the sample
<pre class="prettyprint lang-vb Imports System
Imports System.Drawing
Imports System.Drawing.Graphics
Imports System.Drawing.Imaging
Imports System.Runtime
Imports System.Runtime.InteropServices
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms
Imports System.Threading
Imports System.Drawing.Bitmap
Public Class Form1
Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
Public Const MOUSEEVENTF_LEFTDOWN = &H2 left button down
Public Const MOUSEEVENTF_LEFTUP = &H4 left button up
Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 middle button down
Public Const MOUSEEVENTF_MIDDLEUP = &H40 middle button up
Public Const MOUSEEVENTF_RIGHTDOWN = &H8 right button down
Public Const MOUSEEVENTF_RIGHTUP = &H10 right button up
Public Bitmap Variables for Both Images Original and SceenCaputre(large)
Dim Original_Bitmap As Bitmap
Dim Large_Bitmap As Bitmap
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Try
Dim Fild As New OpenFileDialog
Fild.Filter = ""
Fild.Multiselect = False
If Fild.ShowDialog() = System.Windows.Forms.DialogResult.OK Then
Original Image
Dim Img As Bitmap = Image.FromFile(Fild.FileName)
PictureBox1.BackgroundImage = (Img)
large image
Dim Bmg As Bitmap = New Bitmap(Screen.PrimaryScreen.Bounds.Width, Screen.PrimaryScreen.Bounds.Height)
Dim g As Graphics = Graphics.FromImage(Bmg)
g.CopyFromScreen(0, 0, 0, 0, Screen.PrimaryScreen.Bounds.Size)
g.Dispose()
PictureBox2.BackgroundImage = Bmg
assigning Public Bitmaps
Original_Bitmap = Img
Large_Bitmap = Bmg
End If
Catch
End Try
End Sub
Private Sub FindImage_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FindImage.Click
Try
Small Image
Dim Img As Bitmap = Original_Bitmap
Dim MatchCount As Integer = 0
Large Image
Dim Bmg As Bitmap = Large_Bitmap
to check to make sure both pictures are showing up
PictureBox3.BackgroundImage = Img
PictureBox4.BackgroundImage = Bmg
Thread.Sleep(2000)
original image is img array
Dim oRect As New Rectangle(0, 0, Bmg.Width, Bmg.Height)
Dim oBmpData As BitmapData = Bmg.LockBits(oRect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim oPtr As IntPtr = oBmpData.Scan0
Dim oPixels(99) As Integer
Dim oMaxPix As Integer = Bmg.Width + Bmg.Height
Marshal.Copy(oPtr, oPixels, 0, oMaxPix)
Dim smWidth As Integer
smWidth = Bmg.Width - 1
Large image array
Dim lRect As Rectangle = New Rectangle(0, 0, Img.Width, Img.Height)
Dim lBmpData As BitmapData = Img.LockBits(lRect, ImageLockMode.ReadWrite, System.Drawing.Imaging.PixelFormat.Format24bppRgb)
Dim lPtr As IntPtr = lBmpData.Scan0
Dim PixelCount As Integer = Img.Width * Img.Height
Dim lPixels(PixelCount - 1) As Integer
Marshal.Copy(lPtr, lPixels, 0, PixelCount)
Math variables
Dim MathScore As Integer
Dim MaxScore As Integer = bmg.Height
beginning of Marshal Loop
For i As Integer = 0 To lPixels.GetUpperBound(0)
If oPixels(0) = lPixels(i) Then
we have a match for top left pixel - so compare the other pixels
Dim PixelsToLeft As Integer = (i Mod Img.Width) - 1
Dim PixelsToRight As Integer = Img.Width - PixelsToLeft - smWidth
Dim d As Integer = PixelsToLeft + PixelsToRight
For y As Integer = 0 To 9
For x As Integer = 0 To 9
Dim oIndex As Integer = (y * 10) + x
Dim lIndex As Integer = (i + (y * (d + smWidth))) + x
If oPixels(oIndex) = lPixels(lIndex) Then
MathScore = MathScore + 1
This is where I cannot find the Match
End If
Next
Next
If MathScore >= Val(MaxScore / 2.5) Then
Dim percent As String
Dim myDec As Decimal
inttemp = (intData2 * 100) / intData1
myDec = Val((MathScore * 100) / MaxScore)
myDec = FormatNumber(myDec, 0)
percent = myDec & "%"
Label1.Text = "Match Score: " & percent
Label2.Text = "Math Score: " & MathScore & " out of " & MaxScore
Me.ToolStripStatusLabel2.Text = "Completed"
Me.Button1.Enabled = True
GoTo Foundit
End If
End If
Next
Img.UnlockBits(oBmpData)
bmg.UnlockBits(lBmpData)
Foundit:
mouse_event(MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0)
mouse_event(MOUSEEVENTF_LEFTUP, 0, 0, 0, 0)
Catch
End Try
End Sub
End Class[/code]
<br/>
View the full article