Help with find_boundaries or watershed

Juan Nunez-Iglesias jni.soma at gmail.com
Tue Nov 27 01:40:20 EST 2012


Hi Siggi,

How about replacing

>>> a[find_boundaries(a)]=0

with

>>> s = nd.generate_binary_structure(2, 1)
>>> a[find_boundaries(a) * (nd.grey_erosion(a, footprint=s) != 0)] = 0

?

That will leave you with big objects that are separated with respect to
connectivity=1, and objects that aren't touching will not be "reduced".

The output for your example is:

>>> print a.astype(int)
[[0 0 0 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 3 3 0 0 0 0 0 0 0 0 0 0 0 0]
 [0 3 3 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 0 0]
 [0 0 0 0 1 1 1 1 1 0 0 0 0 0 0]
 [0 0 0 0 1 1 1 1 1 0 0 0 0 0 0]
 [0 0 0 0 1 1 1 0 0 2 2 2 0 0 0]
 [0 0 0 0 1 1 1 0 2 2 2 2 0 0 0]
 [0 0 0 0 1 1 1 0 2 2 2 2 0 0 0]
 [0 0 0 0 0 0 0 2 2 2 2 2 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 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 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 0 0 0]]

Juan.

On Sat, Nov 24, 2012 at 3:13 AM, Sigmund <siggin at gmail.com> wrote:

> Moin moin,
>
> I'm using the find_boundaries function for separating labeled regions
> (separated by the watershed). Afterward I set the as True returned pixels
> to 0.  This works quit well but since my objects (labeled regions) can be
> small (couple pixels) and seldom stick together I would like to limit the
> find_boundaries to connected regions.
> Is there a easy way to only find the boundaries not connected to 0. Or
> tell the watershed function to leave a space between separate objects?
>
> Example:
> import numpy as np
> from scipy import ndimage as nd
> from skimage.segmentation.boundaries import find_boundaries
> a = np.zeros((15,15))
> a[4:9,4:9]=1
> a[6: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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 0 0 0 0 0 0 0 0 0 0 0]]
>
> As you can see, the region 3 is almost gone.
>
> Thanks
> Siggi
>
>  --
>
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/scikit-image/attachments/20121127/9505fa8e/attachment.html>


More information about the scikit-image mailing list