-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathForm1.cs
191 lines (151 loc) · 6.14 KB
/
Form1.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
using AForge;
using AForge.Imaging;
using AForge.Imaging.Filters;
using AForge.Math.Geometry;
using AForge.Video;
using AForge.Video.DirectShow;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace WindowsFormsApplication2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private FilterInfoCollection CuptreDevice;
private VideoCaptureDevice FinalFrame=null;
Color color;
float hue;
private void Form1_Load(object sender, EventArgs e)
{
CuptreDevice = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo Device in CuptreDevice)
{
comboBox1.Items.Add(Device.Name);
}
comboBox1.SelectedIndex = 0;
FinalFrame = new VideoCaptureDevice();
}
private void button1_Click(object sender, EventArgs e)
{
FinalFrame = new VideoCaptureDevice(CuptreDevice[comboBox1.SelectedIndex].MonikerString);
FinalFrame.NewFrame += FinalFrame_NewFrame;
FinalFrame.Start();
}
private void FinalFrame_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
try
{
Bitmap video = (Bitmap)eventArgs.Frame.Clone();
Bitmap tmp = video.Clone() as Bitmap;
//Create color filter
EuclideanColorFiltering filter = new EuclideanColorFiltering();
filter.CenterColor = new AForge.Imaging.RGB(color.R, color.G, color.B);
filter.Radius = 90;
filter.ApplyInPlace(tmp);
BlobCounter blobcounter = new BlobCounter();
blobcounter.MinHeight = Convert.ToInt32(numericUpDown1.Value);
blobcounter.MinWidth = Convert.ToInt32(numericUpDown2.Value);
blobcounter.FilterBlobs = true;
blobcounter.ObjectsOrder = ObjectsOrder.Size;
//locate blobs
blobcounter.ProcessImage(tmp);
Rectangle[] rects = blobcounter.GetObjectsRectangles();
Blob[] bBlobs = blobcounter.GetObjectsInformation();
SimpleShapeChecker shapeChecker = new SimpleShapeChecker();
//draw rectangle around the biggest blob
Pen yellowPen = new Pen(Color.Red, 5);
foreach (Rectangle recs in rects)
if (rects.Length > 0)
{
Rectangle objectRect1 = recs;
Graphics g = Graphics.FromImage(video);
using (Pen pen = new Pen(Color.FromArgb(160, 255, 160), 5))
{
g.DrawRectangle(pen, objectRect1);
PointF drawPoin = new PointF(objectRect1.X, objectRect1.Y);
int objectX = objectRect1.X + objectRect1.Width / 2 - video.Width / 2;
int objectY = video.Height / 2 - (objectRect1.Y + objectRect1.Height / 2);
String Blobinformation = "X= " + objectX.ToString() + "\nY= " + objectY.ToString() + "\nSize=" + objectRect1.Size.ToString();
g.DrawString(Blobinformation, new Font("Arial", 16), new SolidBrush(Color.Blue), drawPoin);
if (objectY == 1)
{
// MessageBox.Show("BOOOOM!!!");
Console.WriteLine("BOOOOM!!!");
Console.WriteLine("X: {0} Y: {1}",objectX,objectY);
video.SetResolution(500, 200);
// g.Dispose();
break;
}
}
g.Dispose();
}
//circle
//for (int i = 0, n = bBlobs.Length; i < n; i++)
//{
// List<IntPoint> edgePoints = blobcounter.GetBlobsEdgePoints(bBlobs[i]);
// Graphics g = Graphics.FromImage(video);
// AForge.Point center;
// float radius;
// // is circle ?
// if (shapeChecker.IsCircle(edgePoints, out center, out radius))
// {
// g.DrawEllipse(yellowPen,
// (float)(center.X - radius), (float)(center.Y - radius),
// (float)(radius * 2), (float)(radius * 2));
// }
//}
//
//yellowPen.Dispose();
pictureBox1.Image = video;
}
catch(Exception)
{
numericUpDown1.Value = 5;
numericUpDown1.Value = 5;
MessageBox.Show("Erorr!!");
}
// pictureBox2.Image = grayImage;
}
// public Rectangle objectRect1 { get; set; }
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
if (FinalFrame.IsRunning == true)
{
FinalFrame.Stop();
}
}
private void button3_Click_1(object sender, EventArgs e)
{
colorDialog1.ShowDialog();
color = (Color)colorDialog1.Color;
hue = color.GetHue();
}
private void button3_Click(object sender, EventArgs e)
{
if (FinalFrame.IsRunning == true)
{
FinalFrame.Stop();
}
pictureBox1.Image = null;
}
private void Form1_Leave(object sender, EventArgs e)
{
if (FinalFrame.IsRunning == true)
{
FinalFrame.Stop();
}
pictureBox1.Image = null;
}
}
}