Project

General

Profile

Actions

Morph-M Python - Python Active Windows » History » Revision 3

« Previous | Revision 3/4 (diff) | Next »
Jean Felder, 03/16/2010 06:28 PM


Morph-M Python - Python Active Windows

Introduction

What Active windows does ?

Sometimes, you do not need to work on the whole image. Active window is here to help you ! Define 1,2,3 or more active window on your image, then get all these pixel and work with them.

Example images are better than words, so try all example.

Example: Construct 3D volume with image stack

Example : Build 1 image with some part of 2 different images

import morphee as mp

def combineImage(ImIn1,ImIn2):
        #we activate the same window on each image (Red windows)
        ImIn1.setActiveWindow(201,14,1,30,30,1)
        ImIn2.setActiveWindow(201,14,1,30,30,1)

        #copy only active window
        mp.ImCopy(ImIn1,ImIn2)

        # remove active window so all image is actived
        ImIn1.resetActiveWindow()
        ImIn2.resetActiveWindow()

        #Now, we activate the same window on each image(Blue windows)
        ImIn1.setActiveWindow(45,73,0,50,40,1)
        ImIn2.setActiveWindow(45,73,0,50,40,1)

        #copy only active window
        mp.ImCopy(ImIn1,ImIn2)

        #remove active window so all image is actived
        ImIn1.resetActiveWindow()
        ImIn2.resetActiveWindow()

        return ImIn2

if __name__=='__main__':
    #Read im1 and im2.
    im1=mp.fileRead(images_dir+ "\\Gray\\tools.png")
    im2=mp.fileRead(images_dir+ "\\Gray\\tw.png")

    final_image = combineImage(im1,im2)

    #write result
    mp.fileWrite(final_image,"D:\\final_image.png")

Example : Mathematical Morphology operation on Active window

import morphee as mp

def ErodeImagePart(im):
        imEro = mp.getSame(im)

        #create structuring element
        nl = mp.NeighborList.neighborsSquare2D

        #we activate the same window on each image (Red box)
        im.setActiveWindow(1,1,1,66,66,1)
        imEro.setActiveWindow(1,1,1,66,66,1)

        #Erode active window
        mp.ImErode( im, mp.HomotheticSE(nl,10), imEro)

        # remove active window so all image is actived
        im.resetActiveWindow()
        imEro.resetActiveWindow()

        return imEro

if __name__=='__main__':
    #Read image
    im=mp.fileRead(images_dir+ "\\Bin\\balls.png")

    final_image = ErodeImagePart(im)

    #write result
    mp.fileWrite(final_image,"D:\\final_image.png")

Example : Set image Boundary with Active window

import morphee as mp

def set_boundary(im, depth, value = 255):

        for i in [(0,0,0),(im.wxSize-depth, 0, 0)]:
                im.setActiveWindow(i[0], i[1], i[2], depth, 
                                          im.wySize,im.wzSize)
                mp.ImSetConstant(im, value)
                im.resetActiveWindow()
        for i in [(0, 0, 0),(0, im.wySize-depth, 0)]:
                im.setActiveWindow(i[0], i[1], i[2], im.wxSize, 
                                                   depth,im.wzSize)
                mp.ImSetConstant(im, value)
                im.resetActiveWindow()
        if im.getZSize() == 3:
                for i in [(0, 0, 0),(0, 0, im3d.wzSize-depth)]:
                        im.setActiveWindow(i[0], i[1], i[2], im.wxSize,
                                                          im.wySize,depth)
                        mp.ImSetConstant(im, value)
                        im.resetActiveWindow()
        return im

if __name__=='__main__':
    #Read image
    im=mp.fileRead(images_dir+ "\\Gray\\tw.png")

    final_image = set_boundary(im, 50, 0):

    #write result
    mp.fileWrite(final_image,"D:\\final_image.png")

Of active window, coordinates and offsets

We have already seen how to create and use an Active Window.
However, we can notice that an active Window defines a new system of coordinates.
That's why, it could be interesting to use offsets and Coordinates in this new system.
Two operations can be defined:
  • coordinates to offset transformation
  • offset to coordinates transformation

coordinates to offset

Let's consider the following example:

import morphee as mp

# I create a new image, size: 200x200x1
imIn = mp.createImage(mp.dataCategory.dtScalar, mp.scalarDataType.sdtUINT8)
imIn.setSize(200, 200)
imIn.allocateImage()

# Let's get an offset from a pixel inside the image:
print "offset at (50, 23):", mp.GetOffsetFromCoords( imIn, (50, 23, 0) )

# Let's define an active Window now !
# It begins at (50, 20, 0), length : (10, 10, 1)
imIn.setActiveWindow(50, 20, 0, 10, 10, 1)

# Let's get an offset inside the active Window
print "Active Window, offset at (0, 3):", mp.GetOffsetFromWindowCoords( imIn, (0, 3, 0) )

If we try to launch the previous example, we'll get as expected :

offset at (50, 23): 4650
Active Window, offset at (0, 3): 4650

From the previous example, we can notice that an offset is a global measure ! It means that your offset doesn't change when an active window is set.
You just need to remember that your coordinates change with an active Window but not your offset

offset to coordinates

We can now see the inverse function wich transforms an active window into coords.
The following code is quite simple:

import morphee as mp

# I create a new image, size: 200x200x1
imIn = mp.createImage(mp.dataCategory.dtScalar, mp.scalarDataType.sdtUINT8)
imIn.setSize(200, 200)
imIn.allocateImage()

# Let's get coords from an offset inside the image:
print "coords without active window:", mp.GetCoordsFromOffset( imIn, 4258 )

# Let's define an active Window now !
# It begins at (50, 20, 0), length : (10, 10, 1)
imIn.setActiveWindow(50, 20, 0, 10, 10, 1)

# Let's get coords inside the active Window
print "coords with active window:", mp.GetRelativeCoordsFromOffset( imIn, 4258 )

It will produce the following output:

coords without active window: (58, 21, 0)
coords with active window: (8, 1, 0)

Unlike the first operation (coordinates to offset transformation), the offset to coordinates operation will check if
your offset is inside the image. As far as active window are concerned, it means that if you have set an active window,
you can't get coords out of your active window.
For example, this code will return an error:

import morphee as mp

# I create a new image, size: 200x200x1
imIn = mp.createImage(mp.dataCategory.dtScalar, mp.scalarDataType.sdtUINT8)
imIn.setSize(200, 200)
imIn.allocateImage()

# Let's get coords from a pixel inside the image:
# 4258 = 21 * 200 + 58 -> (58, 21, 0)
print "coords without active window:", mp.GetCoordsFromOffset( imIn, 4258 )

# Let's define an active Window now !
# It begins at (50, 10, 0), length : (10, 10, 1)
imIn.setActiveWindow(50, 10, 0, 10, 10, 1)

# Let's get try to get coords outside of the active Window,
# it will return an error !
print "coords with active window, error:", mp.GetRelativeCoordsFromOffset( imIn, 4258 )

The error:

coords without active window: (58, 21, 0)
coords with active window, error:
Traceback (most recent call last):
  File "./test.py", line 19, in <module>
    print "coords with active window, error:", mp.GetRelativeCoordsFromOffset( imIn, 4258 )
RuntimeError: t_GetRelativeCoordsFromOffset : result outside the window

    -- Morphee backtrace:

Updated by Jean Felder almost 15 years ago · 3 revisions