Project

General

Profile

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

Revision 2 (Serge Koudoro, 10/23/2009 04:22 PM) → Revision 3/4 (Jean Felder, 03/16/2010 06:28 PM)

h1. Morph-M Python - Python Active Windows 

 h2{color:#8B0000;background:#ddd}. 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.    

 h2{color:#8B0000;background:#ddd}. Example: Construct 3D volume with image stack  

 h2{color:#8B0000;background:#ddd}. Example : Build 1 image with some part of 2 different images 

 | !{width:400px}http://morphm.ensmp.fr/attachments/84/active_windows_1.PNG! |<pre><code class="ruby"> 
 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") 
 </code></pre>| 

 h2{color:#8B0000;background:#ddd}. Example : Mathematical Morphology operation on Active window 

 | !{width:400px}http://morphm.ensmp.fr/attachments/85/active_windows_2.PNG! |<pre><code class="ruby"> 
 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") 
 </code></pre>| 

 h2{color:#8B0000;background:#ddd}. Example : Set image Boundary with Active window 

 | !{width:400px}http://morphm.ensmp.fr/attachments/86/active_windows_3.png! |<pre><code class="ruby"> 
 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") 
 </code></pre>| 

 h2{color:#8B0000;background:#ddd}. 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 

 h3{color:black}. +coordinates to offset+ 

 Let's consider the following example: 

 <pre><code class="ruby"> 
 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) ) 
 </code></pre> 

 If we try to launch the previous example, we'll get as expected : 
 <pre> 
 offset at (50, 23): 4650 
 Active Window, offset at (0, 3): 4650 
 </pre> 

 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 

 h3{color:black}. +offset to coordinates+ 

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

 <pre><code class="ruby"> 
 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 ) 
 </code></pre> 

 It will produce the following output: 

 <pre> 
 coords without active window: (58, 21, 0) 
 coords with active window: (8, 1, 0) 
 </pre> 

 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: 

 <pre><code class="ruby"> 
 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 ) 
 </code></pre> 

 The error: 

 <pre> 
 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: 
 </pre>