-
Notifications
You must be signed in to change notification settings - Fork 151
/
CannyStill.vb
71 lines (56 loc) · 3.02 KB
/
CannyStill.vb
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
'CannyStill.vb
'
'Emgu CV 3.0.0
'
'put this code in your main form, for example frmMain.vb
'
'add the following components to your form:
'
'tlpOuter (TableLayoutPanel)
'tlpInner (TableLayoutPanel)
'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
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 'usual Emgu Cv imports
Imports Emgu.CV.CvEnum '
Imports Emgu.CV.Structure '
Imports Emgu.CV.UI '
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Public Class frmMain
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
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 <> 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
Dim imgOriginal As Mat
Try
imgOriginal = New Mat(ofdOpenFile.FileName, LoadImageType.Color)
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
Dim imgGrayscale As New Mat(imgOriginal.Size, DepthType.Cv8U, 1)
Dim imgBlurred As New Mat(imgOriginal.Size, DepthType.Cv8U, 1)
Dim imgCanny As New Mat(imgOriginal.Size, DepthType.Cv8U, 1)
CvInvoke.CvtColor(imgOriginal, imgGrayscale, ColorConversion.Bgr2Gray)
CvInvoke.GaussianBlur(imgGrayscale, imgBlurred, New Size(5, 5), 1.5)
CvInvoke.Canny(imgBlurred, imgCanny, 100, 200)
ibOriginal.Image = imgOriginal 'update image boxes
ibCanny.Image = imgCanny '
End Sub
End Class