Function iaareaopen

Synopse

Area opening

  • y = iaareaopen(f, a, Bc=iasecross())
    • y: Image
    • f: Image
    • a: Double non negative integer.
    • Bc: Structuring Element ( connectivity).
01. import numpy as np
02. import ia870 as MT
03. from iasecross import iasecross
04. def iaareaopen_eq(f, a, Bc=iasecross()):
05. 
06.     if f.dtype == np.bool:
07.       fr = MT.ialabel(f,Bc)      # binary area open, use area measurement
08.       g = MT.iablob(fr,'area')
09.       y = g >= a
10.     else:
11.       y = np.zeros_like(f)
12.       k1 = f.min()
13.       k2 = f.max()
14.       for k in xrange(k1,k2+1):   # gray-scale, use thresholding decomposition
15.         fk = (f >= k)
16.         fo = MT.iaareaopen(fk,a,Bc)
17.         if not fo.any():
18.           break
19.         y = MT.iaunion(y, MT.iagray(fo,f.dtype,k))
20.     return y
01. def find_area(area, i):
02.     lista = []
03.     while area[i] >= 0:
04.         lista.append(i)
05.         i = area[i]
06.     area[lista] = i
07.     return i
08. 
09. def iaareaopen(f,a,Bc=iasecross()):
10.     a = -a
11.     s = f.shape
12.     g = np.zeros_like(f).ravel()
13.     f1 = np.concatenate((f.ravel(),np.array([0])))
14.     area = -np.ones((f1.size,), np.int32)
15.     N = MT.iaNlut(s, MT.iase2off(Bc))
16.     pontos = f1.nonzero()[0]
17.     pontos = pontos[np.lexsort((np.arange(0,-len(pontos),-1),f1[pontos]))[::-1]]
18.     for p in pontos:
19.         for v in N[p]:
20.             if f1[p] < f1[v] or (f1[p] == f1[v] and v < p):
21.                 rv = find_area(area, v)
22.                 if rv != p:
23.                     if area[rv] > a or f1[p] == f1[rv]:
24.                         area[p] = area[p] + area[rv]
25.                         area[rv] = p
26.                     else:
27.                         area[p] = a
28.     for p in pontos[::-1]:
29.         if area[p] >= 0:
30.             g[p] = g[area[p]]
31.         else:
32.             if area[p] <= a:
33.                 g[p] = f1[p]
34.     return g.reshape(s)

Description

iaareaopen removes any grain (i.e., connected component) with area less than a of a binary image f. The connectivity is given by the structuring element Bc. This operator is generalized to gray-scale images by applying the binary operator successively on slices of f taken from higher threshold levels to lower threshold levels. For a faster implementation use iamxtareaopen

Examples

Numeric binary example

01. from ia870 import iaareaopen
02. from ia870 import iabinary
03. from ia870 import iasecross
04. 
05. 
06. f=array([
07.  [1, 1, 0, 0, 0, 0, 1],
08.  [1, 0, 1, 1, 1, 0, 1],
09.  [0, 0, 0, 0, 1, 0, 0]],bool)
10. y=iaareaopen(f,4,iasecross())
11. print y * 1
[[0 0 0 0 0 0 0]
 [0 0 1 1 1 0 0]
 [0 0 0 0 1 0 0]]

Numeric gray-level example

1. f=array([
2.    [10,   11,   0,    0,   0,   0,  20],
3.    [10,    0,   5,    8,   9,   0,  15],
4.    [10,    0,   0,    0,  10,   0,   0]],uint8)
5. y=iaareaopen(f,4,iasecross())
6. print y
[[10 10  0  0  0  0  0]
 [10  0  5  5  5  0  0]
 [10  0  0  0  5  0  0]]

Binary image example

1. a = adreadgray('form-1.tif')>0
2. b = iaareaopen(a,500)
3. adshow(a)
4. adshow(b)

Gray-level image example

This example uses gray scale area open.

1. import ia870 as MT
2. 
3. a = adreadgray('bloodcells.tif')
4. b = MT.iaareaopen(a,500,MT.iasecross())
5. adshow(a, 'Original')
6. adshow(b, 'Filtered')

Original

Filtered

Equation

See Also