public Point CalculateVelocityUsingSSDMetic(UnmanagedImage current, UnmanagedImage previous, IntPoint point) { ComparisonMetric SSDMetric = ComparisonMetric.SumOfSquaresDifferences; Size areaSize = new Size(_blockWidth, _blockHeight); // centering block IntPoint areaLocationPrev = point - new IntPoint(_blockWidth >> 1, _blockHeight >> 1); IntPoint optimalDelta = new IntPoint(0, 0); float minSSD = float.MaxValue; float maxSSD = float.MinValue; for (int dx = -_maxMoveXRadius; dx <= _maxMoveXRadius; dx += _shiftX) { for (int dy = -_maxMoveYRadius; dy <= _maxMoveYRadius; dy += _shiftY) { IntPoint delta = new IntPoint(dx, dy); IntPoint areaLocationCur = areaLocationPrev + delta; // checking position if (areaLocationCur.X < 0 || areaLocationCur.Y < 0 || areaLocationCur.X + areaSize.Width > current.Width || areaLocationCur.Y + areaSize.Height > current.Height) { continue; } float SSD = SSDMetric.Calculate(current, previous, areaSize, areaLocationCur, areaLocationPrev); if (SSD > maxSSD) { maxSSD = SSD; } if (SSD < minSSD) //|| metricValue == optimalMetric && delta.EuclideanNorm() < optimalDelta.EuclideanNorm()) { minSSD = SSD; optimalDelta = delta; } } } // improves the stability float reliability = maxSSD / (areaSize.Width * areaSize.Height * 255 * 255); if (reliability < 0.001f) { return(new IntPoint()); } return(optimalDelta); }
public unsafe void TrackFeature(UnmanagedImage image) { ComparisonMetric ncCalculator = ComparisonMetric.NormalizedCorrelation; float maxCorrelation = float.MinValue; IntPoint optimalLocation = new IntPoint(); for (int dx = -MaxVelocity; dx <= MaxVelocity; dx += 1) { for (int dy = -MaxVelocity; dy <= MaxVelocity; dy += 1) { IntPoint delta = new IntPoint(dx, dy); IntPoint newLocalation = Location + delta; // checking position if (newLocalation.X < 0 || newLocalation.Y < 0 || newLocalation.X + Size.Width > image.Width || newLocalation.Y + Size.Height > image.Height) { continue; } float nolmalizedCorrelation = ncCalculator.Calculate(image, FeatureImage, Size, newLocalation, new IntPoint(0, 0)); if (nolmalizedCorrelation > maxCorrelation) { maxCorrelation = nolmalizedCorrelation; optimalLocation = newLocalation; } } } // if success detection if (maxCorrelation > LostThreshold) { Accuracy = maxCorrelation; IsLost = false; Location = optimalLocation; // update feature image UpdateFeatureImage(image, Location); } else { IsLost = true; } }
public Point CalculateVelocityUsingNCMetic(UnmanagedImage current, UnmanagedImage previous, IntPoint point, out float normalizedCorrelation) { ComparisonMetric NCMetric = ComparisonMetric.NormalizedCorrelation; Size areaSize = new Size(_blockWidth, _blockHeight); // centering block IntPoint areaLocationPrev = point - new IntPoint(_blockWidth >> 1, _blockHeight >> 1); IntPoint optimalDelta = new IntPoint(0, 0); float maxCorrelation = float.MinValue; for (int dx = -_maxMoveXRadius; dx <= _maxMoveXRadius; dx += _shiftX) { for (int dy = -_maxMoveYRadius; dy <= _maxMoveYRadius; dy += _shiftY) { IntPoint delta = new IntPoint(dx, dy); IntPoint areaLocationCur = areaLocationPrev + delta; // checking position if (areaLocationCur.X < 0 || areaLocationCur.Y < 0 || areaLocationCur.X + areaSize.Width > current.Width || areaLocationCur.Y + areaSize.Height > current.Height) { continue; } float nolmalizedCorrelation = NCMetric.Calculate(current, previous, areaSize, areaLocationCur, areaLocationPrev); if (nolmalizedCorrelation > maxCorrelation) { maxCorrelation = nolmalizedCorrelation; optimalDelta = delta; } } } normalizedCorrelation = maxCorrelation; return(optimalDelta); }