From 577ca7b4367b26f64c226915e06807af752de0b9 Mon Sep 17 00:00:00 2001 From: dirk Date: Fri, 21 Oct 2016 22:03:53 +0200 Subject: [PATCH] Use jagged instead of multidimensional array as suggested in #462. Only create the kernels once in the filters. Former-commit-id: 4df56763ba8b568b5880f85b2fe1147b39ecae4f Former-commit-id: 609592f6cbd8bd084f05b3e510697edd204d2aec Former-commit-id: 3f6ccfb3c69420d39b5bd62080e7be54fad17a97 --- .../Convolution/Convolution2DFilter.cs | 28 +++++++++---------- .../EdgeDetection/EdgeDetector2DFilter.cs | 4 +-- .../EdgeDetection/KayyaliProcessor.cs | 24 +++++++++------- .../EdgeDetection/PrewittProcessor.cs | 24 +++++++++------- .../EdgeDetection/RobertsCrossProcessor.cs | 20 +++++++------ .../EdgeDetection/ScharrProcessor.cs | 24 +++++++++------- .../EdgeDetection/SobelProcessor.cs | 24 +++++++++------- 7 files changed, 83 insertions(+), 65 deletions(-) diff --git a/src/ImageProcessorCore/Samplers/Processors/Convolution/Convolution2DFilter.cs b/src/ImageProcessorCore/Samplers/Processors/Convolution/Convolution2DFilter.cs index 51cbcdf28..3e33b62a6 100644 --- a/src/ImageProcessorCore/Samplers/Processors/Convolution/Convolution2DFilter.cs +++ b/src/ImageProcessorCore/Samplers/Processors/Convolution/Convolution2DFilter.cs @@ -23,7 +23,7 @@ namespace ImageProcessorCore.Processors /// /// The horizontal gradient operator. /// The vertical gradient operator. - public Convolution2DFilter(float[,] kernelX, float[,] kernelY) + public Convolution2DFilter(float[][] kernelX, float[][] kernelY) { this.KernelX = kernelX; this.KernelY = kernelY; @@ -32,22 +32,20 @@ namespace ImageProcessorCore.Processors /// /// Gets the horizontal gradient operator. /// - public float[,] KernelX { get; } + public float[][] KernelX { get; } /// /// Gets the vertical gradient operator. /// - public float[,] KernelY { get; } + public float[][] KernelY { get; } /// public override void Apply(ImageBase target, ImageBase source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY) { - float[,] kernelX = this.KernelX; - float[,] kernelY = this.KernelY; - int kernelYHeight = kernelY.GetLength(0); - int kernelYWidth = kernelY.GetLength(1); - int kernelXHeight = kernelX.GetLength(0); - int kernelXWidth = kernelX.GetLength(1); + int kernelYHeight = KernelY.Length; + int kernelYWidth = KernelY[0].Length; + int kernelXHeight = KernelX.Length; + int kernelXWidth = KernelX[0].Length; int radiusY = kernelYHeight >> 1; int radiusX = kernelXWidth >> 1; @@ -100,16 +98,16 @@ namespace ImageProcessorCore.Processors if (fy < kernelXHeight) { - rX += kernelX[fy, fx] * r; - gX += kernelX[fy, fx] * g; - bX += kernelX[fy, fx] * b; + rX += KernelX[fy][fx] * r; + gX += KernelX[fy][fx] * g; + bX += KernelX[fy][fx] * b; } if (fx < kernelYWidth) { - rY += kernelY[fy, fx] * r; - gY += kernelY[fy, fx] * g; - bY += kernelY[fy, fx] * b; + rY += KernelY[fy][fx] * r; + gY += KernelY[fy][fx] * g; + bY += KernelY[fy][fx] * b; } } } diff --git a/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/EdgeDetector2DFilter.cs b/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/EdgeDetector2DFilter.cs index d39bcaba6..b22832232 100644 --- a/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/EdgeDetector2DFilter.cs +++ b/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/EdgeDetector2DFilter.cs @@ -18,12 +18,12 @@ namespace ImageProcessorCore.Processors /// /// Gets the horizontal gradient operator. /// - public abstract float[,] KernelX { get; } + public abstract float[][] KernelX { get; } /// /// Gets the vertical gradient operator. /// - public abstract float[,] KernelY { get; } + public abstract float[][] KernelY { get; } /// public bool Grayscale { get; set; } diff --git a/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs b/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs index a7d31a607..1b91615d6 100644 --- a/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs +++ b/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs @@ -15,20 +15,24 @@ namespace ImageProcessorCore.Processors where TColor : IPackedVector where TPacked : struct { - /// - public override float[,] KernelX => new float[,] + private static readonly float[][] kernelX = new float[3][] { - { 6, 0, -6 }, - { 0, 0, 0 }, - { -6, 0, 6 } + new float[] { 6, 0, -6 }, + new float[] { 0, 0, 0 }, + new float[] { -6, 0, 6 } }; - /// - public override float[,] KernelY => new float[,] + private static readonly float[][] kernelY = new float[3][] { - { -6, 0, 6 }, - { 0, 0, 0 }, - { 6, 0, -6 } + new float[] { -6, 0, 6 }, + new float[] { 0, 0, 0 }, + new float[] { 6, 0, -6 } }; + + /// + public override float[][] KernelX => kernelX; + + /// + public override float[][] KernelY => kernelY; } } diff --git a/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/PrewittProcessor.cs b/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/PrewittProcessor.cs index 897ba212e..17e73e7b9 100644 --- a/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/PrewittProcessor.cs +++ b/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/PrewittProcessor.cs @@ -15,20 +15,24 @@ namespace ImageProcessorCore.Processors where TColor : IPackedVector where TPacked : struct { - /// - public override float[,] KernelX => new float[,] + private static readonly float[][] kernelX = new float[3][] { - { -1, 0, 1 }, - { -1, 0, 1 }, - { -1, 0, 1 } + new float[] { -1, 0, 1 }, + new float[] { -1, 0, 1 }, + new float[] { -1, 0, 1 } }; - /// - public override float[,] KernelY => new float[,] + private static readonly float[][] kernelY = new float[3][] { - { 1, 1, 1 }, - { 0, 0, 0 }, - { -1, -1, -1 } + new float[] { 1, 1, 1 }, + new float[] { 0, 0, 0 }, + new float[] { -1, -1, -1 } }; + + /// + public override float[][] KernelX => kernelX; + + /// + public override float[][] KernelY => kernelY; } } \ No newline at end of file diff --git a/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs b/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs index 69c5abe34..6167e54bc 100644 --- a/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs +++ b/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs @@ -15,18 +15,22 @@ namespace ImageProcessorCore.Processors where TColor : IPackedVector where TPacked : struct { - /// - public override float[,] KernelX => new float[,] + private static readonly float[][] kernelX = new float[2][] { - { 1, 0 }, - { 0, -1 } + new float[] { 1, 0 }, + new float[] { 0, -1 } }; - /// - public override float[,] KernelY => new float[,] + private static readonly float[][] kernelY = new float[2][] { - { 0, 1 }, - { -1, 0 } + new float[] { 0, 1 }, + new float[] { -1, 0 } }; + + /// + public override float[][] KernelX => kernelX; + + /// + public override float[][] KernelY => kernelY; } } diff --git a/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/ScharrProcessor.cs b/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/ScharrProcessor.cs index f860d7cfb..cbae93c40 100644 --- a/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/ScharrProcessor.cs +++ b/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/ScharrProcessor.cs @@ -15,20 +15,24 @@ namespace ImageProcessorCore.Processors where TColor : IPackedVector where TPacked : struct { - /// - public override float[,] KernelX => new float[,] + private static readonly float[][] kernelX = new float[3][] { - { -3, 0, 3 }, - { -10, 0, 10 }, - { -3, 0, 3 } + new float[] { -3, 0, 3 }, + new float[] { -10, 0, 10 }, + new float[] { -3, 0, 3 } }; - /// - public override float[,] KernelY => new float[,] + private static readonly float[][] kernelY = new float[3][] { - { 3, 10, 3 }, - { 0, 0, 0 }, - { -3, -10, -3 } + new float[] { 3, 10, 3 }, + new float[] { 0, 0, 0 }, + new float[] { -3, -10, -3 } }; + + /// + public override float[][] KernelX => kernelX; + + /// + public override float[][] KernelY => kernelY; } } \ No newline at end of file diff --git a/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/SobelProcessor.cs b/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/SobelProcessor.cs index d7f269178..4e657cb82 100644 --- a/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/SobelProcessor.cs +++ b/src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/SobelProcessor.cs @@ -15,20 +15,24 @@ namespace ImageProcessorCore.Processors where TColor : IPackedVector where TPacked : struct { - /// - public override float[,] KernelX => new float[,] + private static readonly float[][] kernelX = new float[3][] { - { -1, 0, 1 }, - { -2, 0, 2 }, - { -1, 0, 1 } + new float[] { -1, 0, 1 }, + new float[] { -2, 0, 2 }, + new float[] { -1, 0, 1 } }; - /// - public override float[,] KernelY => new float[,] + private static readonly float[][] kernelY = new float[3][] { - { -1, -2, -1 }, - { 0, 0, 0 }, - { 1, 2, 1 } + new float[] { -1, -2, -1 }, + new float[] { 0, 0, 0 }, + new float[] { 1, 2, 1 } }; + + /// + public override float[][] KernelX => kernelX; + + /// + public override float[][] KernelY => kernelY; } }