/// <summary> /// Image Processing. It is done using OpenCVSharp Library. /// </summary> private void ProcessFrame(object sender, EventArgs e) { // increment counter counter++; videoInput.GetPixels(deviceID, frame.ImageData, false, !InvertVertical); if (InvertHorizontal) { frame.Flip(frame, FlipMode.Y); } // convert to grayscale image Cv.CvtColor(frame, grayFrame, ColorConversion.BgrToGray); // apply force field transform transformedFrame = ForceFieldTransform(grayFrame); //Cv.Threshold(grayFrame, grayFrame, grayLowValue, 255, ThresholdType.Binary); //Cv.CvtColor(grayFrame, frame, ColorConversion.GrayToBgr); //Cv.Smooth(grayFrame, grayFrame, SmoothType.Gaussian, smoothGaussianValue); // show image on the separate window window.Image = transformedFrame; }
private void CaptureMotion() { try { float wFactor = (float)this.Width / (float)CaptureBox.Width; float hFactor = (float)this.Height / (float)CaptureBox.Height; CvArr array = null; CvCapture cap = CvCapture.FromCamera(CaptureDevice.Any); this.Invoke(new Action(() => { lblLoading.Visible = false; GBBox.Visible = true; CamBox.Visible = true; RadarBox.Visible = true; lblGhost.Visible = true; GhostBox.Visible = true; lblDescription.Visible = true; CaptureBox.Visible = true; })); SoundTrack.Play(); SoundTrack = new SoundPlayer("Alarm.wav"); Thread.Sleep(3600); while (true) { IplImage img = cap.QueryFrame(); if (img == null) { continue; } //int black = img //if (black < 10) //{ // MessageBox.Show("Please get off cameras' field of view and switch off the lights!", "Ghost Busters Cam"); // continue; //} img.Flip(array, FlipMode.Y); string filepath = "haarcascade_frontalface_alt2.xml"; CvHaarClassifierCascade cascade = CvHaarClassifierCascade.FromFile(filepath); CvSeq <CvAvgComp> faces = Cv.HaarDetectObjects(img, cascade, Cv.CreateMemStorage(), 3.0, 1, HaarDetectionType.Zero, new CvSize(70, 70), new CvSize(500, 500)); Bitmap Image = BitmapConverter.ToBitmap(img); Image.SetResolution(240, 180); this.Invoke(new Action(() => { CaptureBox.Image = Image; })); foreach (CvAvgComp face in faces) { IplImage ClonedImage = img.Clone(); Cv.SetImageROI(ClonedImage, face.Rect); IplImage ThisFace = Cv.CreateImage(face.Rect.Size, ClonedImage.Depth, ClonedImage.NChannels); Cv.Copy(ClonedImage, ThisFace, null); Cv.ResetImageROI(ClonedImage); Bitmap GhostImage = BitmapConverter.ToBitmap(ThisFace); GhostImage.SetResolution(114, 103); img.DrawRect(face.Rect, CvColor.Red, 3); Bitmap FaceImage = BitmapConverter.ToBitmap(img); FaceImage.SetResolution(240, 180); this.Invoke(new Action(() => { AlertTimer.Enabled = true; CaptureBox.Image = FaceImage; GhostBox.Image = GhostImage; })); SoundTrack.Play(); Thread.Sleep(2500); this.Invoke(new Action(() => { AlertTimer.Enabled = false; lblGhostAlert.Visible = false; })); SoundTrack.Stop(); break; } } } catch (Exception e) { Console.WriteLine(e.Message); } }
private void CaptureMotion() { try { float wFactor = (float)this.Width / (float)CaptureBox.Width; float hFactor = (float)this.Height / (float)CaptureBox.Height; CvArr array = null; CvCapture cap = CvCapture.FromCamera(CaptureDevice.Any); this.Invoke(new Action(() => { lblLoading.Visible = false; radioButton1.Visible = true; radioButton2.Visible = true; })); while (true) { IplImage img = cap.QueryFrame(); if (img == null) { continue; } img.Flip(array, FlipMode.Y); if (mode == 1) { string filepath = "haarcascade_frontalface_alt2.xml"; CvHaarClassifierCascade cascade = CvHaarClassifierCascade.FromFile(filepath); CvSeq <CvAvgComp> faces = Cv.HaarDetectObjects(img, cascade, Cv.CreateMemStorage(), 3.0, 1, HaarDetectionType.Zero, new CvSize(70, 70), new CvSize(500, 500)); foreach (CvAvgComp face in faces) { //IplImage ClonedImage = img.Clone(); //Cv.SetImageROI(ClonedImage, face.Rect); //IplImage ThisFace = Cv.CreateImage(face.Rect.Size, ClonedImage.Depth, ClonedImage.NChannels); //Cv.Copy(ClonedImage, ThisFace, null); //Cv.ResetImageROI(ClonedImage); //Bitmap FaceImage = BitmapConverter.ToBitmap(ThisFace); //FaceImage.SetResolution(240, 180); //CaptureBox.Image = FaceImage; img.DrawRect(face.Rect, CvColor.Red, 3); Bitmap FaceImage = BitmapConverter.ToBitmap(img); FaceImage.SetResolution(240, 180); CaptureBox.Image = FaceImage; this.Invoke(new Action(() => { LifeBox.Left = (int)(face.Rect.Left * wFactor - (float)(LifeBox.Width / 2.0) - (float)(this.Width / 2.0)); LifeBox.Top = (int)(face.Rect.Top * hFactor - (float)(LifeBox.Height / 2.0) - (float)(this.Height / 2.0)); if (LifeBox.Left > (this.Width - LifeBox.Width - 12)) { LifeBox.Left = (this.Width - LifeBox.Width - 24); } if (LifeBox.Top > (this.Height - LifeBox.Height - 48)) { LifeBox.Top = (this.Height - LifeBox.Height - 48); } if (LifeBox.Left < 12) { LifeBox.Left = 12; } if (LifeBox.Top < 12) { LifeBox.Top = 12; } Thread.Sleep(30); })); break; } } else { int AllBlobs = 0; CvBlobs blobs = null; IplImage imgHSVsrc = Cv.CreateImage(Cv.GetSize(img), BitDepth.U8, 3); IplImage imgHSVdst = Cv.CreateImage(Cv.GetSize(img), BitDepth.U8, 1); Cv.CvtColor(img, imgHSVsrc, ColorConversion.BgrToHsv); Cv.InRangeS(imgHSVsrc, new CvScalar(86, 80, 30), new CvScalar(115, 250, 250), imgHSVdst); Cv.ReleaseImage(imgHSVsrc); blobs = new CvBlobs(imgHSVdst); blobs.FilterByArea(7000, 40000); AllBlobs = blobs.Count; foreach (KeyValuePair <int, CvBlob> blob in blobs) { CvBlob CurrentBlob = blob.Value; CvRect BlobRect = CurrentBlob.Rect; CvPoint Point1, Point2; Point1.X = BlobRect.X; Point1.Y = BlobRect.Y; Point2.X = BlobRect.X + BlobRect.Width; Point2.Y = BlobRect.Y + BlobRect.Height; img.DrawRect(Point1, Point2, CvColor.LightGreen, 3, LineType.AntiAlias); this.Invoke(new Action(() => { LifeBox.Left = (int)(BlobRect.Left * wFactor - (float)(LifeBox.Width / 2.0) - (float)(this.Width / 2.0)); LifeBox.Top = (int)(BlobRect.Top * hFactor - (float)(LifeBox.Height / 2.0) - (float)(this.Height / 2.0)); if (LifeBox.Left > (this.Width - LifeBox.Width - 12)) { LifeBox.Left = (this.Width - LifeBox.Width - 24); } if (LifeBox.Top > (this.Height - LifeBox.Height - 48)) { LifeBox.Top = (this.Height - LifeBox.Height - 48); } if (LifeBox.Left < 12) { LifeBox.Left = 12; } if (LifeBox.Top < 12) { LifeBox.Top = 12; } Thread.Sleep(30); })); break; } Bitmap Item = BitmapConverter.ToBitmap(img); Item.SetResolution(240, 180); CaptureBox.Image = Item; Bitmap HSVItem = BitmapConverter.ToBitmap(imgHSVdst); HSVItem.SetResolution(240, 180); HSVCaptureBox.Image = HSVItem; Cv.ReleaseImage(imgHSVdst); } } } catch (Exception e) { Console.WriteLine("ERROR: " + e.Message + "DETAILS: " + e.StackTrace); } }