Morph-M Python - Basic Operator » History » Version 3
Serge Koudoro, 10/26/2009 11:36 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 | 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> |