Project

General

Profile

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

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