public void water_test() { Bitmap water = Accord.Imaging.Image.Clone(Resources.water); var dt = new DistanceTransform(); Bitmap result = dt.Apply(water); Assert.AreEqual(111.364265, dt.MaximumDistance, 1e-5); Assert.AreEqual(new IntPoint(436, 310), dt.UltimateErodedPoint); Assert.IsNotNull(result); float[] distance1D; new ImageToArray().Convert(result, out distance1D); float[][] distance; new ImageToMatrix().Convert(result, out distance); Assert.AreEqual(distance1D.Sum(), distance.Sum()); float[][] distances2 = Jagged.Reshape(dt.Pixels, result.Height, result.Width); float actual = distances2[84][533]; float expected = 1.4142135f; Assert.AreEqual(expected, actual); }
public void ApplyTest1() { string localPath = Path.Combine(NUnit.Framework.TestContext.CurrentContext.TestDirectory, "distance"); Directory.CreateDirectory(localPath); // Bitmap cards = new TestImages().GetImage("cards.jpg"); Bitmap cards = Accord.Imaging.Image.Clone(Resources.cards); cards.Save(Path.Combine(localPath, "input.jpg")); Bitmap grayCards = Grayscale.CommonAlgorithms.BT709.Apply(cards); grayCards.Save(Path.Combine(localPath, "grayscale.jpg")); Bitmap binaryCards = new Threshold(200).Apply(grayCards); binaryCards.Save(Path.Combine(localPath, "binary.jpg")); var dt = new DistanceTransform(); Bitmap result = dt.Apply(binaryCards); Assert.AreEqual(35.805027, dt.MaximumDistance, 1e-5); Assert.AreEqual(new IntPoint(254, 129), dt.UltimateErodedPoint); result.Save(Path.Combine(localPath, "distance_transform.jpg")); Assert.IsNotNull(result); }
private int[,] ComputeDistanceTransform(PointsSequence curve) { // create points transformed to the [0 .. MAX_RESOLUTION] range. var points = curve.Points.ToArray(); foreach (var i in Enumerable.Range(0, points.Length)) { points[i].X = 0.5 * Constants.DISTANCE_TRANSFORM_RESOLUTION * (points[i].X + 1); points[i].Y = 0.5 * Constants.DISTANCE_TRANSFORM_RESOLUTION * (points[i].Y + 1); } // close the curve, if needed if (curve is Polygon) { points = points.Concat(Enumerable.Repeat(points.First(), 1)).ToArray(); } // compute distance transform and return it double[,] transform = new double[Constants.DISTANCE_TRANSFORM_RESOLUTION, Constants.DISTANCE_TRANSFORM_RESOLUTION]; DistanceTransform.Compute(points, transform); int[,] result = new int[Constants.DISTANCE_TRANSFORM_RESOLUTION, Constants.DISTANCE_TRANSFORM_RESOLUTION]; for (int x = 0; x < Constants.DISTANCE_TRANSFORM_RESOLUTION; ++x) { for (int y = 0; y < Constants.DISTANCE_TRANSFORM_RESOLUTION; ++y) { result[x, y] = (int)Math.Round(16 * transform[x, y]); Debug.Assert(result[x, y] >= 0); } } return(result); }
public void ApplyTest1() { // Bitmap cards = new TestImages().GetImage("cards.jpg"); Bitmap cards = Accord.Imaging.Image.Clone(Resources.cards); cards.Save(@"c:\Temp\input.jpg"); Bitmap grayCards = Grayscale.CommonAlgorithms.BT709.Apply(cards); grayCards.Save(@"c:\Temp\grayscale.jpg"); Bitmap binaryCards = new Threshold(200).Apply(grayCards); binaryCards.Save(@"c:\Temp\binary.jpg"); var dt = new DistanceTransform(); Bitmap result = dt.Apply(binaryCards); Assert.AreEqual(35.805027, dt.MaximumDistance, 1e-5); Assert.AreEqual(new IntPoint(254, 129), dt.UltimateErodedPoint); result.Save(@"c:\Temp\distance_transform.jpg"); Assert.IsNotNull(result); }