From cf3463fb203af0dbf771b40d9e26cf2aa5019324 Mon Sep 17 00:00:00 2001 From: Luis Pedro Coelho Date: Mon, 6 Apr 2015 21:04:04 +0200 Subject: [PATCH] BUG Fix bounds in sum_rect() Without this fix, it was possible to trigger a crash from the interpreter, which, even if it was an edge case, is always a serious bug. closes #58 --- mahotas/features/_surf.cpp | 4 ++-- mahotas/tests/test_surf.py | 9 ++++++++- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/mahotas/features/_surf.cpp b/mahotas/features/_surf.cpp index 76f7abd9..4c82587b 100644 --- a/mahotas/features/_surf.cpp +++ b/mahotas/features/_surf.cpp @@ -39,8 +39,8 @@ template double sum_rect(numpy::aligned_array integral, int y0, int x0, int y1, int x1) { y0 = std::max(y0-1, 0); x0 = std::max(x0-1, 0); - y1 = std::min(y1-1, integral.dim(0)); - x1 = std::min(x1-1, integral.dim(1)); + y1 = std::min(y1-1, integral.dim(0) - 1); + x1 = std::min(x1-1, integral.dim(1) - 1); const T A = integral.at(y0,x0); const T B = integral.at(y0,x1); diff --git a/mahotas/tests/test_surf.py b/mahotas/tests/test_surf.py index e8ce3f6e..8ed6bdd5 100644 --- a/mahotas/tests/test_surf.py +++ b/mahotas/tests/test_surf.py @@ -22,7 +22,7 @@ def test_integral2(): def test_sum_rect(): f = np.arange(800*160).reshape((800,160)) % 7 - fi = surf.integral(f.copy()) + fi = surf.integral(f.copy()) np.random.seed(22) for i in range(100): @@ -32,6 +32,13 @@ def test_sum_rect(): x1 = np.random.randint(x0+1, 159) assert _surf.sum_rect(fi, y0, x0, y1, x1) == f[y0:y1, x0:x1].sum() +def test_sum_rect_edge(): + # regression + # https://github.com/luispedro/mahotas/issues/58 + f = np.arange(80*60).reshape((80,60)) % 7 + fi = surf.integral(f.copy()) + _surf.sum_rect(fi, 0, 0, 81, 61) + def test_surf_guassians(): f = np.zeros((1024,1024)) Y,X = np.indices(f.shape)