-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Update hessian matrix code to include order kwarg #2327
Merged
Merged
Changes from all commits
Commits
Show all changes
29 commits
Select commit
Hold shift + click to select a range
b436bcc
Update hessian matrix code to include order kwarg
JohnnyTeutonic 99346c6
changed axes order for fortran order to reverse order using in-build …
JohnnyTeutonic 9606c99
raised deprecation warning for when order isn't specified or ndims is 2
JohnnyTeutonic 622d333
changed wording of deprecation warning
JohnnyTeutonic 3c563cf
pep8
JohnnyTeutonic bd406d8
updated order from 'C' and 'F' to 'xy and 'rc' respectively
JohnnyTeutonic afc0f0e
changed to rc convention from xy in test_corner.py
JohnnyTeutonic 99d003c
changed order convention to rc from xy in _frangi.py
JohnnyTeutonic 17acae8
made the deprecation warning for the order more verbose
JohnnyTeutonic 8dfc5b4
changed order to c in function call for testing hessian matrix
JohnnyTeutonic 1ba678f
changed order to rc in eigval test
JohnnyTeutonic e1815e6
changed order to 'rc' in frangi hessian matrix
JohnnyTeutonic 1e5c381
imported assert_warns test
JohnnyTeutonic 14b55ba
added an assert_warning test to test functionality of order to 'test_…
JohnnyTeutonic 43c46a7
removed bug in generation of 2d sample matrix in hessian_matrix test
JohnnyTeutonic 6d8c854
got rid of bug in functional call of np.random.rand to generate a tes…
JohnnyTeutonic 883e7c9
changed warning class to userwarning
JohnnyTeutonic f1e864d
changed docstring for hessian_matrix to remove unnecessary lines
JohnnyTeutonic 7740636
change docstring for the 'order' parameter to better clarify how it i…
JohnnyTeutonic d679355
remove unnecessary call to hessian matrix function
JohnnyTeutonic 0bfebc0
changed order to rc in hessian_matrix_3d in test_Corner
JohnnyTeutonic c2a5a85
changed deprecation warning message for hessian matrix
JohnnyTeutonic 75d178c
change docstring for hesian matrix to include explicit call to order=rc
JohnnyTeutonic 4e3c2f8
remove stray code
JohnnyTeutonic f4bc8f4
change default order to Hessian matrix to xy
JohnnyTeutonic ee112e3
change doctest for order in hessian matrix
JohnnyTeutonic 349c63f
change alignment of deprecation warning of order in hessian matrix
JohnnyTeutonic ea171cb
set order to rc in hessian_matrix_eigvals function
JohnnyTeutonic a6c582d
change to explicit call to order='rc' in shape_index function
JohnnyTeutonic File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,7 +12,7 @@ | |
from ..transform import integral_image | ||
from .._shared.utils import safe_as_int | ||
from .corner_cy import _corner_moravec, _corner_orientations | ||
|
||
from warnings import warn | ||
|
||
def _compute_derivatives(image, mode='constant', cval=0): | ||
"""Compute derivatives in x and y direction using the Sobel operator. | ||
|
@@ -102,13 +102,13 @@ def structure_tensor(image, sigma=1, mode='constant', cval=0): | |
return Axx, Axy, Ayy | ||
|
||
|
||
def hessian_matrix(image, sigma=1, mode='constant', cval=0): | ||
def hessian_matrix(image, sigma=1, mode='constant', cval=0, order=None): | ||
"""Compute Hessian matrix. | ||
|
||
The Hessian matrix is defined as:: | ||
|
||
H = [Hxx Hxy] | ||
[Hxy Hyy] | ||
H = [Hrr Hrc] | ||
[Hrc Hcc] | ||
|
||
which is computed by convolving the image with the second derivatives | ||
of the Gaussian kernel in the respective x- and y-directions. | ||
|
@@ -125,23 +125,28 @@ def hessian_matrix(image, sigma=1, mode='constant', cval=0): | |
cval : float, optional | ||
Used in conjunction with mode 'constant', the value outside | ||
the image boundaries. | ||
order : {'xy', 'rc'}, optional | ||
This parameter allows for the use of reverse or forward order of | ||
the image axes in gradient computation. 'xy' indicates the usage | ||
of the last axis initially (Hxx, Hxy, Hyy), whilst 'rc' indicates | ||
the use of the first axis initially (Hrr, Hrc, Hcc). | ||
|
||
Returns | ||
------- | ||
Hxx : ndarray | ||
Hrr : ndarray | ||
Element of the Hessian matrix for each pixel in the input image. | ||
Hxy : ndarray | ||
Hrc : ndarray | ||
Element of the Hessian matrix for each pixel in the input image. | ||
Hyy : ndarray | ||
Hcc : ndarray | ||
Element of the Hessian matrix for each pixel in the input image. | ||
|
||
Examples | ||
-------- | ||
>>> from skimage.feature import hessian_matrix | ||
>>> square = np.zeros((5, 5)) | ||
>>> square[2, 2] = 4 | ||
>>> Hxx, Hxy, Hyy = hessian_matrix(square, sigma=0.1) | ||
>>> Hxy | ||
>>> Hrr, Hrc, Hcc = hessian_matrix(square, sigma=0.1, order = 'rc') | ||
>>> Hrc | ||
array([[ 0., 0., 0., 0., 0.], | ||
[ 0., 1., 0., -1., 0.], | ||
[ 0., 0., 0., 0., 0.], | ||
|
@@ -154,18 +159,29 @@ def hessian_matrix(image, sigma=1, mode='constant', cval=0): | |
gaussian_filtered = ndi.gaussian_filter(image, sigma=sigma, | ||
mode=mode, cval=cval) | ||
|
||
if order is None: | ||
if image.ndim == 2: | ||
# The legacy 2D code followed (x, y) convention, so we swap the axis | ||
# order to maintain compatibility with old code | ||
warn('deprecation warning: the default order of the hessian matrix values ' | ||
'will be "row-column" instead of "xy" starting in skimage version 0.15. ' | ||
'Use order="rc" or order="xy" to set this explicitly') | ||
order = 'xy' | ||
else: | ||
order = 'rc' | ||
|
||
|
||
gradients = np.gradient(gaussian_filtered) | ||
axes = range(image.ndim) | ||
|
||
if order == 'rc': | ||
axes = reversed(axes) | ||
|
||
H_elems = [np.gradient(gradients[ax0], axis=ax1) | ||
for ax0, ax1 in combinations_with_replacement(axes, 2)] | ||
|
||
if image.ndim == 2: | ||
# The legacy 2D code followed (x, y) convention, so we swap the axis | ||
# order to maintain compatibility with old code | ||
H_elems.reverse() | ||
return H_elems | ||
|
||
|
||
def hessian_matrix_det(image, sigma=1): | ||
"""Computes the approximate Hessian Determinant over an image. | ||
|
||
|
@@ -249,7 +265,7 @@ def structure_tensor_eigvals(Axx, Axy, Ayy): | |
|
||
|
||
def hessian_matrix_eigvals(Hxx, Hxy, Hyy): | ||
"""Compute Eigen values of Hessian matrix. | ||
"""Compute Eigenvalues of Hessian matrix. | ||
|
||
Parameters | ||
---------- | ||
|
@@ -272,7 +288,7 @@ def hessian_matrix_eigvals(Hxx, Hxy, Hyy): | |
>>> from skimage.feature import hessian_matrix, hessian_matrix_eigvals | ||
>>> square = np.zeros((5, 5)) | ||
>>> square[2, 2] = 4 | ||
>>> Hxx, Hxy, Hyy = hessian_matrix(square, sigma=0.1) | ||
>>> Hxx, Hxy, Hyy = hessian_matrix(square, sigma=0.1, order='rc') | ||
>>> hessian_matrix_eigvals(Hxx, Hxy, Hyy)[0] | ||
array([[ 0., 0., 2., 0., 0.], | ||
[ 0., 1., 0., 1., 0.], | ||
|
@@ -320,7 +336,7 @@ def shape_index(image, sigma=1, mode='constant', cval=0): | |
Standard deviation used for the Gaussian kernel, which is used for | ||
smoothing the input data before Hessian eigen value calculation. | ||
mode : {'constant', 'reflect', 'wrap', 'nearest', 'mirror'}, optional | ||
How to handle values outside the image borders. | ||
How to handle values outside the image borders | ||
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. Undo this change. (add back the full stop) |
||
cval : float, optional | ||
Used in conjunction with mode 'constant', the value outside | ||
the image boundaries. | ||
|
@@ -351,7 +367,7 @@ def shape_index(image, sigma=1, mode='constant', cval=0): | |
[ nan, nan, -0.5, nan, nan]]) | ||
""" | ||
|
||
Hxx, Hxy, Hyy = hessian_matrix(image, sigma=sigma, mode=mode, cval=cval) | ||
Hxx, Hxy, Hyy = hessian_matrix(image, sigma=sigma, mode=mode, cval=cval, order='rc') | ||
l1, l2 = hessian_matrix_eigvals(Hxx, Hxy, Hyy) | ||
|
||
return (2.0 / np.pi) * np.arctan((l2 + l1) / (l2 - l1)) | ||
|
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
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
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.
We should raise a deprecation warning here.