-
-
Notifications
You must be signed in to change notification settings - Fork 715
Make 3D SobelOperator consistent with 2D, add UseLegacyCoefficients, add ND support #5718
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
Open
N-Dekker
wants to merge
2
commits into
InsightSoftwareConsortium:main
Choose a base branch
from
N-Dekker:Make-SobelOperator-3D-consistent
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+142
−37
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
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
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
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 |
|---|---|---|
|
|
@@ -19,7 +19,10 @@ | |
| #define itkSobelOperator_hxx | ||
|
|
||
| #include "itkIndexRange.h" | ||
| #include "itkIntTypes.h" // For size_t. | ||
| #include "itkMath.h" | ||
| #include "itkObject.h" | ||
| #include <array> | ||
|
|
||
| namespace itk | ||
| { | ||
|
|
@@ -60,32 +63,85 @@ template <typename TPixel, unsigned int VDimension, typename TAllocator> | |
| auto | ||
| SobelOperator<TPixel, VDimension, TAllocator>::GenerateCoefficients() -> CoefficientVector | ||
| { | ||
| const unsigned int direction = this->GetDirection(); | ||
|
|
||
| if constexpr (VDimension == 2) | ||
| if (const unsigned int direction{ this->GetDirection() }; direction < VDimension) | ||
| { | ||
| switch (direction) | ||
| if constexpr (VDimension == 3) | ||
| { | ||
| case 0: | ||
| return { -1, 0, 1, -2, 0, 2, -1, 0, 1 }; | ||
| case 1: | ||
| return { -1, -2, -1, 0, 0, 0, 1, 2, 1 }; | ||
| if (m_UseLegacyCoefficients) | ||
| { | ||
| switch (direction) | ||
| { | ||
| case 0: | ||
| return { -1, 0, 1, -3, 0, 3, -1, 0, 1, -3, 0, 3, -6, 0, 6, -3, 0, 3, -1, 0, 1, -3, 0, 3, -1, 0, 1 }; | ||
| case 1: | ||
| return { -1, -3, -1, 0, 0, 0, 1, 3, 1, -3, -6, -3, 0, 0, 0, 3, 6, 3, -1, -3, -1, 0, 0, 0, 1, 3, 1 }; | ||
| case 2: | ||
| return { -1, -3, -1, -3, -6, -3, -1, -3, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 3, 6, 3, 1, 3, 1 }; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| // Create N-dimensional Sobel kernels (one per axis). | ||
| // Each kernel is size 3^N, stored in row-major order with the last dimension varying fastest. | ||
| // Standard Sobel definition: derivative = [-1, 0, 1], smoothing = [1, 2, 1]. | ||
| // Kernel for axis a is: K_a(x) = d[x_a] * Product_{i != a} s[x_i], with x_i being element of {-1,0,1}. | ||
| static constexpr auto sobelKernels = [] { | ||
| constexpr int derivative[3] = { -1, 0, 1 }; | ||
| constexpr int smoothing[3] = { 1, 2, 1 }; | ||
|
|
||
| constexpr size_t total = Math::UnsignedPower(3, VDimension); | ||
|
|
||
| // Allocate one kernel per axis | ||
| std::array<std::array<int, total>, VDimension> kernels{}; | ||
|
|
||
| // Iterate over all N-D indices in {0,1,2}^N (mapping to {-1,0,1}) | ||
| for (size_t linear = 0; linear < total; ++linear) | ||
| { | ||
| // Convert linear index -> base-3 digits (idx[0..N-1]) | ||
| const auto idx = [linear] { | ||
| std::array<int, VDimension> result{}; | ||
| size_t temp = linear; | ||
| for (size_t i = 0; i < VDimension; ++i) | ||
| { | ||
| result[VDimension - 1 - i] = static_cast<int>(temp % 3); | ||
| temp /= 3; | ||
| } | ||
| return result; | ||
| }(); | ||
|
|
||
| // For each axis, compute Sobel value at this point | ||
| for (size_t axis = 0; axis < VDimension; ++axis) | ||
| { | ||
| int val = derivative[idx[axis]]; | ||
| if (val == 0) | ||
| { // derivative center => whole product is 0 | ||
| kernels[axis][linear] = 0; | ||
| } | ||
| else | ||
| { | ||
| for (size_t i = 0; i < VDimension; ++i) | ||
| { | ||
| if (i != axis) | ||
| { | ||
| val *= smoothing[idx[i]]; | ||
| } | ||
| } | ||
| kernels[axis][linear] = val; | ||
| } | ||
| } | ||
| } | ||
| return kernels; | ||
| }(); | ||
|
|
||
| // Note: It appears that `sobelKernels` is in the reverse order! | ||
| const auto & kernel = sobelKernels[VDimension - 1 - direction]; | ||
| return CoefficientVector(kernel.cbegin(), kernel.cend()); | ||
|
Comment on lines
+137
to
+138
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why not simply |
||
| } | ||
| if constexpr (VDimension == 3) | ||
| else | ||
| { | ||
| switch (direction) | ||
| { | ||
| case 0: | ||
| return { -1, 0, 1, -3, 0, 3, -1, 0, 1, -3, 0, 3, -6, 0, 6, -3, 0, 3, -1, 0, 1, -3, 0, 3, -1, 0, 1 }; | ||
| case 1: | ||
| return { -1, -3, -1, 0, 0, 0, 1, 3, 1, -3, -6, -3, 0, 0, 0, 3, 6, 3, -1, -3, -1, 0, 0, 0, 1, 3, 1 }; | ||
| case 2: | ||
| return { -1, -3, -1, -3, -6, -3, -1, -3, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 3, 1, 3, 6, 3, 1, 3, 1 }; | ||
| } | ||
| itkExceptionMacro("The direction value (" << direction << ") should be less than the dimensionality (" << VDimension | ||
| << ")."); | ||
| } | ||
| itkExceptionMacro("The direction value (" << direction << ") should be less than the dimensionality (" << VDimension | ||
| << ")."); | ||
| } | ||
| } // namespace itk | ||
|
|
||
|
|
||
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
Oops, something went wrong.
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.
Note that
itkSetMacro(UseLegacyCoefficients, bool)does not compile, becauseitk::SobelOperatoris not derived fromitk::Object, and it does not supportthis->Modified().The macro (itkSetMacro) would make it
SetUseLegacyCoefficients(bool _arg)instead. But I thinkUseLegacyCoefficients(const bool useLegacyCoefficients)is more readable. OK?What about the other member function,
IsUsingLegacyCoefficients()? Is that clear enough?