| [mmsedil] [Up] [mmcdil] | Structuring Elements |
Implemented in Python.
mmseunion creates a structuring element from the union of two structuring elements.
>>> b1 = mmseline(5)
>>> mmseshow(b1)
array([0, 0, 0, 0, 1, 1, 1, 1, 1],'1')
>>> b2 = mmsedisk(3)
>>> mmseshow(b2)
array([[0, 0, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 0],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 0, 0]],'1')
>>> b3 = mmseunion(b1,b2)
>>> mmseshow(b3)
array([[0, 0, 0, 1, 1, 1, 0, 0, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 1, 1, 1, 1, 1, 1, 1, 1],
[0, 1, 1, 1, 1, 1, 1, 1, 0],
[0, 0, 1, 1, 1, 1, 1, 0, 0],
[0, 0, 0, 1, 1, 1, 0, 0, 0]],'1')
def mmseunion(B1, B2):
from Numeric import maximum, ones, asarray, NewAxis
assert B1.typecode() == B2.typecode(), 'Cannot have different datatypes:'
type1 = B1.typecode()
if len(B1) == 0: return B2
if len(B1.shape) == 1: B1 = B1[NewAxis,:]
if len(B2.shape) == 1: B2 = B2[NewAxis,:]
if B1.shape <> B2.shape:
inf = mmlimits(B1)[0]
h1,w1 = B1.shape
h2,w2 = B2.shape
H,W = max(h1,h2),max(w1,w2)
Hc,Wc = (H-1)/2,(W-1)/2 # center
BB1,BB2 = asarray(B1),asarray(B2)
B1, B2 = inf * ones((H,W)), inf *ones((H,W))
dh1s , dh1e = (h1-1)/2 , (h1-1)/2 + (h1+1)%2 # deal with even and odd dimensions
dw1s , dw1e = (w1-1)/2 , (w1-1)/2 + (w1+1)%2
dh2s , dh2e = (h2-1)/2 , (h2-1)/2 + (h2+1)%2
dw2s , dw2e = (w2-1)/2 , (w2-1)/2 + (w2+1)%2
B1[ Hc-dh1s : Hc+dh1e+1 , Wc-dw1s : Wc+dw1e+1 ] = BB1
B2[ Hc-dh2s : Hc+dh2e+1 , Wc-dw2s : Wc+dw2e+1 ] = BB2
B = maximum(B1,B2).astype(type1)
return B
| [mmsedil] [Up] [mmcdil] | |
| Copyright (c) 2003, Roberto A. Lotufo, UNICAMP-University of Campinas; Rubens C. Machado, CenPRA-Renato Archer Research Center. |