private static bool PossibleEyeVector(int leftEyeLabel, int rightEyeLabel, FastBitmap.ConnectedComponentsResults ccResults) { float sizeRatio = ((float)ccResults.Sizes[leftEyeLabel]) / ((float)ccResults.Sizes[rightEyeLabel]); if (sizeRatio > SizeRatioThreadshold || sizeRatio < (1f / SizeRatioThreadshold)) { return(false); } Point leftEyeLoc = ccResults.Centers[leftEyeLabel]; Point rightEyeLoc = ccResults.Centers[rightEyeLabel]; Point DiffVec = new Point(leftEyeLoc.X - rightEyeLoc.X, leftEyeLoc.Y - rightEyeLoc.Y); if (DiffVec.X == 0) { return(false); } if (Math.Abs(DiffVec.Y) / Math.Abs(DiffVec.X) > DisplacementRatioThreshold) { return(false); } return(true); }
public BlinkVector(int leftEyeLabel, int rightEyeLabel, FastBitmap.ConnectedComponentsResults ccResults) { this.leftEyeLabel = leftEyeLabel; this.rightEyeLabel = rightEyeLabel; elements = new float[10]; Point leftEyeLoc = ccResults.Centers[leftEyeLabel]; Point rightEyeLoc = ccResults.Centers[rightEyeLabel]; Point DiffVec = new Point(leftEyeLoc.X - rightEyeLoc.X, leftEyeLoc.Y - rightEyeLoc.Y); float distance = (float)Math.Sqrt((DiffVec.X * DiffVec.X) + (DiffVec.Y * DiffVec.Y)); /* * elements[0] = leftArea / distance; * elements[1] = rightArea / distance; * elements[2] = yDisplacement / xDisplacement; * elements[3] = distance; * elements[4] = (leftYMax - leftYMin) / distance; * elements[5] = (leftXMax - leftXMin) / distance; * elements[6] = (rightYMax - rightYMin) / distance; * elements[7] = (rightXMax - rightXMin) / distance; * elements[8] = elements[4] / elements[5]; * elements[9] = elements[6] / elements[7]; */ elements[0] = ccResults.Sizes[leftEyeLabel] / distance; elements[1] = ccResults.Sizes[rightEyeLabel] / distance; if (DiffVec.X == 0) { elements[2] = float.PositiveInfinity; } else { elements[2] = ((float)Math.Abs(DiffVec.Y)) / ((float)Math.Abs(DiffVec.X)); } elements[3] = distance; elements[4] = (ccResults.BoundingBoxes[leftEyeLabel].Height) / distance; elements[5] = (ccResults.BoundingBoxes[leftEyeLabel].Width) / distance; elements[6] = (ccResults.BoundingBoxes[rightEyeLabel].Height) / distance; elements[7] = (ccResults.BoundingBoxes[rightEyeLabel].Width) / distance; elements[8] = elements[4] / elements[5]; elements[9] = elements[6] / elements[7]; }
private static LinkedList <BlinkVector> GetBlinkVectors(FastBitmap.ConnectedComponentsResults ccResults) { LinkedList <BlinkVector> vectors = new LinkedList <BlinkVector>(); for (int i = 0; i < ccResults.NumberOfComponents; ++i) { for (int j = i + 1; j < ccResults.NumberOfComponents; ++j) { int leftEyeLabel = ccResults.Centers[i].X < ccResults.Centers[j].X ? i : j; int rightEyeLabel = leftEyeLabel == i ? j : i; if (PossibleEyeVector(leftEyeLabel, rightEyeLabel, ccResults)) { vectors.AddLast(new BlinkVector(leftEyeLabel, rightEyeLabel, ccResults)); } } } return(vectors); }
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); }