Пример #1
0
        private unsafe bool FindEyesFromDifferenceImage(FastBitmap img, FastBitmap differenceImage, out EyeData leftEye, out EyeData rightEye)
        {
            leftEye  = null;
            rightEye = null;

            if (thresholdElapsedTime < 0)
            {
                thresholdElapsedTime = 0;
                stopwatch.Reset();
                stopwatch.Start();
            }
            else
            {
                thresholdElapsedTime += stopwatch.ElapsedMilliseconds / 1000f;
                stopwatch.Reset();
                stopwatch.Start();
            }

            FastBitmap.ConnectedComponentsResults ccResults = differenceImage.FindConnectedComponents(100, 2, 10);


            if (ccResults.NumberOfComponents >= 2 && ccResults.NumberOfComponents <= MaxNumberOfConnectedComponents)
            {
                LinkedList <BlinkVector> vectors = GetBlinkVectors(ccResults);

                if (vectors.Count > 0)
                {
                    float       minDistanceSquared = float.PositiveInfinity;
                    BlinkVector bestVector         = null;
                    float       mahalanobisDistanceSquaredThreshold = MahalanobisDistanceSquaredThreshold;
                    float       minimumNcc = MinimumNcc;

                    foreach (BlinkVector v in vectors)
                    {
                        float tempDistSquared = v.GetMahalanobisDistanceSquared(AverageVector, InverseCovarianceMatrix);
                        if (tempDistSquared < minDistanceSquared)
                        {
                            bestVector         = v;
                            minDistanceSquared = tempDistSquared;
                        }
                    }

                    if (minDistanceSquared <= mahalanobisDistanceSquaredThreshold)
                    {
                        try
                        {
                            float ncc = HorizontalFlipNccTemplateMatch(img, ccResults.Centers[bestVector.LeftEyeLabel], ccResults.Centers[bestVector.RightEyeLabel]);

                            if (ncc > minimumNcc)
                            {
                                leftEye  = new EyeData(ccResults.Centers[bestVector.LeftEyeLabel], ccResults.BoundingBoxes[bestVector.LeftEyeLabel]);
                                rightEye = new EyeData(ccResults.Centers[bestVector.RightEyeLabel], ccResults.BoundingBoxes[bestVector.RightEyeLabel]);
                                return(true);
                            }
                            return(false);
                        }
                        catch (ArgumentOutOfRangeException) { }
                    }
                }
            }
            return(false);
        }