Skip to content

Commit

Permalink
BUG Fix crash with close_holes
Browse files Browse the repository at this point in the history
The code did not check correctly for whether the input was 2
dimensional.
  • Loading branch information
luispedro committed Jul 4, 2014
1 parent ac79044 commit a292748
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 6 deletions.
3 changes: 3 additions & 0 deletions ChangeLog
@@ -1,3 +1,6 @@
Version 1.1.1 2014-07-04 by luispedro
* Fix crash in close_holes() with nD images (for n > 2)

Version 1.1.0 2014-02-12 by luispedro
* Better error checking
* Fix interpolation of integer images using order 1
Expand Down
5 changes: 4 additions & 1 deletion mahotas/internal.py
@@ -1,4 +1,4 @@
# Copyright (C) 2011-2012, Luis Pedro Coelho <luis@luispedro.org>
# Copyright (C) 2011-2014, Luis Pedro Coelho <luis@luispedro.org>
# vim: set ts=4 sts=4 sw=4 expandtab smartindent:
#
# License: MIT (see COPYING file)
Expand Down Expand Up @@ -166,3 +166,6 @@ def _check_3(arr, funcname):
if arr.ndim != 3 or arr.shape[2] != 3:
raise ValueError('mahotas.%s: this function expects an array of shape (h, w, 3), received an array of shape %s.' % (funcname, arr.shape))

def _check_2(arr, funcname):
if arr.ndim != 2:
raise ValueError('mahotas.%s: this function can only handle 2D arrays (passed array with shape %s).' % (funcname, arr.shape))
7 changes: 4 additions & 3 deletions mahotas/morph.py
Expand Up @@ -6,7 +6,7 @@
from __future__ import division
import numpy as np

from .internal import _get_output, _verify_is_integer_type
from .internal import _get_output, _verify_is_integer_type, _check_2
from . import _morph

__all__ = [
Expand Down Expand Up @@ -273,10 +273,10 @@ def cwatershed(surface, markers, Bc=None, return_lines=False):
W,WL = cwatershed(surface, markers, Bc=None, return_lines=True)
Seeded watershed in n-dimensions
This function computes the watershed transform on the input surface (which
may actually be an n-dimensional volume).
This function requires initial seed points. A traditional way of
initializing watershed is to use regional minima::
Expand Down Expand Up @@ -477,6 +477,7 @@ def close_holes(ref, Bc=None):
closed : ndarray
superset of `ref` (i.e. with closed holes)
'''
_check_2(ref, 'close_holes')
ref = np.ascontiguousarray(ref, dtype=np.bool_)
Bc = get_structuring_elem(ref, Bc)
return _morph.close_holes(ref, Bc)
Expand Down
13 changes: 11 additions & 2 deletions mahotas/tests/test_morph.py
@@ -1,7 +1,9 @@
import mahotas as mh
import numpy as np
from mahotas.morph import get_structuring_elem, subm, tophat_open, tophat_close
from nose.tools import raises


def test_get_structuring_elem():
A = np.zeros((10,10), np.bool)
Bc = np.ones((4,4), dtype=np.bool)
Expand All @@ -24,7 +26,7 @@ def bad_dims():
get_structuring_elem(A, Bc)

bad_dims()


def test_open():
from mahotas.morph import open
Expand Down Expand Up @@ -201,7 +203,7 @@ def test_distance_multi():

def test_disk():
from mahotas.morph import disk
D2 = disk(2)
D2 = disk(2)
assert D2.shape[0] == D2.shape[1]
assert D2.shape == (5,5)
assert not D2[0,0]
Expand All @@ -226,3 +228,10 @@ def test_negative_dim(dim):
test_negative_dim(-2)
test_negative_dim(-1)
test_negative_dim(0)


@raises(ValueError)
def test_close_holes_3d():
'Close holes should raise exception with 3D inputs'
f = np.random.rand(100,100,3) > .9
mh.close_holes(f)

0 comments on commit a292748

Please sign in to comment.