-
Notifications
You must be signed in to change notification settings - Fork 151
/
Copy pathCannyWebcam.cs
88 lines (71 loc) · 3.26 KB
/
CannyWebcam.cs
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
// CannyWebcam.cs
//
// Emgu CV 3.0.0
//
// put this code in your main form, for example frmMain.cs
// add the following components to your form:
//
// tableLayoutPanel (TableLayoutPanel)
// ibOriginal (Emgu ImageBox)
// ibCanny (Emgu ImageBox)
//
// 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
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Emgu.CV; //
using Emgu.CV.CvEnum; // usual Emgu CV imports
using Emgu.CV.Structure; //
using Emgu.CV.UI; //
///////////////////////////////////////////////////////////////////////////////////////////////////
namespace CannyWebcam1 {
///////////////////////////////////////////////////////////////////////////////////////////////
public partial class frmMain : Form {
// member variables ///////////////////////////////////////////////////////////////////////
Capture capWebcam;
// constructor ////////////////////////////////////////////////////////////////////////////
public frmMain() {
InitializeComponent();
}
///////////////////////////////////////////////////////////////////////////////////////////
private void frmMain_Load(object sender, EventArgs e) {
try {
capWebcam = new Capture(0);
} catch(Exception ex) {
MessageBox.Show("unable to read from webcam, error: " + Environment.NewLine + Environment.NewLine +
ex.Message + Environment.NewLine + Environment.NewLine +
"exiting program");
Environment.Exit(0);
return;
}
Application.Idle += processFrameAndUpdateGUI; // add process image function to the application's list of tasks
}
///////////////////////////////////////////////////////////////////////////////////////////
void processFrameAndUpdateGUI(object sender, EventArgs arg) {
Mat imgOriginal;
imgOriginal = capWebcam.QueryFrame();
if(imgOriginal == null) {
MessageBox.Show("unable to read frame from webcam" + Environment.NewLine + Environment.NewLine +
"exiting program");
Environment.Exit(0);
return;
}
Mat imgGrayscale = new Mat(imgOriginal.Size, DepthType.Cv8U, 1);
Mat imgBlurred = new Mat(imgOriginal.Size, DepthType.Cv8U, 1);
Mat imgCanny = 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;
ibCanny.Image = imgCanny;
}
} // end class
} // end namespace