Project

General

Profile

Actions

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

« Previous | Revision 2/4 (diff) | Next »
Serge Koudoro, 10/26/2009 11:29 AM


Morph-M Python - Basic Operator

This following example show you, by 8 bits image Inversion, how you can create and call your operator.

#Don't forget morphee importation
import morphee

# pixel's inversion Operator (8 bits version) :
def invert8(val):
    return 255-val

# image inversion function:
def testInvert8(im):
    morphee.ImUnaryOperation(im,invert8,im)

# lambda version :
def testInvert8_lambda(im):
    # instead of invert8, we can use a lambda-fonction  
    # Better and lighter
    morphee.ImUnaryOperation(im, lambda x:255-x,im)

An other example: Color conversion (RGB to Gray) :

#Don't forget morphee importation
import morphee

def RGBtoGray(valRGB):
    # valRGB est normalement un pixel_3<UINT8> converti
    # en un 3-tuple.
    assert(type(valRGB)==type((),))# Check type (we need tuple)
    assert(len(valRGB)==3)# Check if it is 3-tuple

    # Hmm, beautiful conversion !
    return (valRGB[0]+valRGB[1]+valRGB[2])/3

def testRGBtoGray(imRGB,imGray):
    morphee.ImUnaryOperation(imRGB,RGBtoGray,imGray)

This example show you an method to construct an operator by using class

#Don't forget morphee importation
import morphee

#Add a constant
class AddNum:
    def __init__(self, n):
        self.number=n
    def __call__(self, val):
        if val+self.number > 255:
            return 255
        else:
            return val+self.number

def testAddCte(im, k):
    op=AddNum(100)
    # The __call__() function is simply used to
        #call on a callable object like the callback() 
        #function outside the class
    morphee.ImUnaryOperation(im,op, im)

Updated by Serge Koudoro about 15 years ago · 2 revisions