Scikit Image - Hessian Matrix



The Hessian matrix, in the context of mathematics and computer vision, is a square matrix that contains second-order partial derivatives of a function or image. This matrix provides valuable information about the local curvature, smoothness, and rate of change of a function or an image at each point.

This matrix is a fundamental tool in various image analysis techniques, including feature detection, corner detection, image segmentation, and texture analysis. By analyzing the eigenvalues and eigenvectors of the Hessian matrix, it's possible to determine the shape, orientation, and significance of features within an image

The scikit-image (skimage) library provides the hessian_matrix and hessian_matrix_eigvals functions for computing the Hessian matrix and its eigenvalues, respectively. And these functions are part of scikit-image's feature module.

Using the skimage.feature.hessian_matrix() function

The hessian_matrix() function is used to compute the Hessian matrix of an image.

which is computed by convolving the image with the second derivatives of the Gaussian kernel in the respective r (row) and c (column) directions. The Hessian matrix for 2D data is defined as −

H = [Hrr Hrc] 
[Hrc Hcc]

The implementation of this function also supports n-dimensional data.

Syntax

Here is the syntax of the function −

skimage.feature.hessian_matrix(image, sigma=1, mode='constant', cval=0, order='rc', use_gaussian_derivatives=None)

Parameters

Following are the details of the function parameters −

  • image (ndarray): This parameter is the input image to compute the Hessian matrix.

  • sigma (float): Sigma is the standard deviation of the Gaussian kernel used to compute the second derivatives. Which is used as a weighting function for the auto-correlation matrix.

  • mode {'constant', 'reflect', 'wrap', 'nearest', 'mirror'}, optional: This parameter, how to handle values outside the image borders.

  • cval (float, optional): When using the 'constant' mode, this parameter defines the constant value to be used for values outside the image boundaries.

  • order ({'rc', 'xy'}, optional): For 2D images, this parameter allows for the use of reverse or forward order of the image axes in gradient computation. 'rc' indicates the use of the first axis initially (Hrr, Hrc, Hcc), while 'xy' indicates the usage of the last axis initially (Hxx, Hxy, Hyy). Images with higher dimensions must always use 'rc' order.

  • use_gaussian_derivatives (boolean, optional): Indicates whether the Hessian is computed by convolving with Gaussian derivatives or by a simple finite-difference operation.

The function returns a list of ndarray (H_elems), representing the upper-diagonal elements of the Hessian matrix for each pixel in the input image. In 2D, this will be a three-element list containing [Hrr, Hrc, Hcc]. In nD, the list will contain (n**2 + n) / 2 arrays.

Example

This example demonstrates how to calculate the Hessian matrix for a simple image using the hessian_matrix() function.

from skimage.feature import hessian_matrix
import numpy as np

# Create a 5x5 NumPy array to represent a 2D image filled with zeros
square = np.zeros((5, 5))

# Assign a value 4 to the central pixel of the 'square' image
square[2, 2] = 4

# print the input array
print("Input array:")
print(square)

# Compute the Hessian matrix components
Hrr, Hrc, Hcc = hessian_matrix(square, sigma=0.1, order='rc', use_gaussian_derivatives=False)

# Print the Hessian matrix components
print("Hessian Matrix Elements:")
print("Hrr:")
print(Hrr)
print("Hrc:")
print(Hrc)
print("Hcc:")
print(Hcc)

Output

Input array:
[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 4. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]
Hessian Matrix Elements:
Hrr:
[[ 0.  0.  2.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0. -2.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  2.  0.  0.]]
Hrc:
[[ 0.  0.  0.  0.  0.]
 [ 0.  1.  0. -1.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 0. -1.  0.  1.  0.]
 [ 0.  0.  0.  0.  0.]]
Hcc:
[[ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]
 [ 2.  0. -2.  0.  2.]
 [ 0.  0.  0.  0.  0.]
 [ 0.  0.  0.  0.  0.]]

Using the skimage.feature.hessian_matrix_eigvals()

The function hessian_matrix_eigvals() is used to compute the eigenvalues of the Hessian matrix for an image.

Syntax

Following is the syntax of the function −

skimage.feature.hessian_matrix_eigvals(H_elems)

Parameters

Following are the details of the function parameters −

  • H_elems (list of ndarray): This parameter represents the upper-diagonal elements of the Hessian matrix, and it should be returned by the hessian_matrix function. These elements contain information about second-order derivatives in the image.

The output of this function is eigs (ndarray) representing the eigenvalues of the Hessian matrix. The eigenvalues are sorted in decreasing order. The leading dimension of the eigs array corresponds to the eigenvalues. That is, eigs[i, j, k] contains the i-th largest eigenvalue at position (j, k) in the image.

Example

The following example calculates the eigenvalues of the Hessian matrix for a 2D image using the hessian_matrix() and hessian_matrix_eigvals() functions.

from skimage.feature import hessian_matrix, hessian_matrix_eigvals
import numpy as np

# Create a 5x5 NumPy array to represent a 2D image filled with zeros
square = np.zeros((5, 5))

# Assign a value 4 to the central pixel of the 'square' image
square[2, 2] = 4

# print the input array
print("Input array:")
print(square)

# Compute the Hessian matrix components for the 'square' image
H_elems = hessian_matrix(square, sigma=0.1, order='rc', use_gaussian_derivatives=False)

# Calculate the first eigenvalue of the Hessian matrix
first_eigenvalue = hessian_matrix_eigvals(H_elems)[0]

# print the first eigenvalue of the Hessian matrix
print('First eigenvalue:')
print(first_eigenvalue)

Output

Input array:
[[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 4. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]

First eigenvalue:
[[ 0.  0.  2.  0.  0.]
 [ 0.  1.  0.  1.  0.]
 [ 2.  0. -2.  0.  2.]
 [ 0.  1.  0.  1.  0.]
 [ 0.  0.  2.  0.  0.]]
Advertisements