Esempio n. 1
0
        void timer_Tick(object sender, EventArgs e)
        {
            //Mat frame = _capture.QueryFrame();

            Mat frame = CvInvoke.Imread(@"C:\Users\Merc.Mercury\Desktop\franks.png", Emgu.CV.CvEnum.ImreadModes.AnyColor);

            if (frame != null)
            {
                Mat gray = new Mat();
                CvInvoke.CvtColor(frame, gray, Emgu.CV.CvEnum.ColorConversion.Bgr2Gray);
                CvInvoke.EqualizeHist(gray, gray);
                Rectangle[] faces = _cascade.DetectMultiScale(gray, 1.3, 3, new System.Drawing.Size(gray.Width / 30, gray.Height / 30), new System.Drawing.Size((int)((double)gray.Width / 1.05), (int)((double)gray.Height / 1.05)));

                Thread oThread = new Thread(new ParameterizedThreadStart(priority_sort));
                oThread.Start(faces);


                foreach (Rectangle face in faces)
                {
                    Emgu.CV.Structure.MCvScalar color = new Emgu.CV.Structure.MCvScalar(0, 0, 153);
                    CvInvoke.Rectangle(frame, face, color, 5);
                }

                CV_Output.Source = BitmapToImageSource(frame.Bitmap);
            }
        }
Esempio n. 2
0
        // 在攝影機畫面中,給被偵測到的臉部加上框,並且用字串把疼痛指數大小量視覺化
        static void DrawFaceBox(Mat frameFaceBox, System.Drawing.Rectangle faceRect, double PSPI)
        {
            Emgu.CV.Structure.MCvScalar colorOrange  = new Emgu.CV.Structure.MCvScalar(51, 150, 255);
            System.Drawing.Point        textPosition = new System.Drawing.Point(faceRect.X, faceRect.Y + 22 + faceRect.Size.Height);

            String painNum    = String.Format("PSPI={0:0.0}", PSPI);
            String painLength = new string('*', Convert.ToInt32(PSPI >= 0 ? PSPI : 0));

            CvInvoke.Rectangle(frameFaceBox, faceRect, colorOrange, 1);
            CvInvoke.PutText(frameFaceBox, painNum, textPosition, Emgu.CV.CvEnum.FontFace.HersheyComplex, 1, colorOrange, 2);

            textPosition.Y = faceRect.Y;
            CvInvoke.PutText(frameFaceBox, painLength, textPosition, Emgu.CV.CvEnum.FontFace.HersheyComplex, 1, colorOrange, 2);
        }
Esempio n. 3
0
        private List <double> CalculateDirection(Mat angle, Mat magnitude)
        {
            float         PI        = Math.PI.To <float>();
            List <double> offsetSum = new List <double>();
            Mat           matX      = new Mat();
            Mat           matY      = new Mat();

            CvInvoke.PolarToCart(magnitude, angle, matX, matY, true);
            Emgu.CV.Structure.MCvScalar sumX = CvInvoke.Sum(matX);
            Emgu.CV.Structure.MCvScalar sumY = CvInvoke.Sum(matY);
            double x     = sumX.V0;
            double y     = sumY.V0;
            double scale = Math.Sqrt(x * x + y * y);

            x /= scale;
            y /= scale;
            offsetSum.Add(x);
            offsetSum.Add(y);
            return(offsetSum);
        }
Esempio n. 4
0
 private void btnPreview_Click(object sender, EventArgs e)
 {
     using (Mat drawingBoard = previewImage.Clone())
     {
         Emgu.CV.Structure.MCvScalar redColor = new Emgu.CV.Structure.MCvScalar(0, 0, 255);
         // Draw detection fields on preview image and let user see it!
         foreach (DetectionField f in detectionTemplate.Fields)
         {
             CvInvoke.Rectangle(drawingBoard, new Rectangle(f.TopLeft, f.Size), redColor);
             for (int i = 1; i < f.NumOfCols; i++)
             {
                 CvInvoke.Line(drawingBoard, new Point(f.TopLeft.X + f.Size.Width * i / f.NumOfCols, f.TopLeft.Y), new Point(f.TopLeft.X + f.Size.Width * i / f.NumOfCols, f.TopLeft.Y + f.Size.Height), redColor);
             }
             for (int i = 1; i < f.NumOfRows; i++)
             {
                 CvInvoke.Line(drawingBoard, new Point(f.TopLeft.X, f.TopLeft.Y + f.Size.Height * i / f.NumOfRows), new Point(f.TopLeft.X + f.Size.Width, f.TopLeft.Y + f.Size.Height * i / f.NumOfRows), redColor);
             }
         }
         Emgu.CV.UI.ImageViewer imv = new Emgu.CV.UI.ImageViewer(drawingBoard);
         imv.Show();
     }
 }
Esempio n. 5
0
        public UMat DrawPatternRectangle(UMat image, float zoomRatio, Point aftZoomRoiLeftUpPt, Emgu.CV.Structure.MCvScalar color)
        {
            RectangleF patternRoiAftZoomF;
            Rectangle  patternRoiAftZoom;
            Rectangle  patternRoiInWindow;

            patternRoiAftZoomF = new RectangleF(PatternRoi.X * zoomRatio, PatternRoi.Y * zoomRatio,
                                                PatternRoi.Width * zoomRatio, PatternRoi.Height * zoomRatio);
            patternRoiAftZoom  = Rectangle.Round(patternRoiAftZoomF);
            patternRoiInWindow = new Rectangle(patternRoiAftZoom.X - aftZoomRoiLeftUpPt.X, patternRoiAftZoom.Y - aftZoomRoiLeftUpPt.Y
                                               , patternRoiAftZoom.Width, patternRoiAftZoom.Height);

            if (new Rectangle(aftZoomRoiLeftUpPt, OwnerWindow.ClientSize).IntersectsWith(patternRoiAftZoom))
            {
                CvInvoke.Rectangle(image, patternRoiInWindow, color, 1);
                CvInvoke.Line(image, new Point(patternRoiInWindow.Left, patternRoiInWindow.Top + patternRoiInWindow.Height / 2)
                              , new Point(patternRoiInWindow.Right, patternRoiInWindow.Top + patternRoiInWindow.Height / 2), color, 1);
                CvInvoke.Line(image, new Point(patternRoiInWindow.Left + patternRoiInWindow.Width / 2, patternRoiInWindow.Top)
                              , new Point(patternRoiInWindow.Right - patternRoiInWindow.Width / 2, patternRoiInWindow.Bottom), color, 1);
                CvInvoke.Line(image, new Point(patternRoiInWindow.Left, patternRoiInWindow.Top)
                              , new Point(patternRoiInWindow.Right, patternRoiInWindow.Bottom), color, 1);
                CvInvoke.Line(image, new Point(patternRoiInWindow.Left, patternRoiInWindow.Bottom)
                              , new Point(patternRoiInWindow.Right, patternRoiInWindow.Top), color, 1);
            }
            return(image);
        }
        private void FindFaceFeatures()
        {
            Marshal.Copy(grayFrame.DataPointer, grayBytes, 0, img_size);
            FindFaceEyesIrisAsync().ContinueWith((tsk) =>
            {
                RetrieveResult retrieveResult = tsk.Result;
                if (retrieveResult == null)
                {
                    Dispatcher.BeginInvoke((Action)(() =>
                    {
                        BitmapSource grayImage = BitmapSource.Create(
                            img_width, img_height,
                            96.0, 96.0, PixelFormats.Gray8,
                            new BitmapPalette(new System.Windows.Media.Color[] { System.Windows.Media.Color.FromRgb(0, 0, 0), System.Windows.Media.Color.FromRgb(255, 255, 255) }),
                            grayBytes, img_width);
                        resultWindow.SetResult(grayImage, grayImage);
                        lock (_locker)
                        {
                            isReady = true;
                            if ((capture.Grab() == false))
                            {
                                App.Current.Shutdown();
                            }
                        }
                    }), DispatcherPriority.Render);
                    return;
                }

                FaceRetrieveResult faceRetrieveResult   = retrieveResult.Face;
                EyeRetrieveResult eyeRetrieveResult     = retrieveResult.Eye;
                PupilRetrieveResult pupilRetrieveResult = retrieveResult.Pupil;
                Emgu.CV.Structure.MCvScalar whiteColor  = new Emgu.CV.Structure.MCvScalar(100, 255, 255);
                Emgu.CV.Structure.MCvScalar redColor    = new Emgu.CV.Structure.MCvScalar(100, 255, 100);
                if (faceRetrieveResult.HasFace)
                {
                    RECT face = faceRetrieveResult.Face;
                    CvInvoke.Rectangle(cameraFrame, face, whiteColor, 3);
                    if (eyeRetrieveResult.HasLeftEye)
                    {
                        RECT leftEye     = eyeRetrieveResult.LeftEye;
                        CIRCLE leftPupil = pupilRetrieveResult.LeftPupil;
                        CvInvoke.Rectangle(cameraFrame, leftEye, whiteColor, 3);
                        CvInvoke.Circle(cameraFrame, leftPupil.Center, leftPupil.Radius, redColor, 2);
                    }
                    if (eyeRetrieveResult.HasRightEye)
                    {
                        RECT rightEye     = eyeRetrieveResult.RightEye;
                        CIRCLE rightPupil = pupilRetrieveResult.RightPupil;
                        CvInvoke.Rectangle(cameraFrame, rightEye, whiteColor, 3);
                        CvInvoke.Circle(cameraFrame, rightPupil.Center, rightPupil.Radius, redColor, 2);
                    }
                }

                Dispatcher.BeginInvoke((Action)(() =>
                {
                    OnFeatureRead(eyeRetrieveResult, pupilRetrieveResult);
                    BitmapSource grayImage = BitmapSource.Create(
                        img_width, img_height,
                        96.0, 96.0, PixelFormats.Gray8,
                        new BitmapPalette(new System.Windows.Media.Color[] { System.Windows.Media.Color.FromRgb(0, 0, 0), System.Windows.Media.Color.FromRgb(255, 255, 255) }),
                        grayBytes, img_width);
                    BitmapSource resultImage = Bitmap2BitmapImage(cameraFrame.Bitmap);
                    resultWindow.SetResult(grayImage, resultImage);

                    lock (_locker)
                    {
                        isReady = true;
                        if ((capture.Grab() == false))
                        {
                            App.Current.Shutdown();
                        }
                    }
                }), DispatcherPriority.Render);
            });
        }