Public Class frmPatternDetection
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
'This call is required by the Windows Form Designer.
InitializeComponent()
'Add any initialization after the InitializeComponent() call
End Sub
'Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
'Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
'NOTE: The following procedure is required by the Windows Form Designer
'It can be modified using the Windows Form Designer.
'Do not modify it using the code editor.
Friend WithEvents TabControl1 As System.Windows.Forms.TabControl
Friend WithEvents TabPage1 As System.Windows.Forms.TabPage
Friend WithEvents TabPage2 As System.Windows.Forms.TabPage
Friend WithEvents picOutput As System.Windows.Forms.PictureBox
Friend WithEvents picInput As System.Windows.Forms.PictureBox
Friend WithEvents cmdLoadInput As System.Windows.Forms.Button
Friend WithEvents cmdLoadOutput As System.Windows.Forms.Button
Friend WithEvents lblInput As System.Windows.Forms.Label
Friend WithEvents Label1 As System.Windows.Forms.Label
Friend WithEvents cmdAdd As System.Windows.Forms.Button
Friend WithEvents cmdClear As System.Windows.Forms.Button
Friend WithEvents cmdTrain As System.Windows.Forms.Button
Friend WithEvents Label2 As System.Windows.Forms.Label
Friend WithEvents Label3 As System.Windows.Forms.Label
Friend WithEvents cmdLoadRunInput As System.Windows.Forms.Button
Friend WithEvents picRunOutput As System.Windows.Forms.PictureBox
Friend WithEvents picRunInput As System.Windows.Forms.PictureBox
Friend WithEvents cmdGenerate As System.Windows.Forms.Button
Friend WithEvents cmdClose As System.Windows.Forms.Button
Friend WithEvents pgMain As HandWriting.PatternGrid
Friend WithEvents cmdRestart As System.Windows.Forms.Button
Friend WithEvents picBoxMain As System.Windows.Forms.PictureBox
Friend WithEvents pgBar As System.Windows.Forms.ProgressBar
Friend WithEvents tabReport As System.Windows.Forms.TabPage
Friend WithEvents lvMain As System.Windows.Forms.ListView
Friend WithEvents picInputLarge As System.Windows.Forms.PictureBox
Friend WithEvents picOutputLarge As System.Windows.Forms.PictureBox
Friend WithEvents picRunOutputLarge As System.Windows.Forms.PictureBox
Friend WithEvents picRunInputLarge As System.Windows.Forms.PictureBox
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
Me.TabControl1 = New System.Windows.Forms.TabControl()
Me.TabPage1 = New System.Windows.Forms.TabPage()
Me.picOutputLarge = New System.Windows.Forms.PictureBox()
Me.picInputLarge = New System.Windows.Forms.PictureBox()
Me.pgBar = New System.Windows.Forms.ProgressBar()
Me.cmdTrain = New System.Windows.Forms.Button()
Me.cmdClear = New System.Windows.Forms.Button()
Me.cmdAdd = New System.Windows.Forms.Button()
Me.Label1 = New System.Windows.Forms.Label()
Me.lblInput = New System.Windows.Forms.Label()
Me.cmdLoadOutput = New System.Windows.Forms.Button()
Me.cmdLoadInput = New System.Windows.Forms.Button()
Me.picOutput = New System.Windows.Forms.PictureBox()
Me.picInput = New System.Windows.Forms.PictureBox()
Me.TabPage2 = New System.Windows.Forms.TabPage()
Me.picRunOutputLarge = New System.Windows.Forms.PictureBox()
Me.picRunInputLarge = New System.Windows.Forms.PictureBox()
Me.cmdGenerate = New System.Windows.Forms.Button()
Me.Label2 = New System.Windows.Forms.Label()
Me.Label3 = New System.Windows.Forms.Label()
Me.cmdLoadRunInput = New System.Windows.Forms.Button()
Me.picRunOutput = New System.Windows.Forms.PictureBox()
Me.picRunInput = New System.Windows.Forms.PictureBox()
Me.tabReport = New System.Windows.Forms.TabPage()
Me.lvMain = New System.Windows.Forms.ListView()
Me.picBoxMain = New System.Windows.Forms.PictureBox()
Me.pgMain = New HandWriting.PatternGrid()
Me.cmdClose = New System.Windows.Forms.Button()
Me.cmdRestart = New System.Windows.Forms.Button()
Me.TabControl1.SuspendLayout()
Me.TabPage1.SuspendLayout()
Me.TabPage2.SuspendLayout()
Me.tabReport.SuspendLayout()
Me.SuspendLayout()
'
'TabControl1
'
Me.TabControl1.Controls.AddRange(New System.Windows.Forms.Control() {Me.TabPage1, Me.TabPage2, Me.tabReport})
Me.TabControl1.Location = New System.Drawing.Point(8, 8)
Me.TabControl1.Name = "TabControl1"
Me.TabControl1.SelectedIndex = 0
Me.TabControl1.Size = New System.Drawing.Size(440, 336)
Me.TabControl1.TabIndex = 0
'
'TabPage1
'
Me.TabPage1.Controls.AddRange(New System.Windows.Forms.Control() {Me.picOutputLarge, Me.picInputLarge, Me.pgBar, Me.cmdTrain, Me.cmdClear, Me.cmdAdd, Me.Label1, Me.lblInput, Me.cmdLoadOutput, Me.cmdLoadInput, Me.picOutput, Me.picInput})
Me.TabPage1.Location = New System.Drawing.Point(4, 22)
Me.TabPage1.Name = "TabPage1"
Me.TabPage1.Size = New System.Drawing.Size(432, 310)
Me.TabPage1.TabIndex = 0
Me.TabPage1.Text = "Training"
'
'picOutputLarge
'
Me.picOutputLarge.BackColor = System.Drawing.Color.White
Me.picOutputLarge.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.picOutputLarge.Location = New System.Drawing.Point(256, 64)
Me.picOutputLarge.Name = "picOutputLarge"
Me.picOutputLarge.Size = New System.Drawing.Size(160, 160)
Me.picOutputLarge.TabIndex = 14
Me.picOutputLarge.TabStop = False
'
'picInputLarge
'
Me.picInputLarge.BackColor = System.Drawing.Color.White
Me.picInputLarge.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.picInputLarge.Location = New System.Drawing.Point(16, 64)
Me.picInputLarge.Name = "picInputLarge"
Me.picInputLarge.Size = New System.Drawing.Size(160, 160)
Me.picInputLarge.TabIndex = 13
Me.picInputLarge.TabStop = False
'
'pgBar
'
Me.pgBar.Location = New System.Drawing.Point(24, 280)
Me.pgBar.Name = "pgBar"
Me.pgBar.Size = New System.Drawing.Size(384, 16)
Me.pgBar.TabIndex = 12
'
'cmdTrain
'
Me.cmdTrain.Location = New System.Drawing.Point(288, 240)
Me.cmdTrain.Name = "cmdTrain"
Me.cmdTrain.Size = New System.Drawing.Size(120, 24)
Me.cmdTrain.TabIndex = 10
Me.cmdTrain.Text = "Train 100 Times"
'
'cmdClear
'
Me.cmdClear.Location = New System.Drawing.Point(160, 240)
Me.cmdClear.Name = "cmdClear"
Me.cmdClear.Size = New System.Drawing.Size(120, 24)
Me.cmdClear.TabIndex = 9
Me.cmdClear.Text = "Clear Training Set"
'
'cmdAdd
'
Me.cmdAdd.Location = New System.Drawing.Point(24, 240)
Me.cmdAdd.Name = "cmdAdd"
Me.cmdAdd.Size = New System.Drawing.Size(120, 24)
Me.cmdAdd.TabIndex = 8
Me.cmdAdd.Text = "Add To Training Set"
'
'Label1
'
Me.Label1.Location = New System.Drawing.Point(256, 8)
Me.Label1.Name = "Label1"
Me.Label1.Size = New System.Drawing.Size(104, 24)
Me.Label1.TabIndex = 7
Me.Label1.Text = "Target Output"
'
'lblInput
'
Me.lblInput.Location = New System.Drawing.Point(16, 8)
Me.lblInput.Name = "lblInput"
Me.lblInput.Size = New System.Drawing.Size(104, 24)
Me.lblInput.TabIndex = 6
Me.lblInput.Text = "Input Image"
'
'cmdLoadOutput
'
Me.cmdLoadOutput.Location = New System.Drawing.Point(296, 32)
Me.cmdLoadOutput.Name = "cmdLoadOutput"
Me.cmdLoadOutput.Size = New System.Drawing.Size(80, 24)
Me.cmdLoadOutput.TabIndex = 5
Me.cmdLoadOutput.Text = "Load Output"
'
'cmdLoadInput
'
Me.cmdLoadInput.Location = New System.Drawing.Point(48, 32)
Me.cmdLoadInput.Name = "cmdLoadInput"
Me.cmdLoadInput.Size = New System.Drawing.Size(80, 24)
Me.cmdLoadInput.TabIndex = 4
Me.cmdLoadInput.Text = "Load Input"
'
'picOutput
'
Me.picOutput.BackColor = System.Drawing.Color.White
Me.picOutput.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.picOutput.Location = New System.Drawing.Point(256, 32)
Me.picOutput.Name = "picOutput"
Me.picOutput.Size = New System.Drawing.Size(16, 16)
Me.picOutput.TabIndex = 3
Me.picOutput.TabStop = False
'
'picInput
'
Me.picInput.BackColor = System.Drawing.Color.White
Me.picInput.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.picInput.Location = New System.Drawing.Point(16, 32)
Me.picInput.Name = "picInput"
Me.picInput.Size = New System.Drawing.Size(16, 16)
Me.picInput.TabIndex = 2
Me.picInput.TabStop = False
'
'TabPage2
'
Me.TabPage2.Controls.AddRange(New System.Windows.Forms.Control() {Me.picRunOutputLarge, Me.picRunInputLarge, Me.cmdGenerate, Me.Label2, Me.Label3, Me.cmdLoadRunInput, Me.picRunOutput, Me.picRunInput})
Me.TabPage2.Location = New System.Drawing.Point(4, 22)
Me.TabPage2.Name = "TabPage2"
Me.TabPage2.Size = New System.Drawing.Size(432, 310)
Me.TabPage2.TabIndex = 1
Me.TabPage2.Text = "Running"
'
'picRunOutputLarge
'
Me.picRunOutputLarge.BackColor = System.Drawing.Color.White
Me.picRunOutputLarge.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.picRunOutputLarge.Location = New System.Drawing.Point(256, 64)
Me.picRunOutputLarge.Name = "picRunOutputLarge"
Me.picRunOutputLarge.Size = New System.Drawing.Size(160, 160)
Me.picRunOutputLarge.TabIndex = 16
Me.picRunOutputLarge.TabStop = False
'
'picRunInputLarge
'
Me.picRunInputLarge.BackColor = System.Drawing.Color.White
Me.picRunInputLarge.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.picRunInputLarge.Location = New System.Drawing.Point(16, 64)
Me.picRunInputLarge.Name = "picRunInputLarge"
Me.picRunInputLarge.Size = New System.Drawing.Size(160, 160)
Me.picRunInputLarge.TabIndex = 15
Me.picRunInputLarge.TabStop = False
'
'cmdGenerate
'
Me.cmdGenerate.Location = New System.Drawing.Point(296, 32)
Me.cmdGenerate.Name = "cmdGenerate"
Me.cmdGenerate.Size = New System.Drawing.Size(80, 24)
Me.cmdGenerate.TabIndex = 13
Me.cmdGenerate.Text = "Generate"
'
'Label2
'
Me.Label2.Location = New System.Drawing.Point(256, 8)
Me.Label2.Name = "Label2"
Me.Label2.Size = New System.Drawing.Size(104, 24)
Me.Label2.TabIndex = 12
Me.Label2.Text = "Generated Output"
'
'Label3
'
Me.Label3.Location = New System.Drawing.Point(16, 8)
Me.Label3.Name = "Label3"
Me.Label3.Size = New System.Drawing.Size(104, 24)
Me.Label3.TabIndex = 11
Me.Label3.Text = "Input"
'
'cmdLoadRunInput
'
Me.cmdLoadRunInput.Location = New System.Drawing.Point(48, 32)
Me.cmdLoadRunInput.Name = "cmdLoadRunInput"
Me.cmdLoadRunInput.Size = New System.Drawing.Size(80, 24)
Me.cmdLoadRunInput.TabIndex = 10
Me.cmdLoadRunInput.Text = "Load Input"
'
'picRunOutput
'
Me.picRunOutput.BackColor = System.Drawing.Color.White
Me.picRunOutput.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.picRunOutput.Location = New System.Drawing.Point(256, 32)
Me.picRunOutput.Name = "picRunOutput"
Me.picRunOutput.Size = New System.Drawing.Size(16, 16)
Me.picRunOutput.TabIndex = 9
Me.picRunOutput.TabStop = False
'
'picRunInput
'
Me.picRunInput.BackColor = System.Drawing.Color.White
Me.picRunInput.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle
Me.picRunInput.Location = New System.Drawing.Point(16, 32)
Me.picRunInput.Name = "picRunInput"
Me.picRunInput.Size = New System.Drawing.Size(16, 16)
Me.picRunInput.TabIndex = 8
Me.picRunInput.TabStop = False
'
'tabReport
'
Me.tabReport.Controls.AddRange(New System.Windows.Forms.Control() {Me.lvMain})
Me.tabReport.Location = New System.Drawing.Point(4, 22)
Me.tabReport.Name = "tabReport"
Me.tabReport.Size = New System.Drawing.Size(432, 310)
Me.tabReport.TabIndex = 2
Me.tabReport.Text = "Report"
'
'lvMain
'
Me.lvMain.Dock = System.Windows.Forms.DockStyle.Fill
Me.lvMain.Name = "lvMain"
Me.lvMain.Size = New System.Drawing.Size(432, 310)
Me.lvMain.TabIndex = 0
'
'picBoxMain
'
Me.picBoxMain.Location = New System.Drawing.Point(40, 352)
Me.picBoxMain.Name = "picBoxMain"
Me.picBoxMain.Size = New System.Drawing.Size(136, 136)
Me.picBoxMain.TabIndex = 11
Me.picBoxMain.TabStop = False
Me.picBoxMain.Visible = False
'
'pgMain
'
Me.pgMain.BackColor = System.Drawing.Color.White
Me.pgMain.Location = New System.Drawing.Point(128, 336)
Me.pgMain.Name = "pgMain"
Me.pgMain.Size = New System.Drawing.Size(104, 96)
Me.pgMain.TabIndex = 6
Me.pgMain.Visible = False
'
'cmdClose
'
Me.cmdClose.Location = New System.Drawing.Point(344, 352)
Me.cmdClose.Name = "cmdClose"
Me.cmdClose.Size = New System.Drawing.Size(104, 32)
Me.cmdClose.TabIndex = 5
Me.cmdClose.Text = "Close"
'
'cmdRestart
'
Me.cmdRestart.Location = New System.Drawing.Point(224, 352)
Me.cmdRestart.Name = "cmdRestart"
Me.cmdRestart.Size = New System.Drawing.Size(104, 32)
Me.cmdRestart.TabIndex = 6
Me.cmdRestart.Text = "Restart"
'
'frmPatternDetection
'
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.ClientSize = New System.Drawing.Size(456, 393)
Me.Controls.AddRange(New System.Windows.Forms.Control() {Me.cmdRestart, Me.cmdClose, Me.TabControl1, Me.picBoxMain, Me.pgMain})
Me.MaximizeBox = False
Me.Name = "frmPatternDetection"
Me.Text = "Image Analyser"
Me.TabControl1.ResumeLayout(False)
Me.TabPage1.ResumeLayout(False)
Me.TabPage2.ResumeLayout(False)
Me.tabReport.ResumeLayout(False)
Me.ResumeLayout(False)
End Sub
#End Region
Private Sub cmdClose_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdClose.Click
Me.Close()
End Sub
Private Sub cmdLoadInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoadInput.Click
Dim cd As New OpenFileDialog()
cd.ShowDialog()
Dim f As String = cd.FileName
LoadImage(f, picInput)
End Sub
Private Sub frmPatternDetection_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
pgMain.Initialize(New Size(1, 1), 16, 256)
End Sub
Private Sub cmdRestart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdRestart.Click
pgMain.Initialize(New Size(1, 1), 16, 256)
End Sub
Private Sub cmdLoadOutput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoadOutput.Click
Dim cd As New OpenFileDialog()
cd.ShowDialog()
Dim f As String = cd.FileName
LoadImage(f, picOutput)
End Sub
Private Sub cmdLoadRunInput_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdLoadRunInput.Click
Dim cd As New OpenFileDialog()
cd.ShowDialog()
Dim f As String = cd.FileName
LoadImage(f, picRunInput)
End Sub
Private Sub cmdAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdAdd.Click
'Read the value from input box
Dim td As New BrainNet.NeuralFramework.TrainingData()
GetValuesTo(picInput, td.Inputs)
GetValuesTo(picOutput, td.Outputs)
pgMain.AddStateFromData(td)
'Find the average rgb of a pixel and feed it
End Sub
' <summary>Find the average values of rgb, for each pixel and feed it to network </summary>
Private Sub GetValuesTo(ByVal pic As PictureBox, ByRef destination As ArrayList)
If pic.Image Is Nothing Then Exit Sub
Dim bmp As New Bitmap(pic.Image)
Dim hgt, wid As Long, clr As Color
hgt = pic.Height
wid = pic.Width
Dim i, j As Integer
Dim r, g, b As Integer
For i = 0 To hgt - 1
For j = 0 To wid - 1
clr = bmp.GetPixel(j, i)
'Make it a percent value
'avg = avg * 100 / 255
r = clr.R
g = clr.G
b = clr.B
Dim avg As Double = (r + g + b) / 3
If avg < 255 / 2 Then
destination.Add(0)
Else
destination.Add(1)
End If
Next j
Next i
End Sub
' <summary>Load a bitmap image to a picture box, after resizing it </summary>
Private Sub LoadImage(ByVal file As String, ByVal target As PictureBox)
If Trim(file) = "" Then Exit Sub
Dim bmap As New Bitmap(file)
Me.picBoxMain.Width = bmap.Width
Me.picBoxMain.Height = bmap.Height
picBoxMain.Image = bmap
DrawImage(picBoxMain, target, True)
ConvertToMono(target)
UpdateImages()
End Sub
Sub UpdateImages()
DrawImage(picInput, picInputLarge, False)
DrawImage(picOutput, picOutputLarge, False)
DrawImage(picRunInput, picRunInputLarge, False)
DrawImage(picRunOutput, picRunOutputLarge, False)
End Sub
' <summary> Convert an image to monochrome </summary>
Private Sub ConvertToMono(ByVal pic As PictureBox)
' Get the source Bitmap.
Dim bmp As New Bitmap(pic.Image)
Dim hgt, wid As Long
hgt = bmp.Height
wid = bmp.Width
Dim i, j, counter As Integer
counter = 0
Dim r, g, b As Integer
For i = 0 To hgt - 1
For j = 0 To wid - 1
r = bmp.GetPixel(i, j).R
g = bmp.GetPixel(i, j).G
b = bmp.GetPixel(i, j).B
Dim colorval As Integer = (r + g + b) / 3
Dim col As New Color()
If colorval > 255 / 2 Then
col = Color.White
Else
col = Color.Black
End If
bmp.SetPixel(j, i, col)
Next j
Next i
' Copy the image.
pic.Image = bmp
End Sub
' <summary> Shrinks an image in one picture box to another, using AntiAliasing </summary>
Private Sub DrawImage(ByVal source As PictureBox, ByVal _
target As PictureBox, Optional ByVal antialias As _
Boolean = False)
' Get the source Bitmap.
If source.Image Is Nothing Then Exit Sub
Dim from_bm As New Bitmap(source.Image)
' Make the destination Bitmap.
Dim wid As Integer = target.Width
Dim hgt As Integer = target.Height
Dim to_bm As New Bitmap(wid, hgt)
' Copy the image.
Dim gr As Graphics = Graphics.FromImage(to_bm)
If antialias Then gr.InterpolationMode = _
Drawing2D.InterpolationMode.HighQualityBilinear
gr.DrawImage(from_bm, 0, 0, wid - 1, hgt - 1)
' Display the result.
target.Image = to_bm
End Sub
' <summary> Shrinks an image in one picture box to another, using AntiAliasing </summary>
Private Sub DrawImage(ByVal bmap As Bitmap, ByVal _
target As PictureBox, Optional ByVal antialias As _
Boolean = False)
' Get the source Bitmap.
Dim from_bm As Bitmap = bmap
' Make the destination Bitmap.
Dim wid As Integer = target.Width
Dim hgt As Integer = target.Height
Dim to_bm As New Bitmap(wid, hgt)
' Copy the image.
Dim gr As Graphics = Graphics.FromImage(to_bm)
If antialias Then gr.InterpolationMode = _
Drawing2D.InterpolationMode.HighQualityBilinear
gr.DrawImage(from_bm, 0, 0, wid, hgt)
' Display the result.
target.Image = to_bm
End Sub
Private Sub cmdGenerate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdGenerate.Click
Dim bmp As New Bitmap(pgMain.GridSize.Width, pgMain.GridSize.Height)
Dim hgt, wid As Long
Dim input As New ArrayList(), output As New ArrayList()
GetValuesTo(picRunInput, input)
output = Me.pgMain.Detect(input)
hgt = bmp.Height
wid = bmp.Width
Dim i, j, counter As Integer
counter = 0
For i = 0 To hgt - 1
For j = 0 To wid - 1
Dim colorval As Integer = Math.Round(output(counter))
Dim col As New Color()
If colorval = 1 Then
col = Color.White
Else
col = Color.Black
End If
bmp.SetPixel(j, i, col)
counter = counter + 1
Next j
Next i
picRunOutput.Image = bmp
UpdateImages()
End Sub
Private Sub cmdTrain_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cmdTrain.Click
pgBar.Maximum = 100
Me.pgMain.Train(100)
pgMain.Report(Me.lvMain)
End Sub
Private Sub pgMain_TrainingProgress(ByVal Current As Long, ByVal Max As Long) Handles pgMain.TrainingProgress
Me.pgBar.Maximum = Max
Me.pgBar.Value = Current
End Sub
End Class