-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathCannyStill.vb
More file actions
111 lines (88 loc) · 6 KB
/
CannyStill.vb
File metadata and controls
111 lines (88 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
'CannyStill.vb
'
'put this code in your main form, for example frmMain.vb
'
'add the following components to your form:
'
'btnOpenFile (Button)
'lblChosenFile (Label)
'ibOriginal (Emgu ImageBox)
'ibCanny (Emgu ImageBox)
'ofdOpenFile (OpenFileDialog)
'
'NOTE: Do NOT copy/paste the entire text of this file into Visual Studio !! It will not work if you do !!
'Follow the video on my YouTube channel to create the project and have Visual Studio write part of the code for you,
'then copy/pase the remaining text as needed
'
'in this example we are using code to resize components when the form is resized,
'if you used the layout managers you do not need to use the component resizing code
Option Explicit On 'require explicit declaration of variables, this is NOT Python !!
Option Strict On 'restrict implicit data type conversions to only widening conversions
Imports Emgu.CV '
Imports Emgu.CV.CvEnum 'usual Emgu Cv imports
Imports Emgu.CV.Structure '
Imports Emgu.CV.UI '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Class frmMain
' member variables ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim imgOriginal As Image(Of Bgr, Byte) 'input image
Dim imgGrayscale As Image(Of Gray, Byte) 'grayscale of input image
Dim imgBlurred As Image(Of Gray, Byte) 'intermediate blured image
Dim imgCanny As Image(Of Gray, Byte) 'Canny edge image
Dim blnFirstTimeInResizeEvent As Boolean = True 'see comment in frmMain_Resize for purpose of this variable
Dim intButtonAndLabelHorizPadding As Integer '
Dim intImageBoxesHorizPadding As Integer 'original component padding for component resizing
Dim intImageBoxesVertPadding As Integer '
' constructor '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Sub New()
InitializeComponent() 'this call is required by the designer
intButtonAndLabelHorizPadding = Me.Width - btnOpenFile.Width - lblChosenFile.Width '
intImageBoxesHorizPadding = Me.Width - ibOriginal.Width - ibCanny.Width 'get original padding for component resizing later
intImageBoxesVertPadding = Me.Height - btnOpenFile.Height - ibOriginal.Height '
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub frmMain_Resize( sender As Object, e As EventArgs) Handles MyBase.Resize
'This If Else statement is necessary to throw out the first time the Form1_Resize event is called.
'For some reason, in VB.NET the Resize event is called once before the constructor, then the constructor is called,
'then the Resize event is called each time the form is resized. The first time the Resize event is called
'(i.e. before the constructor is called) the coordinates of the components on the form all read zero,
'therefore we have to throw out this first call, then the constructor will run and get the correct initial
'component location data, then every time after that we can let the Resize event run as expected
If (blnFirstTimeInResizeEvent = True) Then
blnFirstTimeInResizeEvent = False
Else
lblChosenFile.Width = Me.Width - btnOpenFile.Width - intButtonAndLabelHorizPadding 'resize label
ibOriginal.Width = CInt((Me.Width - intImageBoxesHorizPadding) / 2) 'resize image box widths
ibCanny.Width = ibOriginal.Width '
ibCanny.Left = ibOriginal.Width + CInt(intImageBoxesHorizPadding * (1 / 3)) 'update x position for Canny image box
ibOriginal.Height = Me.Height - btnOpenFile.Height - intImageBoxesVertPadding 'resize image box heights
ibCanny.Height = ibOriginal.Height '
End If
End Sub
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Private Sub btnOpenFile_Click( sender As Object, e As EventArgs) Handles btnOpenFile.Click
Dim drChosenFile As DialogResult
drChosenFile = ofdOpenFile.ShowDialog() 'open file dialog
If (drChosenFile <> Windows.Forms.DialogResult.OK Or ofdOpenFile.FileName = "") Then 'if user chose Cancel or filename is blank . . .
lblChosenFile.Text = "file not chosen" 'show error message on label
Return 'and exit function
End If
Try
imgOriginal = New Image(Of Bgr, Byte)(ofdOpenFile.FileName) 'open image
Catch ex As Exception 'if error occurred
lblChosenFile.Text = "unable to open image, error: " + ex.Message 'show error message on label
Return 'and exit function
End Try
If imgOriginal Is Nothing Then 'if image could not be opened
lblChosenFile.Text = "unable to open image" 'show error message on label
Return 'and exit function
End If
imgGrayscale = imgOriginal.Convert(Of Gray, Byte)() 'convert to grayscale
imgBlurred = imgGrayscale.SmoothGaussian(5) 'blur
Dim dblCannyThresh As Double = 180.0 'declare params for call to Canny
Dim dblCannyThreshLinking As Double = 120.0 '
imgCanny = imgBlurred.Canny(dblCannyThresh, dblCannyThreshLinking) 'get Canny edges
ibOriginal.Image = imgOriginal 'update image boxes
ibCanny.Image = imgCanny '
End Sub
End Class