📄 frmpatterndetection.vb
字号:
'
'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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -