-
-
Notifications
You must be signed in to change notification settings - Fork 886
Addition of Breadley AdaptiveThreshold #725
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
30 commits
Select commit
Hold shift + click to select a range
e1d39de
Addition of Breadley AdaptiveThreshold
SimantoR 152b8e6
# Added Rect.Intersect
SimantoR 7951548
Merge branch 'master' into master
SimantoR 170a65d
Merge branch 'master' into master
SimantoR 5d61a3f
Merge branch 'master' into master
SimantoR 95a7b0d
Added parallelism to loops
SimantoR 1e7dc83
Merge branch 'master' into master
SimantoR 2b6d93a
Temporary fix to accomodate #744
SimantoR bb5cc29
Few general changes without effecting the algorithm implementation
SimantoR ba8929c
Merge branch 'master' into master
SimantoR a501113
Merge branch 'master' of https://github.com/SimantoR/ImageSharp
SimantoR d8f3b39
Fixed few breaking changes
SimantoR 8978bc3
Missed an end of line by accident :p
SimantoR 6805f6d
Used TempBuffer and fixed few logical errors
SimantoR b054102
Added contructor to control threshold limit
SimantoR 319fc95
Fixed several bugs produced during parallelism implementations
SimantoR a239e60
Algorithm behaves abnormally when applied with ParallelHelpers
SimantoR 1f52c9d
Fully working implementation
SimantoR 31e5d8d
Changed naming convention for threshold limit param
SimantoR 8610f5c
Fixed a minor bug
SimantoR be3718d
update to most recent version
SimantoR a5a0ecd
Re-add external test images
brianpopow 019d973
Merge remote-tracking branch 'upstream/master'
brianpopow 39d5a93
Adjustments to changes from the upstream
brianpopow 6963945
Add tests for the AdaptiveThreshold processor
brianpopow 75ac0ee
Remove not needed tmp buffer
brianpopow a468883
Changed startX and endX from ushort to int, Add test with rectangle
brianpopow 1c92670
Using pixel row span to access the pixels
brianpopow 50aa77e
Review changes
brianpopow ee016f6
Minor formatting change
brianpopow File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| using SixLabors.ImageSharp.PixelFormats; | ||
| using SixLabors.ImageSharp.Processing.Processors; | ||
| using SixLabors.Primitives; | ||
|
|
||
| namespace SixLabors.ImageSharp.Processing | ||
| { | ||
| /// <summary> | ||
| /// Extensions to perform AdaptiveThreshold through Mutator | ||
| /// </summary> | ||
| public static class AdaptiveThresholdExtensions | ||
| { | ||
| /// <summary> | ||
| /// Applies Bradley Adaptive Threshold to the image. | ||
| /// </summary> | ||
| /// <param name="source">The image this method extends.</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) | ||
| 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="upper">Upper (white) color for thresholding.</param> | ||
| /// <param name="lower">Lower (black) color for thresholding</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) | ||
| 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="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, Rectangle rectangle) | ||
| where TPixel : struct, IPixel<TPixel> | ||
| => source.ApplyProcessor(new AdaptiveThresholdProcessor<TPixel>(upper, lower), rectangle); | ||
| } | ||
| } |
149 changes: 149 additions & 0 deletions
149
src/ImageSharp/Processing/Processors/Binarization/AdaptiveThresholdProcessor.cs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,149 @@ | ||
| // Copyright (c) Six Labors and contributors. | ||
| // Licensed under the Apache License, Version 2.0. | ||
|
|
||
| using System; | ||
| using System.Threading.Tasks; | ||
| using SixLabors.ImageSharp.Advanced; | ||
| using SixLabors.ImageSharp.Memory; | ||
| using SixLabors.ImageSharp.ParallelUtils; | ||
| using SixLabors.ImageSharp.PixelFormats; | ||
| using SixLabors.Memory; | ||
| using SixLabors.Primitives; | ||
|
|
||
| namespace SixLabors.ImageSharp.Processing.Processors | ||
| { | ||
| /// <summary> | ||
| /// Performs Bradley Adaptive Threshold filter against an image | ||
| /// </summary> | ||
| /// <typeparam name="TPixel">The pixel format of the image</typeparam> | ||
| internal class AdaptiveThresholdProcessor<TPixel> : ImageProcessor<TPixel> | ||
| where TPixel : struct, IPixel<TPixel> | ||
| { | ||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AdaptiveThresholdProcessor{TPixel}"/> class. | ||
| /// </summary> | ||
| public AdaptiveThresholdProcessor() | ||
| : this(NamedColors<TPixel>.White, NamedColors<TPixel>.Black) | ||
| { | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Initializes a new instance of the <see cref="AdaptiveThresholdProcessor{TPixel}"/> class. | ||
| /// </summary> | ||
| /// <param name="upper">Color for upper threshold</param> | ||
| /// <param name="lower">Color for lower threshold</param> | ||
| public AdaptiveThresholdProcessor(TPixel upper, TPixel lower) | ||
| { | ||
| this.Upper = upper; | ||
| this.Lower = lower; | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets upper color limit for thresholding | ||
| /// </summary> | ||
| public TPixel Upper { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Gets or sets lower color limit for threshold | ||
| /// </summary> | ||
| public TPixel Lower { get; set; } | ||
|
|
||
| /// <inheritdoc/> | ||
| protected override void OnFrameApply(ImageFrame<TPixel> source, Rectangle sourceRectangle, Configuration configuration) | ||
| { | ||
| var intersect = Rectangle.Intersect(sourceRectangle, source.Bounds()); | ||
| ushort startY = (ushort)intersect.Y; | ||
| ushort endY = (ushort)intersect.Bottom; | ||
| ushort startX = (ushort)intersect.X; | ||
| ushort endX = (ushort)intersect.Right; | ||
|
|
||
| ushort width = (ushort)(endX - startX); | ||
| ushort height = (ushort)(endY - startY); | ||
|
|
||
| // Tweaked to support upto 4k wide pixels and not more. 4096 / 16 is 256 thus the '-1' | ||
| byte s = (byte)Math.Truncate((width / 16f) - 1); | ||
SimantoR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // Using pooled 2d buffer for integer image table | ||
| using (Buffer2D<ulong> intImage = configuration.MemoryAllocator.Allocate2D<ulong>(width, height, AllocationOptions.Clean)) | ||
| { | ||
| var workingnRectangle = Rectangle.FromLTRB(startX, startY, endX, endY); | ||
|
|
||
| ParallelHelper.IterateRows( | ||
| workingnRectangle, | ||
| configuration, | ||
| rows => | ||
| { | ||
| ulong sum; | ||
|
|
||
| Rgb24 rgb = default; | ||
|
|
||
| for (int i = rows.Min; i < rows.Max; i++) | ||
| { | ||
| var row = source.GetPixelRowSpan(i); | ||
|
|
||
| sum = 0; | ||
|
|
||
| for (int j = startX; j < endX; j++) | ||
| { | ||
| row[j].ToRgb24(ref rgb); | ||
| sum += (ulong)(rgb.B + rgb.G + rgb.B); | ||
|
|
||
| if (i != 0) | ||
| { | ||
| intImage[i, j] = intImage[i - 1, j] + sum; | ||
| } | ||
| else | ||
| { | ||
| intImage[i, j] = sum; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ); | ||
|
|
||
| ParallelHelper.IterateRows( | ||
| workingnRectangle, | ||
| configuration, | ||
| rows => | ||
| { | ||
| ushort x1, x2, y1, y2; | ||
|
|
||
| uint count; | ||
|
|
||
| long sum; | ||
|
|
||
| for (int i = rows.Min; i < rows.Max; i++) | ||
| { | ||
| var row = source.GetPixelRowSpan(i); | ||
|
|
||
| Rgb24 rgb = default; | ||
|
|
||
| for (int j = startX; j < endX; j++) | ||
| { | ||
| x1 = (ushort)Math.Max(i - s + 1, 0); | ||
| x2 = (ushort)Math.Min(i + s + 1, endY - 1); | ||
| y1 = (ushort)Math.Max(j - s + 1, 0); | ||
| y2 = (ushort)Math.Min(j + s + 1, endX - 1); | ||
|
|
||
| count = (uint)((x2 - x1) * (y2 - y1)); | ||
|
|
||
| sum = (long)(intImage[x2, y2] - intImage[x1, y2] - intImage[x2, y1] + intImage[x1, y1]); | ||
|
|
||
| row[j].ToRgb24(ref rgb); | ||
SimantoR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| if ((rgb.R + rgb.G + rgb.B) * count < sum * (1.0 - 0.15)) | ||
SimantoR marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| { | ||
| row[j] = this.Lower; | ||
| } | ||
| else | ||
| { | ||
| row[j] = this.Upper; | ||
| } | ||
| } | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| } | ||
Submodule External
updated
24 files
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.