Project

General

Profile

Morph-M Python - Python Active Windows » History » Version 2

Serge Koudoro, 10/23/2009 04:22 PM

1 1 Serge Koudoro
h1. Morph-M Python - Python Active Windows
2
3
h2{color:#8B0000;background:#ddd}. Introduction
4
5
*What Active windows does ?*  
6
7
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. 
8
9 2 Serge Koudoro
Example images are better than words, so try all example.   
10 1 Serge Koudoro
11 2 Serge Koudoro
h2{color:#8B0000;background:#ddd}. Example: Construct 3D volume with image stack 
12 1 Serge Koudoro
13 2 Serge Koudoro
h2{color:#8B0000;background:#ddd}. Example : Build 1 image with some part of 2 different images
14
15 1 Serge Koudoro
| !{width:400px}http://morphm.ensmp.fr/attachments/84/active_windows_1.PNG! |<pre><code class="ruby">
16
import morphee as mp
17
18
def combineImage(ImIn1,ImIn2):
19
        #we activate the same window on each image (Red windows)
20
        ImIn1.setActiveWindow(201,14,1,30,30,1)
21
        ImIn2.setActiveWindow(201,14,1,30,30,1)
22
23
        #copy only active window
24
        mp.ImCopy(ImIn1,ImIn2)
25
26
        # remove active window so all image is actived
27
        ImIn1.resetActiveWindow()
28
        ImIn2.resetActiveWindow()
29
30
        #Now, we activate the same window on each image(Blue windows)
31
        ImIn1.setActiveWindow(45,73,0,50,40,1)
32
        ImIn2.setActiveWindow(45,73,0,50,40,1)
33
34
        #copy only active window
35
        mp.ImCopy(ImIn1,ImIn2)
36
37
        #remove active window so all image is actived
38
        ImIn1.resetActiveWindow()
39
        ImIn2.resetActiveWindow()
40
41
        return ImIn2
42
43
if __name__=='__main__':
44
    #Read im1 and im2.
45
    im1=mp.fileRead(images_dir+ "\\Gray\\tools.png")
46
    im2=mp.fileRead(images_dir+ "\\Gray\\tw.png")
47
48
    final_image = combineImage(im1,im2)
49
50
    #write result
51
    mp.fileWrite(final_image,"D:\\final_image.png")
52
</code></pre>|
53
54 2 Serge Koudoro
h2{color:#8B0000;background:#ddd}. Example : Mathematical Morphology operation on Active window
55 1 Serge Koudoro
56
| !{width:400px}http://morphm.ensmp.fr/attachments/85/active_windows_2.PNG! |<pre><code class="ruby">
57
import morphee as mp
58
59
def ErodeImagePart(im):
60
        imEro = mp.getSame(im)
61
62
        #create structuring element
63
        nl = mp.NeighborList.neighborsSquare2D
64
65
        #we activate the same window on each image (Red box)
66
        im.setActiveWindow(1,1,1,66,66,1)
67
        imEro.setActiveWindow(1,1,1,66,66,1)
68
        
69
        #Erode active window
70
        mp.ImErode( im, mp.HomotheticSE(nl,10), imEro)
71
        
72
        # remove active window so all image is actived
73
        im.resetActiveWindow()
74
        imEro.resetActiveWindow()
75
76
        return imEro
77
78
if __name__=='__main__':
79
    #Read image
80
    im=mp.fileRead(images_dir+ "\\Bin\\balls.png")
81
82
    final_image = ErodeImagePart(im)
83
84
    #write result
85
    mp.fileWrite(final_image,"D:\\final_image.png")
86
</code></pre>|
87
88 2 Serge Koudoro
h2{color:#8B0000;background:#ddd}. Example : Set image Boundary with Active window
89 1 Serge Koudoro
90
| !{width:400px}http://morphm.ensmp.fr/attachments/86/active_windows_3.png! |<pre><code class="ruby">
91
import morphee as mp
92
93
def set_boundary(im, depth, value = 255):
94
        
95
        for i in [(0,0,0),(im.wxSize-depth, 0, 0)]:
96
                im.setActiveWindow(i[0], i[1], i[2], depth, 
97
                                          im.wySize,im.wzSize)
98
                mp.ImSetConstant(im, value)
99
                im.resetActiveWindow()
100
        for i in [(0, 0, 0),(0, im.wySize-depth, 0)]:
101
                im.setActiveWindow(i[0], i[1], i[2], im.wxSize, 
102
                                                   depth,im.wzSize)
103
                mp.ImSetConstant(im, value)
104
                im.resetActiveWindow()
105
        if im.getZSize() == 3:
106
                for i in [(0, 0, 0),(0, 0, im3d.wzSize-depth)]:
107
                        im.setActiveWindow(i[0], i[1], i[2], im.wxSize,
108
                                                          im.wySize,depth)
109
                        mp.ImSetConstant(im, value)
110
                        im.resetActiveWindow()
111
        return im
112
113
if __name__=='__main__':
114
    #Read image
115
    im=mp.fileRead(images_dir+ "\\Gray\\tw.png")
116
117
    final_image = set_boundary(im, 50, 0):
118
119
    #write result
120
    mp.fileWrite(final_image,"D:\\final_image.png")
121
</code></pre>|