Morph-M Python - Basic Operator » History » Version 1
Serge Koudoro, 10/26/2009 11:27 AM
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 | # pixel's inversion Operator (8 bits version) : |
||
7 | def invert8(val): |
||
8 | return 255-val |
||
9 | |||
10 | # image inversion function: |
||
11 | def testInvert8(im): |
||
12 | morphee.ImUnaryOperation(im,invert8,im) |
||
13 | |||
14 | # lambda version : |
||
15 | def testInvert8_lambda(im): |
||
16 | # instead of invert8, we can use a lambda-fonction |
||
17 | # Better and lighter |
||
18 | morphee.ImUnaryOperation(im, lambda x:255-x,im) |
||
19 | </code></pre> |
||
20 | |||
21 | An other example: Color conversion (RGB to Gray) : |
||
22 | |||
23 | <pre><code class="ruby"> |
||
24 | def RGBtoGray(valRGB): |
||
25 | # valRGB est normalement un pixel_3<UINT8> converti |
||
26 | # en un 3-tuple. |
||
27 | assert(type(valRGB)==type((),))# Check type (we need tuple) |
||
28 | assert(len(valRGB)==3)# Check if it is 3-tuple |
||
29 | |||
30 | # Hmm, beautiful conversion ! |
||
31 | return (valRGB[0]+valRGB[1]+valRGB[2])/3 |
||
32 | |||
33 | def testRGBtoGray(imRGB,imGray): |
||
34 | morphee.ImUnaryOperation(imRGB,RGBtoGray,imGray) |
||
35 | </code></pre> |
||
36 | |||
37 | This example show you an method to construct an operator by using class |
||
38 | |||
39 | <pre><code class="ruby"> |
||
40 | #Add a constant |
||
41 | class AddNum: |
||
42 | def __init__(self, n): |
||
43 | self.number=n |
||
44 | def __call__(self, val): |
||
45 | if val+self.number > 255: |
||
46 | return 255 |
||
47 | else: |
||
48 | return val+self.number |
||
49 | |||
50 | def testAddCte(im, k): |
||
51 | op=AddNum(100) |
||
52 | # The __call__() function is simply used to |
||
53 | #call on a callable object like the callback() |
||
54 | #function outside the class |
||
55 | morphee.ImUnaryOperation(im,op, im) |
||
56 | </code></pre> |