# [Numpy-discussion] binary thinning

Peter Verveer verveer at embl-heidelberg.de
Wed Dec 8 03:12:04 CST 2004

```Hi Bob,

I noticed two problems:

1) you construct rotations of your structures with the nd_image.rotate
function. That will not work properly because this function uses spline
interpolation, which not necessarily produces exact results. In this
case you need to use another, exact rotation method, see my
implementation below.

2) I think (correct me if I am wrong) that the application of the 8
structures is sequential, not parallel as you do it.

Below I give my implementation, which does not produce the exact same
result as shown on the webpage you refer to, although the result seems
to be a proper skeleton. This may be because the order in which the
structures are applied matters (I think). Could you maybe test that by
permutating the order in which the structures are applied? I would be
interested if this is true and if one can get the same result as on the
webpage by finding the right sequence...

Cheers, Peter

import numarray
from numarray import nd_image

image = numarray.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, 0, 0, 0, 0, 0, 0,
0],
[0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0],
[0, 0, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0,
0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0,
0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0,
0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0,
0],
[0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0,
0],
[0, 0, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0,
0],
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 0,
0],
[0, 0, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 0,
0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
0],
[0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
0],
[0, 0, 0, 1, 1, 1, 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]])

hit1 = numarray.array([[0, 0, 0],
[0, 1, 0],
[1, 1, 1]])
miss1 = numarray.array([[1, 1, 1],
[0, 0, 0],
[0, 0, 0]])
hit2 = numarray.array([[0, 0, 0],
[1, 1, 0],
[0, 1, 0]])
miss2 = numarray.array([[0, 1, 1],
[0, 0, 1],
[0, 0, 0]])
hit_list = [hit1, hit2]
miss_list = [miss1, miss2]
for ii in range(6):
hit_list.append(numarray.transpose(hit_list[-2])[::-1, ...])
miss_list.append(numarray.transpose(miss_list[-2])[::-1, ...])

while 1:
last = image
for hit, miss in zip(hit_list, miss_list):
hm = nd_image.binary_hit_or_miss(image, hit, miss)
image = numarray.logical_and(image, numarray.logical_not(hm))
if numarray.abs(last - image).max() == 0:
break

print image

On 8 Dec 2004, at 00:20, Bob Klimek wrote:

> Hi,
>
> I've been playing around with the binary_hit_or_miss function in the
> nd_image and it seems to work well in general. I understand that
> binary thinning can be generated by using hit_or_miss, however I can't
> seem to get it to work right. Anyone have a code snippet that does the
> job?
>
> I follow the procedure at:
>
> http://homepages.inf.ed.ac.uk/rbf/HIPR2/thin.htm
>
> My implementation is as follows:
>
>        ## from HIPR2 web site
>              ## [0,0,0]
>        ## [x,1,x]
>        ## [1,1,1]
>        struct1 = numarray.array(
>        [[0, 0, 0],
>        [0, 1, 0],
>        [1, 1, 1]])
>        struct2 = numarray.array(
>        [[1, 1, 1],
>        [0, 0, 0],
>        [0, 0, 0]])
>
>        ## [x,0,0]
>        ## [1,1,0]
>        ## [x,1,x]
>        struct3 = numarray.array(
>        [[0, 0, 0],
>        [1, 1, 0],
>        [0, 1, 0]])
>        struct4 = numarray.array(
>        [[0, 1, 1],
>        [0, 0, 1],
>        [0, 0, 0]])
>
>        b = []
>        for i in range(4):
>            temp = ND.binary_hit_or_miss(a, struct1, struct2)
>            b.append(temp)
>            temp = ND.binary_hit_or_miss(a, struct3, struct4)
>            b.append(temp)
>
>            struct1 = ND.rotate(struct1, 90)
>            struct2 = ND.rotate(struct2, 90)
>            struct3 = ND.rotate(struct3, 90)
>            struct4 = ND.rotate(struct4, 90)
>
>        result = b[0]
>        for i in range(7):
>            result = numarray.logical_or(result, b[i+1])
>
>        result = a - result
>
>
> Bob
>
>
>
> -------------------------------------------------------
> SF email is sponsored by - The IT Product Guide
> Read honest & candid reviews on hundreds of IT Products from real
> users.
> Discover which products truly live up to the hype. Start reading now.
> http://productguide.itmanagersjournal.com/
> _______________________________________________
> Numpy-discussion mailing list
> Numpy-discussion at lists.sourceforge.net
> https://lists.sourceforge.net/lists/listinfo/numpy-discussion

```