From 08de2e30bd7319f78fea292d16bc7932eaec65a7 Mon Sep 17 00:00:00 2001 From: James Jackson-South Date: Thu, 11 Aug 2016 14:42:27 +1000 Subject: [PATCH] Clean upedge detection Former-commit-id: 3f5ad14a2724c33a3723986e5e5890d7339302dc Former-commit-id: f371e93de1c3ae0c144bc20c26cc49d0b34b5178 Former-commit-id: 9f3aea38493f3236544703c08560fa9dcdc47990 --- src/ImageProcessorCore/Filters/DetectEdges.cs | 67 +++++++++++++++---- .../Convolution/Convolution2DFilter.cs | 1 + .../Processors/Filters/DetectEdgesTest.cs | 25 +++++++ 3 files changed, 80 insertions(+), 13 deletions(-) diff --git a/src/ImageProcessorCore/Filters/DetectEdges.cs b/src/ImageProcessorCore/Filters/DetectEdges.cs index 33fa00d30..c4cee3b23 100644 --- a/src/ImageProcessorCore/Filters/DetectEdges.cs +++ b/src/ImageProcessorCore/Filters/DetectEdges.cs @@ -13,7 +13,7 @@ namespace ImageProcessorCore public static partial class ImageExtensions { /// - /// Detects any edges within the image. Uses the filter + /// Detects any edges within the image. Uses the filter /// operating in Grayscale mode. /// /// The pixel format. @@ -28,6 +28,25 @@ namespace ImageProcessorCore return DetectEdges(source, source.Bounds, new SobelProcessor { Grayscale = true }, progressHandler); } + /// + /// Detects any edges within the image. Uses the filter + /// operating in Grayscale mode. + /// + /// The pixel format. + /// The packed format. long, float. + /// The image this method extends. + /// + /// The structure that specifies the portion of the image object to alter. + /// + /// A delegate which is called as progress is made processing the image. + /// The . + public static Image DetectEdges(this Image source, Rectangle rectangle, ProgressEventHandler progressHandler = null) + where T : IPackedVector + where TP : struct + { + return DetectEdges(source, rectangle, new SobelProcessor { Grayscale = true }, progressHandler); + } + /// /// Detects any edges within the image. /// @@ -35,10 +54,30 @@ namespace ImageProcessorCore /// The packed format. long, float. /// The image this method extends. /// The filter for detecting edges. - /// Whether to convert the image to Grayscale first. Defaults to true. + /// Whether to convert the image to Grayscale first. Defaults to true. /// A delegate which is called as progress is made processing the image. /// The . - public static Image DetectEdges(this Image source, EdgeDetection filter, bool Grayscale = true, ProgressEventHandler progressHandler = null) + public static Image DetectEdges(this Image source, EdgeDetection filter, bool grayscale = true, ProgressEventHandler progressHandler = null) + where T : IPackedVector + where TP : struct + { + return DetectEdges(source, filter, source.Bounds, grayscale, progressHandler); + } + + /// + /// Detects any edges within the image. + /// + /// The pixel format. + /// The packed format. long, float. + /// The image this method extends. + /// The filter for detecting edges. + /// + /// The structure that specifies the portion of the image object to alter. + /// + /// Whether to convert the image to Grayscale first. Defaults to true. + /// A delegate which is called as progress is made processing the image. + /// The . + public static Image DetectEdges(this Image source, EdgeDetection filter, Rectangle rectangle, bool grayscale = true, ProgressEventHandler progressHandler = null) where T : IPackedVector where TP : struct { @@ -47,43 +86,43 @@ namespace ImageProcessorCore switch (filter) { case EdgeDetection.Kayyali: - processor = new KayyaliProcessor { Grayscale = Grayscale }; + processor = new KayyaliProcessor { Grayscale = grayscale }; break; case EdgeDetection.Kirsch: - processor = new KirschProcessor { Grayscale = Grayscale }; + processor = new KirschProcessor { Grayscale = grayscale }; break; case EdgeDetection.Lapacian3X3: - processor = new Laplacian3X3Processor { Grayscale = Grayscale }; + processor = new Laplacian3X3Processor { Grayscale = grayscale }; break; case EdgeDetection.Lapacian5X5: - processor = new Laplacian5X5Processor { Grayscale = Grayscale }; + processor = new Laplacian5X5Processor { Grayscale = grayscale }; break; case EdgeDetection.LaplacianOfGaussian: - processor = new LaplacianOfGaussianProcessor { Grayscale = Grayscale }; + processor = new LaplacianOfGaussianProcessor { Grayscale = grayscale }; break; case EdgeDetection.Prewitt: - processor = new PrewittProcessor { Grayscale = Grayscale }; + processor = new PrewittProcessor { Grayscale = grayscale }; break; case EdgeDetection.RobertsCross: - processor = new RobertsCrossProcessor { Grayscale = Grayscale }; + processor = new RobertsCrossProcessor { Grayscale = grayscale }; break; case EdgeDetection.Scharr: - processor = new ScharrProcessor { Grayscale = Grayscale }; + processor = new ScharrProcessor { Grayscale = grayscale }; break; default: - processor = new ScharrProcessor { Grayscale = Grayscale }; + processor = new ScharrProcessor { Grayscale = grayscale }; break; } - return DetectEdges(source, source.Bounds, processor, progressHandler); + return DetectEdges(source, rectangle, processor, progressHandler); } /// @@ -105,6 +144,8 @@ namespace ImageProcessorCore /// /// Detects any edges within the image. /// + /// The pixel format. + /// The packed format. long, float. /// The image this method extends. /// /// The structure that specifies the portion of the image object to alter. diff --git a/src/ImageProcessorCore/Filters/Processors/Convolution/Convolution2DFilter.cs b/src/ImageProcessorCore/Filters/Processors/Convolution/Convolution2DFilter.cs index 3b6edcf57..d586c3cd5 100644 --- a/src/ImageProcessorCore/Filters/Processors/Convolution/Convolution2DFilter.cs +++ b/src/ImageProcessorCore/Filters/Processors/Convolution/Convolution2DFilter.cs @@ -112,6 +112,7 @@ namespace ImageProcessorCore.Processors packed.PackFromVector4(new Vector4(red, green, blue, targetColor.Z)); targetPixels[x, y] = packed; } + this.OnRowProcessed(); } }); diff --git a/tests/ImageProcessorCore.Tests/Processors/Filters/DetectEdgesTest.cs b/tests/ImageProcessorCore.Tests/Processors/Filters/DetectEdgesTest.cs index 944413007..fde41b5d8 100644 --- a/tests/ImageProcessorCore.Tests/Processors/Filters/DetectEdgesTest.cs +++ b/tests/ImageProcessorCore.Tests/Processors/Filters/DetectEdgesTest.cs @@ -49,5 +49,30 @@ namespace ImageProcessorCore.Tests } } } + + [Theory] + [MemberData("DetectEdgesFilters")] + public void ImageShouldApplyDetectEdgesFilterInBox(EdgeDetection detector) + { + const string path = "TestOutput/DetectEdges"; + if (!Directory.Exists(path)) + { + Directory.CreateDirectory(path); + } + + foreach (string file in Files) + { + using (FileStream stream = File.OpenRead(file)) + { + string filename = Path.GetFileNameWithoutExtension(file) + "-" + detector + "-InBox" + Path.GetExtension(file); + Image image = new Image(stream); + using (FileStream output = File.OpenWrite($"{path}/{filename}")) + { + image.DetectEdges(detector, new Rectangle(image.Width / 4, image.Height / 4, image.Width / 2, image.Height / 2)) + .Save(output); + } + } + } + } } } \ No newline at end of file