public void Gauss_Test() { var sourceBitmap = Samples.sample02; var source = sourceBitmap.ToBytes(); var width = sourceBitmap.Width; var height = sourceBitmap.Height; var result = new byte[source.Length]; UnoptimizedAlgorithms.Gauss(width, height, source, result); Run("samples/sample02.png"); result.RunAs(width, height, 4, "gauss.png"); }
public void NonMaximumSuppression_Test() { var sourceBitmap = Samples.sample03; var source = sourceBitmap.ToBytes(); var channels = 4; var width = sourceBitmap.Width; var height = sourceBitmap.Height; var useLarge = true; var swtEdgeOnColorChange = true; var swtEdgeColorTolerance = 50; var swtConnectByColor = false; var varianceTolerance = 2.0f; var sizeRatioTolerance = 10; var n = height * width; var grayscaleEq = new byte[n]; var background = new byte[n * channels]; var bgWindowSize = 7; UnoptimizedAlgorithms.GrayscaleEq(width, height, source, grayscaleEq); UnoptimizedAlgorithms.Background(width, height, source, channels, grayscaleEq, background, bgWindowSize); source.RunAs(width, height, channels, $"source.png"); //background.RunAs(width, height, channels, $"background{bgWindowSize}.png"); byte[] large = null; if (useLarge) { large = new byte[n * 4 * channels]; UnoptimizedAlgorithms.Enlarge2(width, height, source, large, channels); width *= 2; height *= 2; n = height * width; } var src = useLarge ? large : source; var gauss = new byte[n * channels]; var grayscale = new byte[n]; var gradient = new float[n]; var dx = new float[n]; var dy = new float[n]; var angle = new float[n]; var neighbors = new byte[n * 5]; var nms = new float[n]; var cmp0 = new float[n]; var cmp1 = new float[n]; var swt0 = new int[n]; var swt1 = new int[n]; Array.Fill(swt0, int.MaxValue); Array.Fill(swt1, int.MaxValue); UnoptimizedAlgorithms.Grayscale(width, height, src, grayscale); UnoptimizedAlgorithms.Gauss(width, height, src, gauss); UnoptimizedAlgorithms.Sobel3(width, height, gauss, dx, dy, gradient, angle, neighbors); UnoptimizedAlgorithms.NonMaximumSuppression(width, height, gradient, angle, neighbors, nms, cmp0, cmp1); UnoptimizedAlgorithms.StrokeWidthTransform(width, height, src, gradient, nms, angle, dx, dy, swt0, swt1, sourceChannels: 4, rayLength: 30, colorDifference: swtEdgeColorTolerance, useStrokeColor: swtEdgeOnColorChange); var coloring0 = new int[height * width]; var coloring1 = new int[height * width]; UnoptimizedAlgorithms.PrepareComponents(swt0, coloring0); UnoptimizedAlgorithms.PrepareComponents(swt1, coloring1); var colorRounds0 = UnoptimizedAlgorithms.ColorComponentsFixedPointBackPropagation( width, height, swt0, coloring0); var colorRounds1 = UnoptimizedAlgorithms.ColorComponentsFixedPointBackPropagation( width, height, swt1, coloring1); var componentIndexByColoring0 = new int[n]; var componentIndexByColoring1 = new int[n]; var componentsLimit = 100000; var componentSizeLimit = 1024; var cn = componentsLimit * componentSizeLimit; var componentItems0 = new int[cn]; var componentItems1 = new int[cn]; var componentSwtItems0 = new int[cn]; var componentSwtItems1 = new int[cn]; var components0 = new Component[componentsLimit]; var components1 = new Component[componentsLimit]; var regionCount0 = UnoptimizedAlgorithms.ComponentAnalysis( width, height, src, swt0, coloring0, componentIndexByColoring0, componentItems0, componentSwtItems0, components0, componentsLimit, componentSizeLimit, sourceChannels: channels); var regionCount1 = UnoptimizedAlgorithms.ComponentAnalysis( width, height, src, swt1, coloring1, componentIndexByColoring1, componentItems1, componentSwtItems0, components1, componentsLimit, componentSizeLimit, sourceChannels: channels); UnoptimizedAlgorithms.ColorComponentsFixedPointByColorSimilarity( width, height, swt0, coloring0, src, background, channels, componentIndexByColoring0, components0, 30, useLarge); UnoptimizedAlgorithms.ColorComponentsFixedPointByColorSimilarity( width, height, swt1, coloring1, src, background, channels, componentIndexByColoring1, components1, 30, useLarge); // Filter components 1st pass //var valid0 = Algorithms.PassiveFilter(regionCount0, componentList0); //var valid1 = Algorithms.PassiveFilter(regionCount1, componentList1); //var invalid0 = regionCount0 - valid0; //var invalid1 = regionCount1 - valid1; //Console.WriteLine($"Components BoW: {valid0}/{invalid0}"); //Console.WriteLine($"Components WoB: {valid1}/{invalid1}"); //var rtree0 = Algorithms.ComponentRBush(regionCount0, componentList0); //var rtree1 = Algorithms.ComponentRBush(regionCount1, componentList1); //Algorithms.MergeComponents(width, height, regionCount0, componentList0, rtree0); //Algorithms.MergeComponents(width, height, regionCount1, componentList1, rtree1); //Algorithms.RemoveBoxes(width, height, regionCount0, componentList0, rtree0); //Algorithms.RemoveBoxes(width, height, regionCount1, componentList1, rtree1); if (useLarge) { large.RunAs(width, height, channels, "large.png"); //large.RunAsText(width, height, channels, "large.txt"); } //gauss.RunAs(width, height, channels, "gauss.png"); //grayscale.RunAs(width, height, 1, "gray.png"); //grayscale.ReplaceEquals(255, 0).RunAsText(width, height, 1, "gray.txt"); //gradient.RunAs(width, height, 1, "gradient.png"); gradient.RunAsText(width, height, 1, "gradient.txt"); //angle.RunAsText(width, height, 1, "angle.txt"); //nms.RunAs(width, height, 1, "nms.png"); nms.RunAsText(width, height, 1, "nms.txt"); swt0.RunAs(width, height, 1, "swt0.png"); swt0.ReplaceEquals(int.MaxValue, 0).MultiplyBy(10).RunAsText(width, height, 1, "swt0.txt"); swt1.RunAs(width, height, 1, "swt1.png"); swt1.ReplaceEquals(int.MaxValue, 0).MultiplyBy(10).RunAsText(width, height, 1, "swt1.txt"); coloring0.RunAs(width, height, 1, "cc0.png"); coloring0.ReplaceGreaterOrEquals(n, 0).RunAsText(width, height, 1, "cc0.txt"); coloring1.RunAs(width, height, 1, "cc1.png"); coloring1.ReplaceGreaterOrEquals(n, 0).RunAsText(width, height, 1, "cc1.txt"); var text0 = new int[n]; var text1 = new int[n]; for (var i = 0; i < n; i++) { var color = coloring0[i]; if (color >= n) { text0[i] = 255; continue; } var ci = componentIndexByColoring0[color]; if (color >= n || ci == -1) { text0[i] = 255; continue; } ref var c = ref components0[ci]; if (!c.IsValid()) { text0[i] = c.Validity; } }