-
-
Notifications
You must be signed in to change notification settings - Fork 855
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
Add TryGetLinearlySeparableComponents and tests #2224
Merged
JimBobSquarePants
merged 7 commits into
main
from
sp/auto-matrix-linear-decomposition-2
Sep 26, 2022
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
45b2dee
Add TryGetLinearlySeparableComponents and tests
JimBobSquarePants f935433
Merge branch 'main' into sp/auto-matrix-linear-decomposition-2
JimBobSquarePants 65088b3
Fix merge and namespace style
JimBobSquarePants 1c219bf
Merge branch 'main' into sp/auto-matrix-linear-decomposition-2
JimBobSquarePants 434747c
Merge branch 'main' into sp/auto-matrix-linear-decomposition-2
JimBobSquarePants 7649427
Merge branch 'main' into sp/auto-matrix-linear-decomposition-2
JimBobSquarePants 897e472
Merge branch 'main' into sp/auto-matrix-linear-decomposition-2
JimBobSquarePants 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 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
70 changes: 70 additions & 0 deletions
70
tests/ImageSharp.Tests/Processing/Processors/Convolution/ConvolutionProcessorHelpersTest.cs
This file contains 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,70 @@ | ||
// Copyright (c) Six Labors. | ||
// Licensed under the Six Labors Split License. | ||
|
||
using SixLabors.ImageSharp.Processing.Processors.Convolution; | ||
|
||
namespace SixLabors.ImageSharp.Tests.Processing.Processors.Convolution; | ||
|
||
[GroupOutput("Convolution")] | ||
public class ConvolutionProcessorHelpersTest | ||
{ | ||
[Theory] | ||
[InlineData(3)] | ||
[InlineData(5)] | ||
[InlineData(9)] | ||
[InlineData(22)] | ||
[InlineData(33)] | ||
[InlineData(80)] | ||
public void VerifyGaussianKernelDecomposition(int radius) | ||
{ | ||
int kernelSize = (radius * 2) + 1; | ||
float sigma = radius / 3F; | ||
float[] kernel = ConvolutionProcessorHelpers.CreateGaussianBlurKernel(kernelSize, sigma); | ||
DenseMatrix<float> matrix = DotProduct(kernel, kernel); | ||
|
||
bool result = matrix.TryGetLinearlySeparableComponents(out float[] row, out float[] column); | ||
|
||
Assert.True(result); | ||
Assert.NotNull(row); | ||
Assert.NotNull(column); | ||
Assert.Equal(row.Length, matrix.Rows); | ||
Assert.Equal(column.Length, matrix.Columns); | ||
|
||
float[,] dotProduct = DotProduct(row, column); | ||
|
||
for (int y = 0; y < column.Length; y++) | ||
{ | ||
for (int x = 0; x < row.Length; x++) | ||
{ | ||
Assert.True(Math.Abs(matrix[y, x] - dotProduct[y, x]) < 0.0001F); | ||
} | ||
} | ||
} | ||
|
||
[Fact] | ||
public void VerifyNonSeparableMatrix() | ||
{ | ||
bool result = LaplacianKernels.LaplacianOfGaussianXY.TryGetLinearlySeparableComponents( | ||
out float[] row, | ||
out float[] column); | ||
|
||
Assert.False(result); | ||
Assert.Null(row); | ||
Assert.Null(column); | ||
} | ||
|
||
private static DenseMatrix<float> DotProduct(float[] row, float[] column) | ||
{ | ||
float[,] matrix = new float[column.Length, row.Length]; | ||
|
||
for (int x = 0; x < row.Length; x++) | ||
{ | ||
for (int y = 0; y < column.Length; y++) | ||
{ | ||
matrix[y, x] = row[x] * column[y]; | ||
} | ||
} | ||
|
||
return matrix; | ||
} | ||
} |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Using a hardcoded epsilon might be problematic in many cases. Personally I prefer to make it a parameter with a default value or at least to work with library-wide constants proven to work well for the given library.
What are ImageSharp use-cases for this feature?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah we should normalise constants. There might be one in Numerics already.
I plan to use this as an underlying optimisation when exposing a general convolution API.