Browse Source

Changed startX and endX from ushort to int, Add test with rectangle

pull/1574/head
Brian Popow 6 years ago
parent
commit
c1fc52b676
  1. 31
      src/ImageSharp/Processing/Processors/Binarization/AdaptiveThresholdProcessor{TPixel}.cs
  2. 52
      tests/ImageSharp.Tests/Processing/Binarization/AdaptiveThresholdTests.cs
  3. 2
      tests/Images/External

31
src/ImageSharp/Processing/Processors/Binarization/AdaptiveThresholdProcessor{TPixel}.cs

@ -44,14 +44,13 @@ namespace SixLabors.ImageSharp.Processing.Processors.Binarization
TPixel lower = this.definition.Lower.ToPixel<TPixel>();
float thresholdLimit = this.definition.ThresholdLimit;
// Used ushort because the values should never exceed max ushort value.
ushort startY = (ushort)intersect.Y;
ushort endY = (ushort)intersect.Bottom;
ushort startX = (ushort)intersect.X;
ushort endX = (ushort)intersect.Right;
int startY = intersect.Y;
int endY = intersect.Bottom;
int startX = intersect.X;
int endX = intersect.Right;
ushort width = (ushort)intersect.Width;
ushort height = (ushort)intersect.Height;
int width = intersect.Width;
int height = intersect.Height;
// ClusterSize defines the size of cluster to used to check for average. Tweaked to support up to 4k wide pixels and not more. 4096 / 16 is 256 thus the '-1'
byte clusterSize = (byte)Math.Truncate((width / 16f) - 1);
@ -63,10 +62,10 @@ namespace SixLabors.ImageSharp.Processing.Processors.Binarization
var workingRectangle = Rectangle.FromLTRB(startX, startY, endX, endY);
Rgba32 rgb = default;
for (ushort x = startX; x < endX; x++)
for (int x = startX; x < endX; x++)
{
ulong sum = 0;
for (ushort y = startY; y < endY; y++)
for (int y = startY; y < endY; y++)
{
Span<TPixel> row = source.GetPixelRowSpan(y);
ref TPixel rowRef = ref MemoryMarshal.GetReference(row);
@ -101,9 +100,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Binarization
private readonly TPixel upper;
private readonly TPixel lower;
private readonly float thresholdLimit;
private readonly ushort startX;
private readonly ushort endX;
private readonly ushort startY;
private readonly int startX;
private readonly int endX;
private readonly int startY;
private readonly byte clusterSize;
[MethodImpl(InliningOptions.ShortMethod)]
@ -115,9 +114,9 @@ namespace SixLabors.ImageSharp.Processing.Processors.Binarization
TPixel lower,
float thresholdLimit,
byte clusterSize,
ushort startX,
ushort endX,
ushort startY)
int startX,
int endX,
int startY)
{
this.bounds = bounds;
this.source = source;
@ -137,7 +136,7 @@ namespace SixLabors.ImageSharp.Processing.Processors.Binarization
{
Rgba32 rgb = default;
for (ushort x = this.startX; x < this.endX; x++)
for (int x = this.startX; x < this.endX; x++)
{
TPixel pixel = this.source.PixelBuffer[x, y];
pixel.ToRgba32(ref rgb);

52
tests/ImageSharp.Tests/Processing/Binarization/AdaptiveThresholdTests.cs

@ -1,7 +1,6 @@
// Copyright (c) Six Labors and contributors.
// Licensed under the Apache License, Version 2.0.
using SixLabors.ImageSharp.Formats.Tga;
using SixLabors.ImageSharp.PixelFormats;
using SixLabors.ImageSharp.Processing;
using SixLabors.ImageSharp.Processing.Processors.Binarization;
@ -15,10 +14,15 @@ namespace SixLabors.ImageSharp.Tests.Processing.Binarization
[Fact]
public void AdaptiveThreshold_UsesDefaults_Works()
{
// arrange
var expectedThresholdLimit = .85f;
Color expectedUpper = Color.White;
Color expectedLower = Color.Black;
// act
this.operations.AdaptiveThreshold();
// assert
AdaptiveThresholdProcessor p = this.Verify<AdaptiveThresholdProcessor>();
Assert.Equal(expectedThresholdLimit, p.ThresholdLimit);
Assert.Equal(expectedUpper, p.Upper);
@ -28,8 +32,13 @@ namespace SixLabors.ImageSharp.Tests.Processing.Binarization
[Fact]
public void AdaptiveThreshold_SettingThresholdLimit_Works()
{
// arrange
var expectedThresholdLimit = .65f;
// act
this.operations.AdaptiveThreshold(expectedThresholdLimit);
// assert
AdaptiveThresholdProcessor p = this.Verify<AdaptiveThresholdProcessor>();
Assert.Equal(expectedThresholdLimit, p.ThresholdLimit);
Assert.Equal(Color.White, p.Upper);
@ -39,9 +48,14 @@ namespace SixLabors.ImageSharp.Tests.Processing.Binarization
[Fact]
public void AdaptiveThreshold_SettingUpperLowerThresholds_Works()
{
// arrange
Color expectedUpper = Color.HotPink;
Color expectedLower = Color.Yellow;
// act
this.operations.AdaptiveThreshold(expectedUpper, expectedLower);
// assert
AdaptiveThresholdProcessor p = this.Verify<AdaptiveThresholdProcessor>();
Assert.Equal(expectedUpper, p.Upper);
Assert.Equal(expectedLower, p.Lower);
@ -50,16 +64,39 @@ namespace SixLabors.ImageSharp.Tests.Processing.Binarization
[Fact]
public void AdaptiveThreshold_SettingUpperLowerWithThresholdLimit_Works()
{
// arrange
var expectedThresholdLimit = .77f;
Color expectedUpper = Color.HotPink;
Color expectedLower = Color.Yellow;
// act
this.operations.AdaptiveThreshold(expectedUpper, expectedLower, expectedThresholdLimit);
// assert
AdaptiveThresholdProcessor p = this.Verify<AdaptiveThresholdProcessor>();
Assert.Equal(expectedThresholdLimit, p.ThresholdLimit);
Assert.Equal(expectedUpper, p.Upper);
Assert.Equal(expectedLower, p.Lower);
}
[Fact]
public void AdaptiveThreshold_SettingUpperLowerWithThresholdLimit_WithRectangle_Works()
{
// arrange
var expectedThresholdLimit = .77f;
Color expectedUpper = Color.HotPink;
Color expectedLower = Color.Yellow;
// act
this.operations.AdaptiveThreshold(expectedUpper, expectedLower, expectedThresholdLimit, this.rect);
// assert
AdaptiveThresholdProcessor p = this.Verify<AdaptiveThresholdProcessor>(this.rect);
Assert.Equal(expectedThresholdLimit, p.ThresholdLimit);
Assert.Equal(expectedUpper, p.Upper);
Assert.Equal(expectedLower, p.Lower);
}
[Theory]
[WithFile(TestImages.Png.Bradley01, PixelTypes.Rgba32)]
[WithFile(TestImages.Png.Bradley02, PixelTypes.Rgba32)]
@ -73,5 +110,18 @@ namespace SixLabors.ImageSharp.Tests.Processing.Binarization
image.CompareToReferenceOutput(ImageComparer.Exact, provider);
}
}
[Theory]
[WithFile(TestImages.Png.Bradley02, PixelTypes.Rgba32)]
public void AdaptiveThreshold_WithRectangle_Works<TPixel>(TestImageProvider<TPixel> provider)
where TPixel : unmanaged, IPixel<TPixel>
{
using (Image<TPixel> image = provider.GetImage())
{
image.Mutate(img => img.AdaptiveThreshold(Color.White, Color.Black, new Rectangle(60, 90, 200, 30)));
image.DebugSave(provider);
image.CompareToReferenceOutput(ImageComparer.Exact, provider);
}
}
}
}

2
tests/Images/External

@ -1 +1 @@
Subproject commit c04c8b73a99c1b198597ae640394d91ddd8e033b
Subproject commit 6fdc6d19b101dc1c00a297d3e92257df60c413d0
Loading…
Cancel
Save