Beispiel #1
0
 public double GetPercentageOfSkinInImage(string filePath, string debugImageFilePath = null)
 {
     using (IplImage imgSrc = new IplImage(filePath))
     {
         using (CvMemStorage storage = new CvMemStorage())
         {
             CvRect rect;
             using (IplImage imgGray = new IplImage(filePath, LoadMode.GrayScale))
             {
                 rect = FindBorder(imgGray);
             }
             if (rect.Width < imgSrc.Width * 0.10 || rect.Height < imgSrc.Height * 0.10)
             {
                 rect = new CvRect(0, 0, imgSrc.Width, imgSrc.Height);
             }
             using (var subImg = imgSrc.GetSubImage(rect))
             {
                 using (IplImage imgHueMask = new IplImage(subImg.Size, BitDepth.U8, 1))
                 {
                     CvAdaptiveSkinDetector detector = new CvAdaptiveSkinDetector(1, MorphingMethod.ErodeDilate);
                     detector.Process(subImg, imgHueMask);
                     int count   = CountSkinPoints(imgHueMask, CvColor.White);
                     var percent = ((double)count / (double)(subImg.Width * subImg.Height) * 100);
                     if (debugImageFilePath != null)
                     {
                         imgHueMask.SaveImage(debugImageFilePath);
                     }
                     return(percent);
                 }
             }
         }
     }
 }
        public DiffContainer GetDiffs(Bitmap newFrame, int compressRate)
        {
            IplImage Frame = BitmapConverter.ToIplImage(newFrame);

            if (_oldImage == null || _oldImage.Height != newFrame.Height)
            {
                _oldImage = new Bitmap(newFrame.Width, newFrame.Height);
            }
            IplImage Previous_Frame = BitmapConverter.ToIplImage(_oldImage);

            IplImage Difference = new IplImage(Previous_Frame.Width, Previous_Frame.Height, BitDepth.U8, 4);

            Cv.AbsDiff(Frame, Previous_Frame, Difference);

            IplImage gray = new IplImage(Previous_Frame.Width, Previous_Frame.Height, BitDepth.U8, 1);

            Cv.CvtColor(Difference, gray, ColorConversion.RgbToGray);

            DiffContainer container = new DiffContainer();

            CvSeq <CvPoint> contours;
            CvSeq <CvPoint> contour;

            using (CvMemStorage storage = new CvMemStorage())
                for (int i = gray.FindContours(storage, out contours, CvContour.SizeOf, ContourRetrieval.External, ContourChain.ApproxSimple); contours != null; contours = contours.HNext)
                {
                    contour = Cv.ApproxPoly(contours, CvContour.SizeOf, storage, ApproxPolyMethod.DP, 0.2);

                    if (contour.ContourPerimeter() > 100 &&
                        contour.ContourPerimeter() < 10000
                        )
                    {
                        var r = contour.BoundingRect();


                        var im = Frame.GetSubImage(r);



                        Rectangle rect = new Rectangle(r.X, r.Y, r.Width, r.Height);


                        rect.X      -= 1;
                        rect.Y      -= 1;
                        rect.Width  += 2;
                        rect.Height += 2;


                        var j = BitmapConverter.ToBitmap(im);
                        container.Data.Add(rect, j);
                    }
                }
            UpdateOldFrame(container);


            return(container);
        }
Beispiel #3
0
        static IplImage GetCountours(IplImage src)
        {
            CvMemStorage    mem          = Cv.CreateMemStorage(0);
            CvSeq <CvPoint> firstContour = null;

            IplImage dst = GetThresholdImage(src);

            dst = GetThresholdImage(dst);

            int count = dst.FindContours(mem, out firstContour);

            //src.DrawContours(firstContour, CvColor.Green, CvColor.Blue, 2);
            //src.DrawRect(firstContour.BoundingRect(), CvColor.Red);

            src = src.GetSubImage(firstContour.BoundingRect());

            mem.Dispose();
            firstContour.Dispose();
            dst.Dispose();

            return(src);
        }
Beispiel #4
0
        public IplImage getFaceImage(IplImage image)
        {
            Mat mat   = new Mat(image);
            var faces = cascade.DetectMultiScale(mat);

            if (faces.Length > 0)
            {
                int id      = 0;
                int maxArea = 0;
                for (int i = 0; i < faces.Length; i++)
                {
                    if (faces[i].Width * faces[i].Height > maxArea)
                    {
                        id      = i;
                        maxArea = faces[i].Width * faces[i].Height;
                    }
                }

                image = image.GetSubImage(faces[id]);
            }

            return(image);
        }
        private FaceImage FaceDetect(FaceImage faceImage)
        {
            if (faceImage.FullName == null || File.Exists(faceImage.FullName) == false || faceImage.FullName == string.Empty)
            {
                throw new ArgumentException("Image's path is not valid: {0}.", faceImage.FullName);
            }

            var facesDir = CreateDirByShortName(Strings.Face_Database_Folder_Name);

            var fullName = CreateFaceFileFullName(faceImage, facesDir);

            faceImage.FullName = fullName;

            if (File.Exists(fullName))
            {
                return(faceImage);
            }

            try
            {
                using (IplImage img = new IplImage(faceImage.FullName, LoadMode.AnyColor))
                {
                    using (IplImage smallImg = new IplImage(new CvSize(Cv.Round(img.Width / Scale), Cv.Round(img.Height / Scale)), BitDepth.U8, 1))
                    {
                        using (IplImage gray = new IplImage(img.Size, BitDepth.U8, 1))
                        {
                            Cv.CvtColor(img, gray, ColorConversion.BgrToGray);
                            Cv.Resize(gray, smallImg, Interpolation.Linear);
                            Cv.EqualizeHist(smallImg, smallImg);
                        }

                        //using (CvHaarClassifierCascade cascade = Cv.Load<CvHaarClassifierCascade>(Const.XmlHaarcascade))
                        using (CvHaarClassifierCascade cascade = CvHaarClassifierCascade.FromFile(Constants.FaceCascadeDefFileName))
                            using (CvMemStorage storage = new CvMemStorage())
                            {
                                storage.Clear();

                                Stopwatch         watch = Stopwatch.StartNew();
                                CvSeq <CvAvgComp> faces = Cv.HaarDetectObjects(smallImg, cascade, storage, /*1.2*/ ScaleFactor, MinNeighbors, 0, new CvSize(30, 30));//(30,30)
                                watch.Stop();

                                DetectionTime = watch.ElapsedMilliseconds; //detection time (ms)

                                for (int i = 0; i < faces.Total; i++)
                                {
                                    CvRect r = faces[i].Value.Rect;
                                    //CvPoint center = new CvPoint
                                    //{
                                    //    X = Cv.Round((r.X + r.Width * 0.5) * Scale),
                                    //    Y = Cv.Round((r.Y + r.Height * 0.5) * Scale)
                                    //};
                                    //int radius = Cv.Round((r.Width + r.Height) * 0.25 * Scale);
                                    //img.Circle(center, radius, new CvColor(0, 0, 255), 3, LineType.AntiAlias, 0);

                                    smallImg.ROI = new CvRect(r.X, r.Y, r.Width, r.Height);

                                    IplImage subImage = smallImg.GetSubImage(smallImg.ROI);

                                    _face = BitmapConverter.ToBitmap(subImage);

                                    FaceImage newFaceImage = SaveFace(faceImage);
                                    return(newFaceImage);
                                }
                            }
                    }
                }
            }
            catch (Exception ex)
            {
                bool rethrow = ExceptionPolicy.HandleException(ex, "BusinessLogin Policy");
                if (rethrow)
                {
                    throw;
                }

                MessageBox.Show(string.Format("Failed to separate face from image"));
            }
            return(null);
        }