Browse Source

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
pull/1/head
dirk 10 years ago
parent
commit
577ca7b436
  1. 28
      src/ImageProcessorCore/Samplers/Processors/Convolution/Convolution2DFilter.cs
  2. 4
      src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/EdgeDetector2DFilter.cs
  3. 24
      src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs
  4. 24
      src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/PrewittProcessor.cs
  5. 20
      src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs
  6. 24
      src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/ScharrProcessor.cs
  7. 24
      src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/SobelProcessor.cs

28
src/ImageProcessorCore/Samplers/Processors/Convolution/Convolution2DFilter.cs

@ -23,7 +23,7 @@ namespace ImageProcessorCore.Processors
/// </summary> /// </summary>
/// <param name="kernelX">The horizontal gradient operator.</param> /// <param name="kernelX">The horizontal gradient operator.</param>
/// <param name="kernelY">The vertical gradient operator.</param> /// <param name="kernelY">The vertical gradient operator.</param>
public Convolution2DFilter(float[,] kernelX, float[,] kernelY) public Convolution2DFilter(float[][] kernelX, float[][] kernelY)
{ {
this.KernelX = kernelX; this.KernelX = kernelX;
this.KernelY = kernelY; this.KernelY = kernelY;
@ -32,22 +32,20 @@ namespace ImageProcessorCore.Processors
/// <summary> /// <summary>
/// Gets the horizontal gradient operator. /// Gets the horizontal gradient operator.
/// </summary> /// </summary>
public float[,] KernelX { get; } public float[][] KernelX { get; }
/// <summary> /// <summary>
/// Gets the vertical gradient operator. /// Gets the vertical gradient operator.
/// </summary> /// </summary>
public float[,] KernelY { get; } public float[][] KernelY { get; }
/// <inheritdoc/> /// <inheritdoc/>
public override void Apply(ImageBase<TColor, TPacked> target, ImageBase<TColor, TPacked> source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY) public override void Apply(ImageBase<TColor, TPacked> target, ImageBase<TColor, TPacked> source, Rectangle targetRectangle, Rectangle sourceRectangle, int startY, int endY)
{ {
float[,] kernelX = this.KernelX; int kernelYHeight = KernelY.Length;
float[,] kernelY = this.KernelY; int kernelYWidth = KernelY[0].Length;
int kernelYHeight = kernelY.GetLength(0); int kernelXHeight = KernelX.Length;
int kernelYWidth = kernelY.GetLength(1); int kernelXWidth = KernelX[0].Length;
int kernelXHeight = kernelX.GetLength(0);
int kernelXWidth = kernelX.GetLength(1);
int radiusY = kernelYHeight >> 1; int radiusY = kernelYHeight >> 1;
int radiusX = kernelXWidth >> 1; int radiusX = kernelXWidth >> 1;
@ -100,16 +98,16 @@ namespace ImageProcessorCore.Processors
if (fy < kernelXHeight) if (fy < kernelXHeight)
{ {
rX += kernelX[fy, fx] * r; rX += KernelX[fy][fx] * r;
gX += kernelX[fy, fx] * g; gX += KernelX[fy][fx] * g;
bX += kernelX[fy, fx] * b; bX += KernelX[fy][fx] * b;
} }
if (fx < kernelYWidth) if (fx < kernelYWidth)
{ {
rY += kernelY[fy, fx] * r; rY += KernelY[fy][fx] * r;
gY += kernelY[fy, fx] * g; gY += KernelY[fy][fx] * g;
bY += kernelY[fy, fx] * b; bY += KernelY[fy][fx] * b;
} }
} }
} }

4
src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/EdgeDetector2DFilter.cs

@ -18,12 +18,12 @@ namespace ImageProcessorCore.Processors
/// <summary> /// <summary>
/// Gets the horizontal gradient operator. /// Gets the horizontal gradient operator.
/// </summary> /// </summary>
public abstract float[,] KernelX { get; } public abstract float[][] KernelX { get; }
/// <summary> /// <summary>
/// Gets the vertical gradient operator. /// Gets the vertical gradient operator.
/// </summary> /// </summary>
public abstract float[,] KernelY { get; } public abstract float[][] KernelY { get; }
/// <inheritdoc/> /// <inheritdoc/>
public bool Grayscale { get; set; } public bool Grayscale { get; set; }

24
src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/KayyaliProcessor.cs

@ -15,20 +15,24 @@ namespace ImageProcessorCore.Processors
where TColor : IPackedVector<TPacked> where TColor : IPackedVector<TPacked>
where TPacked : struct where TPacked : struct
{ {
/// <inheritdoc/> private static readonly float[][] kernelX = new float[3][]
public override float[,] KernelX => new float[,]
{ {
{ 6, 0, -6 }, new float[] { 6, 0, -6 },
{ 0, 0, 0 }, new float[] { 0, 0, 0 },
{ -6, 0, 6 } new float[] { -6, 0, 6 }
}; };
/// <inheritdoc/> private static readonly float[][] kernelY = new float[3][]
public override float[,] KernelY => new float[,]
{ {
{ -6, 0, 6 }, new float[] { -6, 0, 6 },
{ 0, 0, 0 }, new float[] { 0, 0, 0 },
{ 6, 0, -6 } new float[] { 6, 0, -6 }
}; };
/// <inheritdoc/>
public override float[][] KernelX => kernelX;
/// <inheritdoc/>
public override float[][] KernelY => kernelY;
} }
} }

24
src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/PrewittProcessor.cs

@ -15,20 +15,24 @@ namespace ImageProcessorCore.Processors
where TColor : IPackedVector<TPacked> where TColor : IPackedVector<TPacked>
where TPacked : struct where TPacked : struct
{ {
/// <inheritdoc/> private static readonly float[][] kernelX = new float[3][]
public override float[,] KernelX => new float[,]
{ {
{ -1, 0, 1 }, new float[] { -1, 0, 1 },
{ -1, 0, 1 }, new float[] { -1, 0, 1 },
{ -1, 0, 1 } new float[] { -1, 0, 1 }
}; };
/// <inheritdoc/> private static readonly float[][] kernelY = new float[3][]
public override float[,] KernelY => new float[,]
{ {
{ 1, 1, 1 }, new float[] { 1, 1, 1 },
{ 0, 0, 0 }, new float[] { 0, 0, 0 },
{ -1, -1, -1 } new float[] { -1, -1, -1 }
}; };
/// <inheritdoc/>
public override float[][] KernelX => kernelX;
/// <inheritdoc/>
public override float[][] KernelY => kernelY;
} }
} }

20
src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/RobertsCrossProcessor.cs

@ -15,18 +15,22 @@ namespace ImageProcessorCore.Processors
where TColor : IPackedVector<TPacked> where TColor : IPackedVector<TPacked>
where TPacked : struct where TPacked : struct
{ {
/// <inheritdoc/> private static readonly float[][] kernelX = new float[2][]
public override float[,] KernelX => new float[,]
{ {
{ 1, 0 }, new float[] { 1, 0 },
{ 0, -1 } new float[] { 0, -1 }
}; };
/// <inheritdoc/> private static readonly float[][] kernelY = new float[2][]
public override float[,] KernelY => new float[,]
{ {
{ 0, 1 }, new float[] { 0, 1 },
{ -1, 0 } new float[] { -1, 0 }
}; };
/// <inheritdoc/>
public override float[][] KernelX => kernelX;
/// <inheritdoc/>
public override float[][] KernelY => kernelY;
} }
} }

24
src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/ScharrProcessor.cs

@ -15,20 +15,24 @@ namespace ImageProcessorCore.Processors
where TColor : IPackedVector<TPacked> where TColor : IPackedVector<TPacked>
where TPacked : struct where TPacked : struct
{ {
/// <inheritdoc/> private static readonly float[][] kernelX = new float[3][]
public override float[,] KernelX => new float[,]
{ {
{ -3, 0, 3 }, new float[] { -3, 0, 3 },
{ -10, 0, 10 }, new float[] { -10, 0, 10 },
{ -3, 0, 3 } new float[] { -3, 0, 3 }
}; };
/// <inheritdoc/> private static readonly float[][] kernelY = new float[3][]
public override float[,] KernelY => new float[,]
{ {
{ 3, 10, 3 }, new float[] { 3, 10, 3 },
{ 0, 0, 0 }, new float[] { 0, 0, 0 },
{ -3, -10, -3 } new float[] { -3, -10, -3 }
}; };
/// <inheritdoc/>
public override float[][] KernelX => kernelX;
/// <inheritdoc/>
public override float[][] KernelY => kernelY;
} }
} }

24
src/ImageProcessorCore/Samplers/Processors/Convolution/EdgeDetection/SobelProcessor.cs

@ -15,20 +15,24 @@ namespace ImageProcessorCore.Processors
where TColor : IPackedVector<TPacked> where TColor : IPackedVector<TPacked>
where TPacked : struct where TPacked : struct
{ {
/// <inheritdoc/> private static readonly float[][] kernelX = new float[3][]
public override float[,] KernelX => new float[,]
{ {
{ -1, 0, 1 }, new float[] { -1, 0, 1 },
{ -2, 0, 2 }, new float[] { -2, 0, 2 },
{ -1, 0, 1 } new float[] { -1, 0, 1 }
}; };
/// <inheritdoc/> private static readonly float[][] kernelY = new float[3][]
public override float[,] KernelY => new float[,]
{ {
{ -1, -2, -1 }, new float[] { -1, -2, -1 },
{ 0, 0, 0 }, new float[] { 0, 0, 0 },
{ 1, 2, 1 } new float[] { 1, 2, 1 }
}; };
/// <inheritdoc/>
public override float[][] KernelX => kernelX;
/// <inheritdoc/>
public override float[][] KernelY => kernelY;
} }
} }

Loading…
Cancel
Save