Actions
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 |
---|---|
|
|
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))
Iterators with Mask¶
#Create your original image
im = createImage( dataCategory.dtScalar,scalarDataType.sdtUINT8 )
im.setSize(3,3)
im.allocateImage()
L=[ 1,2,3,4,5,6,7,8,9]
im.fromList(L)
#Create your mask
im_mask = createImage( dataCategory.dtScalar,scalarDataType.sdtUINT8 )
im_mask.setSize(3,3)
im_mask.allocateImage()
L=[ 0,1,0,1,1,1,0,1,0]
im_mask.fromList(L)
#create a mask iterator
mask_iter = im.imageDataWithMask(im_mask, 1, compareOp.Equal)
while(mask_iter.isNotFinished()):
mask_iter.setPixel(mask_iter.getOffset(),0)
mask_iter.next()
Updated by Serge Koudoro about 15 years ago ยท 3 revisions