Morph-M Python - Python Pixel Manipulation » History » Revision 2
Revision 1 (Serge Koudoro, 10/23/2009 03:43 PM) → Revision 2/3 (Serge Koudoro, 10/26/2009 01:51 PM)
h1. Morph-M Python - Python Pixel Manipulation
h2{color:#8B0000;background:#ddd}. Introduction
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++.
h2{color:#8B0000;background:#ddd}. From the image
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.
|_. Example Gray Image |_. Example Color Image |
|<pre><code class="ruby">
>>> im = mp.fileRead("testGray.png")
>>> offs = GetOffsetFromCoords(im, (x,y,z))
>>> print im.getPixel(offs)
235
>>> im.setPixel(offs, 255) </code></pre>|<pre><code class="ruby">
>>> im = mp.fileRead("lena_std.png")
>>> offs = GetOffsetFromCoords(im, (x,y,z))
>>> print im.getPixel(offs)
(235,236,237)
>>> im.setPixel(offs,(255,255,255))</code></pre>|
h2{color:#8B0000;background:#ddd}. Iterators
*_Iterators_* also have _getPixel_ and _setPixel_. They also have the _getOffset_ that is used often in morphological algorithms.
_+Example Iterator:+_
<pre><code class="ruby">
def MyErosion( imIn, nl ,imOut ):
""" Personnal erosion with iterator """
itIn = imIn.imageData()
itOut = imOut.imageData()
neighb = createNeighborhood( imIn, nl )
while itIn.isNotFinished() and itOut.isNotFinished():
neighb.setCenter( itIn )
# "min" work with iterator
itOut.setPixel( min( neighb.imageData() ) )
# Do not forget!
itIn.next()
itOut.next()
if __name__=='__main__':
im = fileRead(images_dir+"/Gray/foreman.png")
imEro = getSame(im)
imEroRef = getSame(im)
nl = NeighborList.neighborsSquare2D
MyErosion( im, nl, imEro )
ImErode( im, nl, imEroRef)
assert(isEqual(imEro, imEroRef))
</code></pre>
h2{color:#8B0000;background:#ddd}. Iterators with Mask