Actions
Morph-M Python - Basic Operator » History » Revision 1
Revision 1/4
| Next »
Serge Koudoro, 10/26/2009 11:27 AM
Morph-M Python - Basic Operator¶
This following example show you, by 8 bits image Inversion, how you can create and call your operator.
# 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) :
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
#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 · 1 revisions