Project

General

Profile

Actions

Morph-M Python - Python Pixel Manipulation » History » Revision 1

Revision 1/3 | Next »
Serge Koudoro, 10/23/2009 03:43 PM


Morph-M Python - Python Pixel Manipulation

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++.

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

>>> im = mp.fileRead("testGray.png")
>>> offs = GetOffsetFromCoords(im, (x,y,z))
>>> print im.getPixel(offs)
235
>>> im.setPixel(offs, 255) 

>>> 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))

Iterators

Iterators also have getPixel and setPixel. They also have the getOffset that is used often in morphological algorithms.

Example Iterator:

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))

Updated by Serge Koudoro over 14 years ago · 1 revisions