|
| 1 | + |
| 2 | + |
| 3 | +using System; |
| 4 | +using System.Drawing; |
| 5 | +using System.Drawing.Imaging; |
| 6 | +using System.Text; |
| 7 | +using System.Windows.Forms; |
| 8 | +using System.Collections.Generic; |
| 9 | +using AForge; |
| 10 | +using AForge.Video; |
| 11 | +using AForge.Video.DirectShow; |
| 12 | +using AForge.Imaging; |
| 13 | +using AForge.Imaging.Filters; |
| 14 | +using System.Media; |
| 15 | + |
| 16 | + |
| 17 | + |
| 18 | + |
| 19 | +namespace cam_aforge1 |
| 20 | +{ |
| 21 | + |
| 22 | + public partial class Form1 : Form |
| 23 | + |
| 24 | + { |
| 25 | + private bool DeviceExist = false; |
| 26 | + private FilterInfoCollection videoDevices; |
| 27 | + private VideoCaptureDevice videoSource = null; |
| 28 | + //private FileVideoSource videoSource = null; |
| 29 | + |
| 30 | + |
| 31 | + |
| 32 | + public Form1() |
| 33 | + { |
| 34 | + InitializeComponent(); |
| 35 | + } |
| 36 | + |
| 37 | + private void getCamList() |
| 38 | + { |
| 39 | + try |
| 40 | + { |
| 41 | + videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice); |
| 42 | + comboBox1.Items.Clear(); |
| 43 | + if (videoDevices.Count == 0) |
| 44 | + throw new ApplicationException(); |
| 45 | + |
| 46 | + DeviceExist = true; |
| 47 | + foreach (FilterInfo device in videoDevices) |
| 48 | + { |
| 49 | + comboBox1.Items.Add(device.Name); |
| 50 | + } |
| 51 | + comboBox1.SelectedIndex = 0; //make dafault to first cam |
| 52 | + } |
| 53 | + catch (ApplicationException) |
| 54 | + { |
| 55 | + DeviceExist = false; |
| 56 | + comboBox1.Items.Add("No capture device on your system"); |
| 57 | + } |
| 58 | + } |
| 59 | + |
| 60 | + private void rfsh_Click(object sender, EventArgs e) |
| 61 | + { |
| 62 | + getCamList(); |
| 63 | + } |
| 64 | + |
| 65 | + private void start_Click(object sender, EventArgs e) |
| 66 | + { |
| 67 | + if (start.Text == "&Start") |
| 68 | + { |
| 69 | + if (DeviceExist) |
| 70 | + { |
| 71 | + videoSource = new VideoCaptureDevice(videoDevices[comboBox1.SelectedIndex].MonikerString); |
| 72 | + // videoSource = new VideoCaptureDevice(); |
| 73 | + // videoSource = new FileVideoSource("IR2.mp4"); |
| 74 | + videoSource.DesiredFrameSize = new Size(320, 240); |
| 75 | + videoSource.DesiredFrameRate = 30; |
| 76 | + |
| 77 | + videoSource.NewFrame += new NewFrameEventHandler(video_NewFrame); |
| 78 | + |
| 79 | + |
| 80 | + CloseVideoSource(); |
| 81 | + |
| 82 | + videoSource.Start(); |
| 83 | + |
| 84 | + |
| 85 | + label2.Text = "Device running..."; |
| 86 | + start.Text = "&Stop"; |
| 87 | + timer1.Enabled = true; |
| 88 | + |
| 89 | + |
| 90 | + } |
| 91 | + else |
| 92 | + { |
| 93 | + label2.Text = "Error: No Device selected."; |
| 94 | + } |
| 95 | + } |
| 96 | + else |
| 97 | + { |
| 98 | + if (videoSource.IsRunning) |
| 99 | + { |
| 100 | + timer1.Enabled = false; |
| 101 | + CloseVideoSource(); |
| 102 | + label2.Text = "Device stopped."; |
| 103 | + start.Text = "&Start"; |
| 104 | + } |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + |
| 109 | + |
| 110 | + |
| 111 | + public void video_NewFrame(object sender, NewFrameEventArgs eventArgs) |
| 112 | + { |
| 113 | + Bitmap globaly, globalcb, globalcr, yChannel, cbChannel, crChannel; |
| 114 | + |
| 115 | + |
| 116 | + Bitmap img = (Bitmap)eventArgs.Frame.Clone(); |
| 117 | + // Bitmap imgsmoke = (Bitmap)eventArgs.Frame.Clone(); |
| 118 | + // create filter |
| 119 | + //Erosion filterer = new Erosion(); |
| 120 | + // apply the filter |
| 121 | + //filterer.Apply(img); |
| 122 | + |
| 123 | + |
| 124 | + |
| 125 | + globaly = (Bitmap)eventArgs.Frame.Clone(); //bitmap image to store Y extract |
| 126 | + |
| 127 | + // create filter |
| 128 | + BrightnessCorrection filterb1 = new BrightnessCorrection(-0.5); |
| 129 | + // apply the filter |
| 130 | + filterb1.ApplyInPlace(globaly); |
| 131 | + |
| 132 | + globalcb = (Bitmap)eventArgs.Frame.Clone(); //bitmap image to store Cb extract |
| 133 | + |
| 134 | + // create filter |
| 135 | + BrightnessCorrection filterb2 = new BrightnessCorrection(-0.5); |
| 136 | + // apply the filter |
| 137 | + filterb2.ApplyInPlace(globalcb); |
| 138 | + |
| 139 | + globalcr = (Bitmap)eventArgs.Frame.Clone(); //bitmap image to store Cr extract |
| 140 | + //globaly.Height = globalcb.Height = globalcr.Height = 25; |
| 141 | + //globaly.Width = globalcb.Width = globalcr.Width = 50; |
| 142 | + |
| 143 | + |
| 144 | + YCbCrExtractChannel filtery = new YCbCrExtractChannel(YCbCr.YIndex); |
| 145 | + // apply the filter and extract Y channel |
| 146 | + //globaly.Clone(); |
| 147 | + yChannel = filtery.Apply(globaly); |
| 148 | + |
| 149 | + |
| 150 | + yChannel.Clone(); |
| 151 | + Bitmap y2Channel = (Bitmap)yChannel.Clone(); |
| 152 | + |
| 153 | + |
| 154 | + |
| 155 | + YCbCrExtractChannel filtercb = new YCbCrExtractChannel(YCbCr.CbIndex); |
| 156 | + // apply the filter and extract Cb channel |
| 157 | + globalcb.Clone(); |
| 158 | + cbChannel = filtercb.Apply(globalcb); |
| 159 | + |
| 160 | + |
| 161 | + cbChannel.Clone(); |
| 162 | + Bitmap cb2Channel = (Bitmap)cbChannel.Clone(); |
| 163 | + |
| 164 | + |
| 165 | + |
| 166 | + YCbCrExtractChannel filtercr = new YCbCrExtractChannel(YCbCr.CrIndex); |
| 167 | + // apply the filter and extract Cr channel |
| 168 | + globalcr.Clone(); |
| 169 | + crChannel = filtercr.Apply(globalcr); |
| 170 | + |
| 171 | + crChannel.Clone(); |
| 172 | + Bitmap cr2Channel = (Bitmap)crChannel.Clone(); |
| 173 | + |
| 174 | + |
| 175 | + |
| 176 | + |
| 177 | + |
| 178 | + //fire pixel processing |
| 179 | + BitmapData subdata2 = cb2Channel.LockBits(new Rectangle(0, 0, cb2Channel.Width, cb2Channel.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); |
| 180 | + BitmapData subdata1 = y2Channel.LockBits(new Rectangle(0, 0, y2Channel.Width, y2Channel.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); |
| 181 | + // BitmapData subdata3 = cr2Channel.LockBits(new Rectangle(0, 0, cr2Channel.Width, cr2Channel.Height), ImageLockMode.ReadWrite, PixelFormat.Format8bppIndexed); |
| 182 | + |
| 183 | + int stride1 = subdata2.Stride; |
| 184 | + int stride0 = subdata1.Stride; |
| 185 | + //int stride2 = subdata3.Stride; |
| 186 | + |
| 187 | + System.IntPtr Scan01 = subdata2.Scan0; |
| 188 | + System.IntPtr Scan02 = subdata1.Scan0; |
| 189 | + //System.IntPtr Scan2 = subdata3.Scan0; |
| 190 | + |
| 191 | + |
| 192 | + unsafe |
| 193 | + { |
| 194 | + byte* q = (byte*)(void*)Scan01; |
| 195 | + byte* p = (byte*)(void*)Scan02; |
| 196 | + |
| 197 | + //byte* r = (byte*)(void*)Scan2; |
| 198 | + |
| 199 | + |
| 200 | + int cnt = 0 ; |
| 201 | + //int nOffset1 = stride1 - cbChannel.Width * 3; |
| 202 | + |
| 203 | + |
| 204 | + |
| 205 | + for (int y = 0; y < cbChannel.Height; y++) //scanning pixels horizontally |
| 206 | + { |
| 207 | + for (int x = 0; x < cbChannel.Width; x++) //scanning pixels vertically |
| 208 | + { |
| 209 | + |
| 210 | + |
| 211 | + int x1 = int.Parse(p[0].ToString());//y |
| 212 | + int y1 = int.Parse(q[0].ToString());//cb |
| 213 | + //int z1 = int.Parse(r[0].ToString());//cr |
| 214 | + int z1 = x1 - y1; |
| 215 | + |
| 216 | + if (z1 > 125) //defining condition for difference between y and cb channel for a fire pixel of ligter flame (trial & error) |
| 217 | + { |
| 218 | + cnt++; |
| 219 | + } |
| 220 | + p += 1; |
| 221 | + //r +=1; |
| 222 | + q += 1; |
| 223 | + } |
| 224 | + |
| 225 | + //q += nOffset1; |
| 226 | + } |
| 227 | + |
| 228 | + |
| 229 | + if (cnt > 125) |
| 230 | + { |
| 231 | + |
| 232 | + |
| 233 | + |
| 234 | + |
| 235 | + //SoundPlayer simpleSound = new SoundPlayer(@"C:\voice.wav"); |
| 236 | + //simpleSound.Play(); |
| 237 | + |
| 238 | + SoundPlayer simpleSound = new SoundPlayer(@"D:\fire detected_MAIN - Copy\fire detected_MAIN - Copy\Resources\\fewsec.wav"); |
| 239 | + //SoundPlayer simpleSound = new SoundPlayer(@"F:\fire detected_MAIN - Copy\fire detected_MAIN - Copy\\Resources\\"); |
| 240 | + simpleSound.Play(); |
| 241 | + if (cnt < 150) |
| 242 | + { |
| 243 | + setLabel1TextSafe("Fire Detected!!! \n Low Intesity"); |
| 244 | + } |
| 245 | + else if (cnt > 150 && cnt < 300) |
| 246 | + { |
| 247 | + setLabel1TextSafe("Fire Detected!!! \n Medium intesity"); |
| 248 | + } |
| 249 | + else if (cnt > 300 && cnt <= 500) |
| 250 | + { |
| 251 | + setLabel1TextSafe("Fire Detected!!! \n High intesity"); |
| 252 | + } |
| 253 | + |
| 254 | + |
| 255 | + |
| 256 | + } |
| 257 | + else |
| 258 | + { |
| 259 | + setLabel1TextSafe(" "); |
| 260 | + } |
| 261 | + |
| 262 | + cb2Channel.UnlockBits(subdata2); |
| 263 | + y2Channel.UnlockBits(subdata1); |
| 264 | + } |
| 265 | + pictureBox1.Image = img; //picturebox stores cloned image of frame |
| 266 | + pictureBox2.Image = yChannel; |
| 267 | + pictureBox3.Image = cbChannel; |
| 268 | + pictureBox4.Image = crChannel; |
| 269 | + |
| 270 | + |
| 271 | + |
| 272 | + |
| 273 | + |
| 274 | + |
| 275 | + } |
| 276 | + |
| 277 | + |
| 278 | + |
| 279 | + private void setLabel1TextSafe(string txt) |
| 280 | + { |
| 281 | + if (label7.InvokeRequired) |
| 282 | + { |
| 283 | + label7.Invoke(new Action(() => label7.Text = txt)); |
| 284 | + return; |
| 285 | + } |
| 286 | + label7.Text = txt; |
| 287 | + } |
| 288 | + |
| 289 | + public delegate void Action(); |
| 290 | + private void CloseVideoSource() |
| 291 | + { |
| 292 | + if (!(videoSource == null)) |
| 293 | + if (videoSource.IsRunning) |
| 294 | + { |
| 295 | + videoSource.SignalToStop(); |
| 296 | + videoSource = null; |
| 297 | + } |
| 298 | + } |
| 299 | + |
| 300 | + private void timer1_Tick(object sender, EventArgs e) |
| 301 | + { |
| 302 | + label2.Text = "Device running... " + videoSource.FramesReceived.ToString() + " FPS"; |
| 303 | + } |
| 304 | + |
| 305 | + private void Form1_FormClosed(object sender, FormClosedEventArgs e) |
| 306 | + { |
| 307 | + CloseVideoSource(); |
| 308 | + } |
| 309 | + |
| 310 | + private void Form1_Load(object sender, EventArgs e) |
| 311 | + { |
| 312 | + |
| 313 | + getCamList(); |
| 314 | + this.Text = "Fire Detection Through Image Processing"; |
| 315 | + label8.Text = DateTime.Now.ToString("HH:mm:ss"); |
| 316 | + label9.Text = DateTime.Now.ToString("dd/MMMM/yyyy ,(dddd)"); |
| 317 | + } |
| 318 | + |
| 319 | + private void pictureBox3_Click(object sender, EventArgs e) |
| 320 | + { |
| 321 | + |
| 322 | + } |
| 323 | + |
| 324 | + private void pictureBox4_Click(object sender, EventArgs e) |
| 325 | + { |
| 326 | + |
| 327 | + } |
| 328 | + |
| 329 | + private void timer2_Tick(object sender, EventArgs e) |
| 330 | + { |
| 331 | + label8.Text = DateTime.Now.ToString("HH:mm:ss"); |
| 332 | + label9.Text = DateTime.Now.ToString("dd/MMMM/yyyy ,(dddd)"); |
| 333 | + |
| 334 | + |
| 335 | + } |
| 336 | + |
| 337 | + |
| 338 | + |
| 339 | + |
| 340 | + } |
| 341 | + |
| 342 | + |
| 343 | +} |
0 commit comments