Project

General

Profile

Morph-M Python - Basic Operator » History » Version 4

Serge Koudoro, 10/26/2009 02:39 PM

1 1 Serge Koudoro
h1. Morph-M Python - Basic Operator
2
3
This following example show you, by 8 bits image Inversion, how you can create and call your operator.
4
5
<pre><code class="ruby">
6 2 Serge Koudoro
#Don't forget morphee importation
7
import morphee
8
9 1 Serge Koudoro
# pixel's inversion Operator (8 bits version) :
10
def invert8(val):
11
	return 255-val
12
	
13
# image inversion function:
14
def testInvert8(im):
15
	morphee.ImUnaryOperation(im,invert8,im)
16
17
# lambda version :
18
def testInvert8_lambda(im):
19
	# instead of invert8, we can use a lambda-fonction  
20
	# Better and lighter
21
	morphee.ImUnaryOperation(im, lambda x:255-x,im)
22 3 Serge Koudoro
23
im8=morphee.fileRead(os.path.join(images_dir_gray,"foreman.png"))
24
testInvert8(im8)
25
morphee.fileWrite(im8,os.path.join(temp_dir,"invert8.png"))
26
testInvert8_lambda(im8)
27
morphee.fileWrite(im8,os.path.join(temp_dir,"foreman.png"))
28 1 Serge Koudoro
</code></pre>
29
30
An other example: Color conversion (RGB to Gray) : 
31
32
<pre><code class="ruby">
33 2 Serge Koudoro
#Don't forget morphee importation
34
import morphee
35
36 1 Serge Koudoro
def RGBtoGray(valRGB):
37
	# valRGB est normalement un pixel_3<UINT8> converti
38
	# en un 3-tuple.
39
	assert(type(valRGB)==type((),))# Check type (we need tuple)
40
	assert(len(valRGB)==3)# Check if it is 3-tuple
41
	
42
	# Hmm, beautiful conversion !
43
	return (valRGB[0]+valRGB[1]+valRGB[2])/3
44
45
def testRGBtoGray(imRGB,imGray):
46
	morphee.ImUnaryOperation(imRGB,RGBtoGray,imGray)
47 3 Serge Koudoro
48
imRGB=morphee.fileRead(os.path.join(images_dir_color,"ours1.bmp"))
49
imRGB_Gray=morphee.getSameOf(imRGB,morphee.dataCategory.dtScalar,morphee.scalarDataType.sdtUINT8)
50
testRGBtoGray(imRGB,imRGB_Gray)
51
morphee.fileWrite(imRGB_Gray,os.path.join(temp_dir,"rgbtogray.png"))
52 1 Serge Koudoro
</code></pre>
53
54
This example show you an method to construct an operator by using class 
55
56
<pre><code class="ruby">
57 2 Serge Koudoro
#Don't forget morphee importation
58
import morphee
59
60 1 Serge Koudoro
#Add a constant
61
class AddNum:
62
	def __init__(self, n):
63
		self.number=n
64
	def __call__(self, val):
65
		if val+self.number > 255:
66
			return 255
67
		else:
68
			return val+self.number
69
70
def testAddCte(im, k):
71
	op=AddNum(100)
72
	# The __call__() function is simply used to
73
        #call on a callable object like the callback() 
74
        #function outside the class
75
	morphee.ImUnaryOperation(im,op, im)
76 3 Serge Koudoro
77
im8=morphee.pngFileRead(os.path.join(images_dir_gray,"foreman.png"))
78
testAddCte(im8,100)
79
morphee.fileWrite(im8,os.path.join(temp_dir,"add100.png"))
80 1 Serge Koudoro
</code></pre>
81 4 Serge Koudoro
82
You can create *Binary Operator*
83
84
<pre><code class="ruby">
85
#Don't forget morphee importation
86
from morphee import *
87
88
im = morphee.createImage( dataCategory.dtScalar,scalarDataType.sdtUINT32 )
89
im.setSize(3,3)
90
im.allocateImage()
91
imOut=morphee.getSame(self.im)
92
L=[ 1,2,3,4,5,6,7,8,9]
93
im.fromList(L)
94
95
im2 = getSame(im)
96
ImCopy(im,im2)	
97
98
im3 = getSame(im)
99
100
#Lambda function with our operator which add 2 images 
101
ImBinaryOperation(self.im,self.im2, lambda x,y:x+y, self.im3)
102
103
#test result
104
for p1,p3 in zip(self.im.imageData(),self.im3.imageData()):
105
			self.assertEqual(p3,2*p1)
106
</code></pre>