Project

General

Profile

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

Revision 2 (Serge Koudoro, 10/26/2009 11:29 AM) → Revision 3/4 (Serge Koudoro, 10/26/2009 11:36 AM)

h1. Morph-M Python - Basic Operator 

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

 <pre><code class="ruby"> 
 #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) 

 im8=morphee.fileRead(os.path.join(images_dir_gray,"foreman.png")) 
 testInvert8(im8) 
 morphee.fileWrite(im8,os.path.join(temp_dir,"invert8.png")) 
 testInvert8_lambda(im8) 
 morphee.fileWrite(im8,os.path.join(temp_dir,"foreman.png")) 
 </code></pre> 

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

 <pre><code class="ruby"> 
 #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) 

 imRGB=morphee.fileRead(os.path.join(images_dir_color,"ours1.bmp")) 
 imRGB_Gray=morphee.getSameOf(imRGB,morphee.dataCategory.dtScalar,morphee.scalarDataType.sdtUINT8) 
 testRGBtoGray(imRGB,imRGB_Gray) 
 morphee.fileWrite(imRGB_Gray,os.path.join(temp_dir,"rgbtogray.png")) 
 </code></pre> 

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

 <pre><code class="ruby"> 
 #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) 

 im8=morphee.pngFileRead(os.path.join(images_dir_gray,"foreman.png")) 
 testAddCte(im8,100) 
 morphee.fileWrite(im8,os.path.join(temp_dir,"add100.png")) 
 </code></pre>