Function iadist

Synopse

Distance transform.

  • y = iadist(f, Bc=ia_secross(), METRIC=NULL)
    • y: Image
    • f: Image
    • Bc: Structuring Element (connectivity): secross() gives city-block, sebox() gives chessboard
    • METRIC: String 'EUCLIDEAN', or 'EUC2' for squared Euclidean.
01. from numpy import *
02. from string import upper
03. from iasecross import iasecross
04. 
05. def iadist(f, Bc=iasecross(), METRIC=None):
06.     from iagray import iagray
07.     from iaintersec import iaintersec
08.     from iaisequal import iaisequal
09.     from iaero import iaero
10.     from iasebox import iasebox
11. 
12.     if METRIC is not None: METRIC = upper(METRIC)
13.     f = iagray(f,'uint16')
14.     y = iaintersec(f,0)
15.     if (METRIC == 'EUCLIDEAN') or (METRIC == 'EUC2'):
16.         f = int32(f)
17.         b = int32(zeros((3,3)))
18.         i=1
19.         while any(f != y):
20.             a4,a2 = -4*i+2, -2*i+1
21.             b = int32([[a4,a2,a4],
22.                        [a2, 0,a2],
23.                        [a4,a2,a4]])
24.             y=f
25.             i=i+1
26.             f = iaero(f,b)
27.         if METRIC == 'EUCLIDEAN':
28.             y = uint16(sqrt(f)+0.5)
29.     else:
30.         if iaisequal(Bc, iasecross()):
31.             b = int32([[-2147483647,  -1, -2147483647],
32.                        [         -1,   0,          -1],
33.                        [-2147483647,  -1, -2147483647]])
34.         elif iaisequal(Bc, iasebox()):
35.             b = int32([[-1,-1,-1],
36.                        [-1, 0,-1],
37.                        [-1,-1,-1]])
38.         else: b = Bc
39.         while any(f != y):
40.             y=f
41.             f = iaero(f,b)
42.     return y

Description

iadist creates the distance image y of the binary image f. The value of y at the pixel x is the distance of x to the complement of f, that is, the distance of x to nearest point in the complement of f. The distances available are based on the Euclidean metrics and on metrics generated by a a regular graph, that is characterized by a connectivity rule defined by the structuring element Bc: 4-connected gives city-block metric and 8-connected gives chessboard metric.

Examples

Example 1

01. from ia870 import iadist
02. from ia870 import iaframe
03. from ia870 import iabinary
04. from ia870 import iasebox
05. from ia870 import ianeg
06. 
07. a = iaframe( iabinary(ones((5,9))),WT=4,HT=2)
08. f4=iadist(a)
09. f8=iadist(a,iasebox())
10. fe=iadist(a,iasebox(),'EUCLIDEAN')
11. print 'a:\n', a
12. print 'f4:\n', f4
13. print 'f8:\n', f8
14. print 'fe:\n', fe
a:
[[ True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True]
 [ True  True  True  True False  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True]
 [ True  True  True  True  True  True  True  True  True]]
f4:
[[6 5 4 3 2 3 4 5 6]
 [5 4 3 2 1 2 3 4 5]
 [4 3 2 1 0 1 2 3 4]
 [5 4 3 2 1 2 3 4 5]
 [6 5 4 3 2 3 4 5 6]]
f8:
[[4 3 2 2 2 2 2 3 4]
 [4 3 2 1 1 1 2 3 4]
 [4 3 2 1 0 1 2 3 4]
 [4 3 2 1 1 1 2 3 4]
 [4 3 2 2 2 2 2 3 4]]
fe:
[[4 4 3 2 2 2 3 4 4]
 [4 3 2 1 1 1 2 3 4]
 [4 3 2 1 0 1 2 3 4]
 [4 3 2 1 1 1 2 3 4]
 [4 4 3 2 2 2 3 4 4]]
1. import ia636
2. a = ones((5,4),'bool')
3. d = iadist(a,iasebox(), 'EUCLIDEAN')
4. print 'info(d):', ia636.iaimginfo(d)
5. print 'd=\n',d
info(d): <type 'numpy.ndarray'> (5, 4) uint16 256.000000 256.000000
d=
[[256 256 256 256]
 [256 256 256 256]
 [256 256 256 256]
 [256 256 256 256]
 [256 256 256 256]]

Example 2

01. from ia870 import ianeg
02. from ia870 import iagradm
03. from ia636 import ianormalize
04. 
05. f = adreadgray('gear.tif') > 0
06. f = ianeg( iagradm(f))
07. d4=iadist(f)
08. d8=iadist(f,iasebox())
09. de=iadist(f,iasebox(),'EUCLIDEAN')
10. adshow(ianormalize(f))
11. adshow(ianormalize(d4%8),'city-block')
12. adshow(ianormalize(d8%8),'chessboard')
13. adshow(ianormalize(de%8),'Euclidean')

city-block

chessboard

Euclidean

Equation

Limitations

To generate useful Distance transforms, the structuring elements must be symmetric and have the origin included. The Euclidean distance transform is rounded to the nearest integer, since it is represented as an unsigned integer image. Use the option EUC2 to compute exact squared Euclidean distance transform.