Browse Source

Added contructor to control threshold limit

pull/725/head
Simanto Rahman 8 years ago
parent
commit
b0541025b2
  1. 42
      src/ImageSharp/Processing/AdaptiveThresholdExtensions.cs
  2. 31
      src/ImageSharp/Processing/Processors/Binarization/AdaptiveThresholdProcessor.cs

42
src/ImageSharp/Processing/AdaptiveThresholdExtensions.cs

@ -1,5 +1,5 @@
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing.Processors;
using SixLabors.ImageSharp.Processing.Processors.Binarization;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing
@ -19,18 +19,42 @@ namespace SixLabors.ImageSharp.Processing
where TPixel : struct, IPixel<TPixel>
=> source.ApplyProcessor(new AdaptiveThresholdProcessor<TPixel>());
/// <summary>
/// Applies Bradley Adaptive Threshold to the image.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="threshold">Threshold limit (0.0-1.0) to consider for binarization.</param>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static IImageProcessingContext<TPixel> AdaptiveThreshold<TPixel>(this IImageProcessingContext<TPixel> source, float threshold)
where TPixel : struct, IPixel<TPixel>
=> source.ApplyProcessor(new AdaptiveThresholdProcessor<TPixel>(threshold));
/// <summary>
/// Applies Bradley Adaptive Threshold to the image.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="upper">Upper (white) color for thresholding.</param>
/// <param name="lower">Lower (black) color for thresholding</param>
/// /// <typeparam name="TPixel">The pixel format.</typeparam>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static IImageProcessingContext<TPixel> AdaptiveThreshold<TPixel>(this IImageProcessingContext<TPixel> source, TPixel upper, TPixel lower)
where TPixel : struct, IPixel<TPixel>
=> source.ApplyProcessor(new AdaptiveThresholdProcessor<TPixel>(upper, lower));
/// <summary>
/// Applies Bradley Adaptive Threshold to the image.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="upper">Upper (white) color for thresholding.</param>
/// <param name="lower">Lower (black) color for thresholding</param>
/// <param name="threshold">Threshold limit (0.0-1.0) to consider for binarization.</param>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static IImageProcessingContext<TPixel> AdaptiveThreshold<TPixel>(this IImageProcessingContext<TPixel> source, TPixel upper, TPixel lower, float threshold)
where TPixel : struct, IPixel<TPixel>
=> source.ApplyProcessor(new AdaptiveThresholdProcessor<TPixel>(upper, lower, threshold));
/// <summary>
/// Applies Bradley Adaptive Threshold to the image.
/// </summary>
@ -43,5 +67,19 @@ namespace SixLabors.ImageSharp.Processing
public static IImageProcessingContext<TPixel> AdaptiveThreshold<TPixel>(this IImageProcessingContext<TPixel> source, TPixel upper, TPixel lower, Rectangle rectangle)
where TPixel : struct, IPixel<TPixel>
=> source.ApplyProcessor(new AdaptiveThresholdProcessor<TPixel>(upper, lower), rectangle);
/// <summary>
/// Applies Bradley Adaptive Threshold to the image.
/// </summary>
/// <param name="source">The image this method extends.</param>
/// <param name="upper">Upper (white) color for thresholding.</param>
/// <param name="lower">Lower (black) color for thresholding</param>
/// <param name="threshold">Threshold limit (0.0-1.0) to consider for binarization.</param>
/// <param name="rectangle">Rectangle region to apply the processor on.</param>
/// <typeparam name="TPixel">The pixel format.</typeparam>
/// <returns>The <see cref="Image{TPixel}"/>.</returns>
public static IImageProcessingContext<TPixel> AdaptiveThreshold<TPixel>(this IImageProcessingContext<TPixel> source, TPixel upper, TPixel lower, float threshold, Rectangle rectangle)
where TPixel : struct, IPixel<TPixel>
=> source.ApplyProcessor(new AdaptiveThresholdProcessor<TPixel>(upper, lower, threshold), rectangle);
}
}

31
src/ImageSharp/Processing/Processors/Binarization/AdaptiveThresholdProcessor.cs

@ -10,7 +10,7 @@ using SixLabors.ImageSharp.PixelFormats;
using SixLabors.Memory;
using SixLabors.Primitives;
namespace SixLabors.ImageSharp.Processing.Processors
namespace SixLabors.ImageSharp.Processing.Processors.Binarization
{
/// <summary>
/// Performs Bradley Adaptive Threshold filter against an image
@ -25,7 +25,21 @@ namespace SixLabors.ImageSharp.Processing.Processors
/// Initializes a new instance of the <see cref="AdaptiveThresholdProcessor{TPixel}"/> class.
/// </summary>
public AdaptiveThresholdProcessor()
: this(NamedColors<TPixel>.White, NamedColors<TPixel>.Black)
: this(NamedColors<TPixel>.White, NamedColors<TPixel>.Black, 0.85f)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="AdaptiveThresholdProcessor{TPixel}"/> class.
/// </summary>
/// <param name="threshold">Threshold limit</param>
public AdaptiveThresholdProcessor(float threshold)
: this(NamedColors<TPixel>.White, NamedColors<TPixel>.Black, threshold)
{
}
public AdaptiveThresholdProcessor(TPixel upper, TPixel lower)
: this(upper, lower, 0.85f)
{
}
@ -34,12 +48,14 @@ namespace SixLabors.ImageSharp.Processing.Processors
/// </summary>
/// <param name="upper">Color for upper threshold</param>
/// <param name="lower">Color for lower threshold</param>
public AdaptiveThresholdProcessor(TPixel upper, TPixel lower)
/// <param name="threshold">Threshold limit</param>
public AdaptiveThresholdProcessor(TPixel upper, TPixel lower, float threshold)
{
this.pixelOpInstance = PixelOperations<TPixel>.Instance;
this.Upper = upper;
this.Lower = lower;
this.Threshold = threshold;
}
/// <summary>
@ -52,6 +68,11 @@ namespace SixLabors.ImageSharp.Processing.Processors
/// </summary>
public TPixel Lower { get; set; }
/// <summary>
/// Gets or sets the value for threshold limit
/// </summary>
public float Threshold { get; set; }
/// <inheritdoc/>
protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle sourceRectangle, Configuration configuration)
{
@ -67,8 +88,6 @@ namespace SixLabors.ImageSharp.Processing.Processors
// Tweaked to support upto 4k wide pixels and not more. 4096 / 16 is 256 thus the '-1'
byte clusterSize = (byte)((width / 16) - 1);
float threshold = 0.85f;
// Using pooled 2d buffer for integer image table
using (Buffer2D<ulong> intImage = configuration.MemoryAllocator.Allocate2D<ulong>(width, height))
{
@ -136,7 +155,7 @@ namespace SixLabors.ImageSharp.Processing.Processors
sum = (long)(intImage[x2, y2] - intImage[x1, y2] - intImage[x2, y1] + intImage[x1, y1]);
if ((rgb.R + rgb.G + rgb.B) * count < sum * threshold)
if ((rgb.R + rgb.G + rgb.B) * count < sum * this.Threshold)
{
originalSpan[j] = this.Lower;
}

Loading…
Cancel
Save