From 3e2ca1493e15dba21f5632d44e2caa065280dce0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Johannes=20Sch=C3=B6nberger?= Date: Tue, 27 Aug 2013 15:37:58 +0200 Subject: [PATCH] Add test cases for structure tensor and hessian matrix functions --- skimage/feature/corner.py | 4 ++- skimage/feature/tests/test_corner.py | 45 +++++++++++++++++++++++++++- 2 files changed, 47 insertions(+), 2 deletions(-) diff --git a/skimage/feature/corner.py b/skimage/feature/corner.py index 66f9bc21..3d665946 100644 --- a/skimage/feature/corner.py +++ b/skimage/feature/corner.py @@ -126,7 +126,7 @@ def hessian_matrix(image, sigma=1, mode='constant', cval=0): # window extent to the left and right, which covers > 99% of the normal # distribution - window_ext = np.ceil(3 * sigma) + window_ext = max(1, np.ceil(3 * sigma)) ky, kx = np.mgrid[-window_ext:window_ext + 1, -window_ext:window_ext + 1] @@ -134,8 +134,10 @@ def hessian_matrix(image, sigma=1, mode='constant', cval=0): gaussian_exp = np.exp(-(kx ** 2 + ky ** 2) / (2 * sigma ** 2)) kernel_xx = 1 / (2 * np.pi * sigma ** 4) * (kx ** 2 / sigma ** 2 - 1) kernel_xx *= gaussian_exp + kernel_xx /= kernel_xx.sum() kernel_xy = 1 / (2 * np.pi * sigma ** 6) * (kx * ky) kernel_xy *= gaussian_exp + kernel_xy /= kernel_xx.sum() kernel_yy = kernel_xx.transpose() Hxx = ndimage.convolve(image, kernel_xx, mode=mode, cval=cval) diff --git a/skimage/feature/tests/test_corner.py b/skimage/feature/tests/test_corner.py index 47c6d016..48833f92 100644 --- a/skimage/feature/tests/test_corner.py +++ b/skimage/feature/tests/test_corner.py @@ -10,7 +10,50 @@ from skimage.morphology import octagon from skimage.feature import (corner_moravec, corner_harris, corner_shi_tomasi, corner_subpix, peak_local_max, corner_peaks, corner_kitchen_rosenfeld, corner_foerstner, - corner_fast, corner_orientations) + corner_fast, corner_orientations, + structure_tensor, hessian_matrix) + + +def test_structure_tensor(): + square = np.zeros((5, 5)) + square[2, 2] = 1 + Axx, Axy, Ayy = structure_tensor(square, sigma=0.1) + assert_array_equal(Axx, np.array([[ 0, 0, 0, 0, 0], + [ 0, 1, 0, 1, 0], + [ 0, 4, 0, 4, 0], + [ 0, 1, 0, 1, 0], + [ 0, 0, 0, 0, 0]])) + assert_array_equal(Axy, np.array([[ 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]])) + assert_array_equal(Ayy, np.array([[ 0, 0, 0, 0, 0], + [ 0, 1, 4, 1, 0], + [ 0, 0, 0, 0, 0], + [ 0, 1, 4, 1, 0], + [ 0, 0, 0, 0, 0]])) + + +def test_structure_tensor(): + square = np.zeros((5, 5)) + square[2, 2] = 1 + Hxx, Hxy, Hyy = hessian_matrix(square, sigma=0.1) + assert_array_equal(Hxx, np.array([[ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0], + [ 0, 0, 1, 0, 0], + [ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0]])) + assert_array_equal(Hxy, np.array([[ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0]])) + assert_array_equal(Hyy, np.array([[ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0], + [ 0, 0, 1, 0, 0], + [ 0, 0, 0, 0, 0], + [ 0, 0, 0, 0, 0]])) def test_square_image():