-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathFormIR.cs
343 lines (230 loc) · 10.8 KB
/
FormIR.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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;
using System.Collections.Generic;
using AForge;
using AForge.Video;
using AForge.Video.DirectShow;
using AForge.Imaging;
using AForge.Imaging.Filters;
using System.Media;
namespace cam_aforge1
{
public partial class FormIR : Form
{
private bool DeviceExist = false;
private FilterInfoCollection videoDevices;
//private VideoCaptureDevice videoSource = null;
private FileVideoSource videoSource = null;
public FormIR()
{
InitializeComponent();
}
private void getCamList()
{
try
{
videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
comboBox1.Items.Clear();
if (videoDevices.Count == 0)
throw new ApplicationException();
DeviceExist = true;
foreach (FilterInfo device in videoDevices)
{
comboBox1.Items.Add(device.Name);
}
comboBox1.SelectedIndex = 0; //make dafault to first cam
}
catch (ApplicationException)
{
DeviceExist = false;
comboBox1.Items.Add("No capture video on your system");
}
}
private void rfsh_Click(object sender, EventArgs e)
{
getCamList();
}
private void start_Click(object sender, EventArgs e)
{
if (start.Text == "&Start")
{
var objDia = new OpenFileDialog
{
Title = @"Select File",
Filter = @"Video Files | *.wmv"
};
// videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString);
// videoSource = new VideoCaptureDevice();
if (objDia.ShowDialog() != DialogResult.OK) return;
videoSource = new FileVideoSource(objDia.FileName);
//videoSource = new FileVideoSource("G:\\fire detected_MAIN - Copy\\fire detected_MAIN - Copy\\bin\\Debug\\IR\\IR1.wmv");
//videoSource.DesiredFrameSize = new Size(320, 240);
//videoSource.DesiredFrameRate = 30;
videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame);
// CloseVideoSource();
videoSource.Start();
label2.Text = "Video running...";
start.Text = "&Stop";
timer1.Enabled = true;
}
else
{
label2.Text = "Error: No video selected.";
}
}
public void video_NewFrame(object sender, NewFrameEventArgs eventArgs)
{
Bitmap globaly, globalcb, globalcr, yChannel, cbChannel, crChannel;
Bitmap img = (Bitmap)eventArgs.Frame.Clone();
Bitmap imgsmoke = (Bitmap)eventArgs.Frame.Clone();
// create filter
//Erosion filterer = new Erosion();
// apply the filter
//filterer.Apply(img);
globaly = (Bitmap)eventArgs.Frame.Clone(); //bitmap image to store Y extract
// create filter
BrightnessCorrection filterb1 = new BrightnessCorrection(-0.5);
// apply the filter
filterb1.ApplyInPlace(globaly);
globalcb = (Bitmap)eventArgs.Frame.Clone(); //bitmap image to store Cb extract
// create filter
BrightnessCorrection filterb2 = new BrightnessCorrection(-0.5);
// apply the filter
filterb2.ApplyInPlace(globalcb);
globalcr = (Bitmap)eventArgs.Frame.Clone(); //bitmap image to store Cr extract
//globaly.Height = globalcb.Height = globalcr.Height = 25;
//globaly.Width = globalcb.Width = globalcr.Width = 50;
YCbCrExtractChannel filtery = new YCbCrExtractChannel(YCbCr.YIndex);
// apply the filter and extract Y channel
//globaly.Clone();
yChannel = filtery.Apply(globaly);
yChannel.Clone();
Bitmap y2Channel = (Bitmap)yChannel.Clone();
YCbCrExtractChannel filtercb = new YCbCrExtractChannel(YCbCr.CbIndex);
// apply the filter and extract Cb channel
globalcb.Clone();
cbChannel = filtercb.Apply(globalcb);
cbChannel.Clone();
Bitmap cb2Channel = (Bitmap)cbChannel.Clone();
YCbCrExtractChannel filtercr = new YCbCrExtractChannel(YCbCr.CrIndex);
// apply the filter and extract Cr channel
globalcr.Clone();
crChannel = filtercr.Apply(globalcr);
crChannel.Clone();
Bitmap cr2Channel = (Bitmap)crChannel.Clone();
//fire pixel processing
BitmapData subdata2 = cb2Channel.LockBits(new Rectangle(0, 0, cb2Channel.Width, cb2Channel.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
BitmapData subdata1 = y2Channel.LockBits(new Rectangle(0, 0, y2Channel.Width, y2Channel.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
// BitmapData subdata3 = cr2Channel.LockBits(new Rectangle(0, 0, cr2Channel.Width, cr2Channel.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed);
int stride1 = subdata2.Stride;
int stride0 = subdata1.Stride;
//int stride2 = subdata3.Stride;
System.IntPtr Scan01 = subdata2.Scan0;
System.IntPtr Scan02 = subdata1.Scan0;
//System.IntPtr Scan2 = subdata3.Scan0;
unsafe
{
byte* q = (byte*)(void*)Scan01;
byte* p = (byte*)(void*)Scan02;
//byte* r = (byte*)(void*)Scan2;
int cnt = 0 ;
//int nOffset1 = stride1 - cbChannel.Width * 3;
for (int y = 0; y < cbChannel.Height; y++) //scanning pixels horizontally
{
for (int x = 0; x < cbChannel.Width; x++) //scanning pixels vertically
{
int x1 = int.Parse(p[0].ToString());//y
int y1 = int.Parse(q[0].ToString());//cb
//int z1 = int.Parse(r[0].ToString());//cr
int z1 = x1 - y1;
if (z1 > 125) //defining condition for difference between y and cb channel for a fire pixel
{
cnt++;
}
p += 1;
//r +=1;
q += 1;
}
//q += nOffset1;
}
if (cnt > 125)
{
SoundPlayer simpleSound = new SoundPlayer(@"F:\fire detected_MAIN - Copy\fire detected_MAIN - Copy\Resources\\fewsec.wav");
simpleSound.Play();
//SoundPlayer simpleSound = new SoundPlayer(@"C:\\voice.wav");
//simpleSound.Play();
if (cnt < 150)
{
setLabel1TextSafe("Fire Detected!!! \n Low Intesity");
}
else if (cnt > 150 && cnt < 300)
{
setLabel1TextSafe("Fire Detected!!! \n Medium intesity");
}
else if (cnt > 300 && cnt <= 500)
{
setLabel1TextSafe("Fire Detected!!! \n High intesity");
}
}
else
{
setLabel1TextSafe(" ");
}
cb2Channel.UnlockBits(subdata2);
y2Channel.UnlockBits(subdata1);
}
pictureBox1.Image = img; //picturebox stores cloned image of frame
pictureBox2.Image = yChannel;
pictureBox3.Image = cbChannel;
pictureBox4.Image = crChannel;
}
private void setLabel1TextSafe(string txt)
{
if (label7.InvokeRequired)
{
label7.Invoke(new Action(() => label7.Text = txt));
return;
}
label7.Text = txt;
}
public delegate void Action();
private void CloseVideoSource()
{
if (!(videoSource == null))
if (videoSource.IsRunning)
{
videoSource.SignalToStop();
videoSource = null;
}
}
private void timer1_Tick(object sender, EventArgs e)
{
label2.Text = "video running... " + videoSource.FramesReceived.ToString() + " FPS";
}
private void Form1_FormClosed(object sender, FormClosedEventArgs e)
{
CloseVideoSource();
}
private void Form1_Load(object sender, EventArgs e)
{
getCamList();
this.Text = "Fire Detection Through Image Processing";
label8.Text = DateTime.Now.ToString("HH:mm:ss");
label9.Text = DateTime.Now.ToString("dd/MMMM/yyyy ,(dddd)");
}
private void pictureBox3_Click(object sender, EventArgs e)
{
}
private void pictureBox4_Click(object sender, EventArgs e)
{
}
private void timer2_Tick(object sender, EventArgs e)
{
label8.Text = DateTime.Now.ToString("HH:mm:ss");
label9.Text = DateTime.Now.ToString("dd/MMMM/yyyy ,(dddd)");
}
}
}