Project

General

Profile

Actions

Morph-M Python - Neighborhood Operator » History » Revision 2

« Previous | Revision 2/3 (diff) | Next »
Serge Koudoro, 10/26/2009 02:45 PM


Morph-M Python - Neighborhood Operator

Example 1: Erosion. This generic function, create a list with all neighborhord values. These values are send to its operator and rthe return value will be put on center.

def MyErode(imIn, nl, imOut):
    # version lambda:
    morphee.ImNeighborhoodUnaryOperation(imIn, nl, lambda l:min(l), imOut)

    # version that directly use the python function 'min':
    morphee.ImNeighborhoodUnaryOperation(imIn, nl, min, imOut)

if __name__=='__main__':
  im8=morphee.fileRead(os.path.join(images_dir_gray,"foreman.png"))
  imOut=morphee.getSame(im8)
  imOut_2=morphee.getSame(im8)
  nl=morphee.NeighborList.neighborsSquare2D

  # version Python:
  MyErode(im8,nl,imOut)
  # version C++:
  morphee.ImErode(im8,nl,imOut_2)

  # vérification:
  morphee.pngFileWrite(imOut,os.path.join(temp_dir,"ero1.png"))
  morphee.pngFileWrite(imOut_2,os.path.join(temp_dir,"ero2.png"))
  for p1,p2 in zip(imOut.imageData(), imOut_2.imageData()):
    assert(p1==p2)

Filtre de rang:

class RankOrder:
    def __init__(self, quantile):
        # on stocke la valeur du quantile:
        self.quantile=quantile

    def __call__(self, pyt):
        l=list(pyt)

        # l est la liste des valeurs des voisins
        l.sort()
        # conversion quantile/index:
        index=int(len(l)*self.quantile)
        if index >= len(l):# si on dépasse, on prend le dernier
            index=-1# -1 est le dernier élément de la liste
        return l[index]

def ImRankOrder(imIn, nl, quantile, imOut):
    op=RankOrder(quantile)

    morphee.ImNeighborhoodUnaryOperation(imIn,nl,op,imOut)

# min (erosion)
ImRankOrder(im8,nl,0,imOut)
# check results:
morphee.ImErode(im8,nl,imOut_2)
for p1,p2 in zip(imOut.imageData(), imOut_2.imageData()):
    assert(p1==p2)

# max (dilatation)
ImRankOrder(im8,nl,1,imOut)
# check results:
morphee.ImDilate(im8,nl,imOut_2)
for p1,p2 in zip(imOut.imageData(), imOut_2.imageData()):
    assert(p1==p2)

# quantile à 0.25:
ImRankOrder(im8,nl,0.25,imOut)
# check results:
for pOut,pIn in zip(imOut.imageData(), im8.imageData()):
    try:
        assert(pOut<=pIn)
    except:
        pass
        #print "Opérateur non anti-extensif: ",pOut, pIn

Filtre de rang, avec [anti]extensivité garantie

class RankOrderWithExtensivity:
    def __init__(self, quantile):
        self.quantile=quantile
        if quantile > 0.5:    
            self.f=max
        elif quantile < 0.5:
            self.f=min
        else:
            self.f=None

    def __call__(self, pyt,center):
        l=list(pyt)
        l.sort()
        index=int(len(l)*self.quantile)
        if index == len(l):
            index=-1

        if self.f:
            # selon que l'on a choisi un quantile
            # dans la moitié supérieure ou inférieure,
            # on prend le max (resp. min) avec le centre,
            # pour garantir imOut>=imIn (resp <=)
            return self.f(center, l[index])
        else:
            return l[index]

def ImRankOrderWithExtensivity(imIn, nl, quantile, imOut):
    op=RankOrderWithExtensivity(quantile)

    morphee.ImNeighborhoodUnaryOperationWithCenter(imIn,nl,op,imOut)

# quantile à 0.25 sauf si on est pas en-dessous
ImRankOrderWithExtensivity(im8,nl,0.25,imOut)
# Check results
for pOut,pIn in zip(imOut.imageData(), im8.imageData()):
    try:
        assert(pOut<=pIn)
    except:
        print "Opérateur complexe non anti-extensif: ",pOut, pIn

Minkowski Addition (dual dilatation)

def MinkowskiAdditionOperator(pyt,center):
    while pyt.isNotFinished():
        if pyt.getPixel() < center:
            pyt.setPixel(center)
        pyt.next()

def MyMinkowskiAddition(imIn, nl, imOut):
    morphee.ImUnaryOperationOnNeighborhoodWithCenter(imIn,nl,MinkowskiAdditionOperator,imOut)

# Addition de minkowski (dilatation duale)
# version Python:
MyMinkowskiAddition(im8,nl,imOut)
# version C++:
morphee.ImMinkowskiAddition(im8,nl,imOut_2)
morphee.pngFileWrite(imOut,os.path.join(temp_dir,"dil1.png"))
morphee.pngFileWrite(imOut_2,os.path.join(temp_dir,"dil2.png"))
for p1,p2 in zip(imOut.imageData(), imOut_2.imageData()):
    assert(p1==p2)

Updated by Serge Koudoro about 15 years ago · 2 revisions