-
Intro to Computer Vision Using AForge.NET
AForge.NET is a computer vision and artificial intelligence
library originally developed by Andrew Kirillov for the .NET
Framework.
The source code and binaries of the project are available under
the terms of the Lesser GPL and the GPL (GNU General Public
License).
We are going to walk through a demo on how to setup a project
using Visual Studio. I will be using Visual Studio Express C# 2010
for this tutorial. However any version should work just fine.
To get started go to
http://www.aforgenet.com/framework/downloads.html and download the
installer.
You will also need to Visual Studio go to
https://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspx
Or if you want you can still download Visual Express 2010
here
http://download.microsoft.com/download/1/E/5/1E5F1C0A-0D5B-426A-A603-1798B951DDAE/VS2010Express1.iso
1
https://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspxhttps://www.visualstudio.com/en-us/products/visual-studio-express-vs.aspxhttp://download.microsoft.com/download/1/E/5/1E5F1C0A-0D5B-426A-A603-1798B951DDAE/VS2010Express1.isohttp://download.microsoft.com/download/1/E/5/1E5F1C0A-0D5B-426A-A603-1798B951DDAE/VS2010Express1.iso
-
Intro to Computer Vision Using AForge.NET
Run the Aforgeinstaller. And notethe location of theinstalled
framework.
Complete theinstallation and installVisual Studio.
2
-
Intro to Computer Vision Using AForge.NET
Open Visual Studio Express.
Andcreate anew project.
Select Windows Forms Application.
Enter in the Project Name and click OK.
3
-
Intro to Computer Vision Using AForge.NET
First we need to add a reference to the Aforge.NET.
Right click on References and select Add Reference.
Then navigate to the Aforge.NET release folder: C:\Program
Files\AForge.NET\Framework\Release
We want to add reference to four libraries.
C:\Program
Files\AForge.NET\Framework\Release\AForge.dllC:\Program
Files\AForge.NET\Framework\Release\AForge.Imaging.dllC:\Program
Files\AForge.NET\Framework\Release\AForge.Video.dllC:\Program
Files\AForge.NET\Framework\Release\AForge.Video.DirectShow.dll
4
-
Intro to Computer Vision Using AForge.NET
5
-
Intro to Computer Vision Using AForge.NET
Next double click on Form1.cs in the Solution Explorer menu to
the right. Then press F7 to go into view code.
And add the reference to the top of the code.
using AForge;using AForge.Imaging;using AForge.Video;using
AForge.Video.DirectShow;
Press Shift-F7 to switch back to view design.
Add three controls to your userform, PictureBox, Button and
ComboBox.
Rename each control.
ComboBox –> cboCameraButton → btnStartCameraPictureBox →
picVideo
Change the text on the button to“Start Camera” and Set the
Sizefor PictureBox to 320,240
Set PictureBox SizeMode toStretchImage
Next double click anywhere onthe form where there is not
acontrol. This will create a LoadEvent for the form.
6
-
Intro to Computer Vision Using AForge.NET
Add the following code
// Add Global References private FilterInfoCollection colCamera;
private VideoCaptureDevice CaptureDevice;
private void Form1_Load(object sender, EventArgs e) {
FillInDropDownCamera(); }
void FillInDropDownCamera() { colCamera = new
FilterInfoCollection(FilterCategory.VideoInputDevice); foreach
(FilterInfo Device in colCamera) {
cboCamera.Items.Add(Device.Name); cboCamera.SelectedIndex = 0;
CaptureDevice = new VideoCaptureDevice(); } }
This code will get a list of the cameras available and add them
to the drop down ComboBox list.
Press F5 to compile and run
You should see the list of camerasavailable.
Close the userform or press the Stopbutton in Visual Studio
7
-
Intro to Computer Vision Using AForge.NET
Next switch back to the Design Mode and double click on the
Start Camera Button.
This will make an event for the button.
We want to add the code to the button
private void btnStartCamera_Click(object sender, EventArgs e) {
CaptureDevice = new
VideoCaptureDevice(colCamera[cboCamera.SelectedIndex].MonikerString);
CaptureDevice.NewFrame += new
NewFrameEventHandler(FinalFrame_NewFrame); CaptureDevice.Start();
}
void FinalFrame_NewFrame(object sender, NewFrameEventArgs
eventArgs) {
Bitmap video1 = (Bitmap)eventArgs.Frame.Clone(); picVideo.Image
= video1;
}
The click event will start the camera and the
NewFrameEventHandler will run everytime a frame of video is
captured. The video is then overlayed on the PictureBox.
Next we need to add two more events, We will start with a
FormClosed event.
Select the border of the userform and click on the lightning
bolt icon at the top of properties menu. Find the FormClosed event
and double click next to it to add the event,
8
-
Intro to Computer Vision Using AForge.NET
Next add an event for the ComboBox DropDown.
Select the ComboBox and select the lightning bolt. Double Click
on SelectedIndexChanged
You will now see the two events added to your code.
Add the following code. This will ensure that the camera closes
on exit. And when the ComboBox changes selection.
private void Form1_FormClosed(object sender, FormClosedEventArgs
e) { CloseCamera(); }
private void cboCamera_SelectedIndexChanged(object sender,
EventArgs e) { CloseCamera(); }
void CloseCamera() {
if (CaptureDevice != null) { if (CaptureDevice.IsRunning ==
true) { CaptureDevice.Stop(); picVideo.Image = new Bitmap(640,
480); } } }
9
-
Intro to Computer Vision Using AForge.NET
Press F5 to run your code.
Next let's add a second PictureBox to our Userform.
You can select the picturebox and copy and paste a copy with
CTRL C and CTRL V.
Move the box to the right and rename to picVideoProcessed.
Add a line to the CloseCamera subroutine to blank out the video
on close
picVideoProcessed.Image = new Bitmap(640, 480);
void CloseCamera() {
if (CaptureDevice != null) { if (CaptureDevice.IsRunning ==
true) { CaptureDevice.Stop(); picVideo.Image = new Bitmap(640,
480); → picVideoProcessed.Image = new Bitmap(640, 480); } } }
10
-
Intro to Computer Vision Using AForge.NET
We need to add a reference to the second PictureBox to display
the image.
Add the following code
Bitmap video2 =
(Bitmap)eventArgs.Frame.Clone();picVideoProcessed.Image =
video2;
picVideoProcessed.Image = new Bitmap(640, 480);
void FinalFrame_NewFrame(object sender, NewFrameEventArgs
eventArgs) {
Bitmap video1 = (Bitmap)eventArgs.Frame.Clone(); picVideo.Image
= video1; → Bitmap video2 = (Bitmap)eventArgs.Frame.Clone(); →
picVideoProcessed.Image = video2;
}
void CloseCamera() {
if (CaptureDevice != null) { if (CaptureDevice.IsRunning ==
true) { CaptureDevice.Stop(); picVideo.Image = new Bitmap(640,
480); → picVideoProcessed.Image = new Bitmap(640, 480); } } }
11
-
Intro to Computer Vision Using AForge.NET
Now we can apply different filters to affect the processed
video.
Each frame is process separately as the FinalFrame_NewFrame
event is triggered.
Add a new filter
System.Drawing.Image MirrorImage(System.Drawing.Image Image1) {
// create filter Mirror filter = new Mirror(false, true); // apply
the filter filter.ApplyInPlace((Bitmap)Image1); return Image1;
}
And edit the line
picVideoProcessed.Image = video2;
to
picVideoProcessed.Image = MirrorImage(video2);
12
-
Intro to Computer Vision Using AForge.NET
Finally Let's Add a checkbox to our form and name it chkRaw and
add the following code.
This code will try to track an object based on it's color. We
can run it inline with other filters.
int intRedLower = 50; int intRedUpper = 100; int intGreenLower =
0; int intGreenUpper = 50; int intBlueLower = 0; int intBlueUpper =
50;
System.Drawing.Image CameraTracking(Bitmap video2) {
// create filter ColorFiltering colorFilter = new
ColorFiltering(); // set color ranges to keep colorFilter.Red = new
IntRange(intRedLower, intRedUpper); colorFilter.Green = new
IntRange(intGreenLower, intGreenUpper); colorFilter.Blue = new
IntRange(intBlueLower, intBlueUpper);
// apply the filter colorFilter.ApplyInPlace(video2);
BlobCounter BlobCounter = new BlobCounter();
// MinHeight doesn't seem to do anything? BlobCounter.MinHeight
= 50;
BlobCounter.ObjectsOrder = ObjectsOrder.Size;
BlobCounter.ProcessImage(video2); Rectangle[] rect =
BlobCounter.GetObjectsRectangles(); if (chkRaw.Checked) { if
(rect.Length > 0) { for (int i = 0; i
-
Intro to Computer Vision Using AForge.NET
{ bool bolRectFound = false;
if (rect.Length > 0) { Rectangle obj = rect[0]; int X = obj.X
+ (obj.Width / 2); int Y = obj.Y + (obj.Height / 2);
// filter out small objects int intThreshhold = 5; if (obj.Width
> intThreshhold && obj.Height > intThreshhold) {
Graphics graphic = Graphics.FromImage(video2); using (Pen Pen =
new Pen(Color.White, 3)) { graphic.DrawRectangle(Pen, obj); }
graphic.Dispose(); bolRectFound = true; }
}
}
return video2; }
and then we can run the code on each frame.
void FinalFrame_NewFrame(object sender, NewFrameEventArgs
eventArgs) {
Bitmap video1 = (Bitmap)eventArgs.Frame.Clone(); picVideo.Image
= video1; Bitmap video2 = (Bitmap)eventArgs.Frame.Clone(); →
picVideoProcessed.Image = CameraTracking((Bitmap)
MirrorImage(video2));
}
You can adjust the color ranges by changing the intRefLower and
intRedUpper Ranges.
14