Morph-M Python - Python Pixel Manipulation » History » Version 2
Serge Koudoro, 10/26/2009 01:51 PM
1 | 1 | Serge Koudoro | h1. Morph-M Python - Python Pixel Manipulation |
---|---|---|---|
2 | |||
3 | h2{color:#8B0000;background:#ddd}. Introduction |
||
4 | |||
5 | Although the speed of an algorithm entirely written in MorpheePython is usually abysmal, it is very convenient for rapid development. Most functions that operate directly on pixels have been ported from C++. |
||
6 | |||
7 | h2{color:#8B0000;background:#ddd}. From the image |
||
8 | |||
9 | Two functions are available to access pixel data from an image: *_getPixel_* (possibly also known as pixelFromOffset) and *_setPixel_*. They mirror the C++ method *_pixelFromOffset_*. These functions are not currently range-checked. |
||
10 | |||
11 | |_. Example Gray Image |_. Example Color Image | |
||
12 | |<pre><code class="ruby"> |
||
13 | >>> im = mp.fileRead("testGray.png") |
||
14 | >>> offs = GetOffsetFromCoords(im, (x,y,z)) |
||
15 | >>> print im.getPixel(offs) |
||
16 | 235 |
||
17 | >>> im.setPixel(offs, 255) </code></pre>|<pre><code class="ruby"> |
||
18 | >>> im = mp.fileRead("lena_std.png") |
||
19 | >>> offs = GetOffsetFromCoords(im, (x,y,z)) |
||
20 | >>> print im.getPixel(offs) |
||
21 | (235,236,237) |
||
22 | >>> im.setPixel(offs,(255,255,255))</code></pre>| |
||
23 | |||
24 | h2{color:#8B0000;background:#ddd}. Iterators |
||
25 | |||
26 | *_Iterators_* also have _getPixel_ and _setPixel_. They also have the _getOffset_ that is used often in morphological algorithms. |
||
27 | |||
28 | _+Example Iterator:+_ |
||
29 | |||
30 | <pre><code class="ruby"> |
||
31 | def MyErosion( imIn, nl ,imOut ): |
||
32 | """ Personnal erosion with iterator """ |
||
33 | |||
34 | itIn = imIn.imageData() |
||
35 | itOut = imOut.imageData() |
||
36 | |||
37 | neighb = createNeighborhood( imIn, nl ) |
||
38 | |||
39 | while itIn.isNotFinished() and itOut.isNotFinished(): |
||
40 | |||
41 | neighb.setCenter( itIn ) |
||
42 | |||
43 | # "min" work with iterator |
||
44 | itOut.setPixel( min( neighb.imageData() ) ) |
||
45 | |||
46 | # Do not forget! |
||
47 | itIn.next() |
||
48 | itOut.next() |
||
49 | |||
50 | if __name__=='__main__': |
||
51 | im = fileRead(images_dir+"/Gray/foreman.png") |
||
52 | |||
53 | imEro = getSame(im) |
||
54 | imEroRef = getSame(im) |
||
55 | |||
56 | nl = NeighborList.neighborsSquare2D |
||
57 | |||
58 | MyErosion( im, nl, imEro ) |
||
59 | ImErode( im, nl, imEroRef) |
||
60 | assert(isEqual(imEro, imEroRef)) |
||
61 | |||
62 | </code></pre> |
||
63 | 2 | Serge Koudoro | |
64 | h2{color:#8B0000;background:#ddd}. Iterators with Mask |