Project

General

Profile

Morph-M Python - ImportExport to other python librairies » History » Version 3

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

1 1 Serge Koudoro
h1. Morph-M Python - ImportExport to other python librairies
2
3
h2{color:#8B0000;background:#ddd}. Introduction
4
5
Sometimes, you need to convert an image created by morphee to the image format of other libraries. It's possible to make this conversion and then to transform them back to MorpheeImages.
6
7
h2{color:#8B0000;background:#ddd}. PIL(Python Image Library)
8
9
bq. *%{color:#006400}+Morph-M Gray images To PIL...+%*
10
11 2 Serge Koudoro
<pre><code class="ruby">
12 1 Serge Koudoro
im = mp.fileRead("testGray.png")
13
imPIL = pil.new("L",(im.getWxSize(),im.getWySize()))
14
data =mp.ImageToString(im,1,0,mp.colorType.GRAY)
15
imPIL.fromstring(data)
16 2 Serge Koudoro
</code></pre>
17 1 Serge Koudoro
18
bq. *%{color:#006400}+... To Morph-M+%*
19
20 2 Serge Koudoro
<pre><code class="ruby">
21 1 Serge Koudoro
imMorp=mp.ImCreate(imPIL.size[0],imPIL.size[1],"UINT8")
22
data = imPIL.tostring()
23
mp.ImageFromString(imMorp, data, 1, 0, colorType.GRAY)
24 2 Serge Koudoro
</code></pre>
25 1 Serge Koudoro
26
bq. *%{color:#006400}+Morph-M Color images to PIL...+%*
27
28 2 Serge Koudoro
<pre><code class="ruby">
29 1 Serge Koudoro
import morphee as mp
30
import Image as pil
31
im = mp.fileRead("testRGB.png")
32
imPIL = pil.new("RGB",(im.getWxSize(),im.getWySize()))
33
data =mp.ImageToString(im,3,0,mp.colorType.ARGB)
34
imPIL.fromstring(data)
35 2 Serge Koudoro
</code></pre>
36 1 Serge Koudoro
37
h2{color:#8B0000;background:#ddd}. wxPython
38
39
bq. *%{color:#006400}+Morph-M Images To WX+%*
40
41 2 Serge Koudoro
<pre><code class="ruby">
42 1 Serge Koudoro
import morphee as mp
43
import wx
44
im = mp.fileRead("testRGB.png")
45
imWX = wx.EmptyImage(im.getWxSize(), im.getWySize())
46
data = mp.ImageToString(im,3,0,mp.colorType.ABGR)
47
imWX.SetData(data)
48 2 Serge Koudoro
</code></pre>
49 1 Serge Koudoro
50
h2{color:#8B0000;background:#ddd}. Numpy(the replacement for Numeric/numarray)
51
52
bq. *%{color:#006400}+Morph-M Images To Numpy....+%*
53
54 2 Serge Koudoro
<pre><code class="ruby">
55 1 Serge Koudoro
import morphee as mp
56
import numpy
57
im = mp.fileRead("testGray.png")
58
arr = mp.ImageToNumarray(im)
59 2 Serge Koudoro
</code></pre>
60 1 Serge Koudoro
61
bq. *%{color:#006400}+... To Morph-M+%*
62
63 2 Serge Koudoro
<pre><code class="ruby">
64 1 Serge Koudoro
imMorp=mp.ImCreate(arr.shape[0],arr.shape[1],"UINT8")
65
im.fromList( map(int,arr.flat) )
66 2 Serge Koudoro
</code></pre>
67 1 Serge Koudoro
68
h2{color:#8B0000;background:#ddd}. VTK images
69
70
bq. *%{color:#006400}+Morph-M Images To VTK+%*
71
72 2 Serge Koudoro
<pre><code class="ruby">
73 1 Serge Koudoro
import morphee as mp
74
import vtk
75
im = mp.fileRead("testGray.png")
76
str = mp.ImageToString(im,1,0,mp.colorType.GRAY)
77
x=im.getXSize()
78
y=im.getYSize()
79
z=im.getZSize()
80
imimport = vtk.vtkImageImport()
81
imimport.CopyImportVoidPointer(str,x*y*z)
82
imimport.SetDataScalarTypeToUnsignedChar()
83
imimport.SetDataExtent(0,x-1,0,y-1,0,z-1)
84
imimport.SetWholeExtent(0,x-1,0,y-1,0,z-1)
85
imdata = imimport.GetOutput()
86
imdata.Update()
87 2 Serge Koudoro
</code></pre>
88 1 Serge Koudoro
89
h2{color:#8B0000;background:#ddd}. Qt4 Image
90
91
bq. *%{color:#006400}+Morph-M To QT4: Gray level images+%*
92
93 2 Serge Koudoro
<pre><code class="ruby">
94 1 Serge Koudoro
import morphee as mp
95
from PyQt4 import QtGui
96
# create/read the image
97
im = mp.fileRead("testGray.png")
98
x=im.getXSize()
99
y=im.getYSize()
100
sdata=mp.ImageToString(im,1,(4 - x % 4) % 4,mp.colorType.GRAY)
101
fmt = QtGui.QImage.Format_Indexed8
102
img = QtGui.QImage(sdata, x, y, fmt)
103
# make sure the image is displayed in shades of gray
104
# (and not with a funky LUT)
105
img.setColorTable([QtGui.qRgb(i,i,i) for i in range(0,256)])
106 2 Serge Koudoro
</code></pre>
107 1 Serge Koudoro
108
bq. *%{color:#006400}+Morph-M To QT4: Color images+%*
109
110 2 Serge Koudoro
<pre><code class="ruby">
111 1 Serge Koudoro
# create/read the color image
112
im = mp.fileRead("testRGB.png")
113
# transfering the data to a qtpixmap
114
sdata = mp.ImageToString(imLUT,4,0,mp.colorType.ARGB)
115
x=imLUT.getXSize()
116
y=imLUT.getYSize()
117
# convert 32bit buffer to a Qt QImage
118
fmt = QtGui.QImage.Format_ARGB32
119
img = QtGui.QImage(sdata, x, y, fmt)
120 2 Serge Koudoro
</code></pre>
121 3 Serge Koudoro
122
h2{color:#8B0000;background:#ddd}. OpenCV
123
124
h3{color:#8B0000}. +Introduction+
125
126
OpenCV (Open Source Computer vision) is a library of programming functions mainly aimed at real time computer Vision. This library contains Python bindings that we can use easily. Some links:
127
128
* OpenCV Installation:
129
** "Official Guide":http://www.cs.indiana.edu/cgi-pub/oleykin/website/OpenCVHelp/
130
** "Ubuntu installation":http://dircweb.king.ac.uk/reason/opencv_cvs.php
131
* Basic uses:
132
** "A good and brief introduction":http://www.cs.iit.edu/~agam/cs512/lect-notes/opencv-intro/opencv-intro.html
133
** "Brief useful functions of Matrix operation":http://note.sonots.com/OpenCV/MatrixOperations.html
134
* OpenCV and python:
135
** "Presentation of API differences in Python":http://opencv.willowgarage.com/wiki/PythonInterface
136
** [[Python wrapper installation]]
137
138
h3{color:#8B0000}. +Reference functions+
139
140
imOut = *ImageFromIplImage*( imIn ) : ??convert a IMorpheeImage from an Ipl or CvMat image?? 
141
* *_imIn_* : _Ipl or CvMat images_
142
* *_imOut* : Morphee Image Interface_
143
144
*ImageToIplImage*(imIn,imOut) : ??convert a an Ipl or CvMat image from IMorpheeImage?? 
145
* *_imIn* : Morphee Image Interface_
146
* *_imOut* : Ipl or CvMat images_
147
148
+Note:+ 
149
* Functions name is the same under python or C++
150
* Under python, these functions only accept CvMat Images. 
151
152
h2{color:#8B0000}. +OpenCV and Morph-M under Python+
153
154
h4. +Basic use+
155
156
| !http://morphm.ensmp.fr/attachments/48/retina.png! | <pre><code class="ruby">
157
158
# import opencv and Morph-M modules
159
from opencv.cv import *
160
from opencv.highgui import *
161
from morphee import *
162
163
cvStartWindowThread()
164
cvNamedWindow("UnderBuild Retina")
165
166
#Structuring Element
167
nl = SquareSE
168
nl_cv = cvCreateStructuringElementEx(12, 12,6,6, CV_SHAPE_RECT)
169
170
#Load images
171
imIn = cvLoadImage(images_dir+"/Gray/retina2.png",0)
172
imTmp=cvCloneImage(imIn)
173
imOpen=cvCloneImage(imIn)
174
175
# -------------------------------
176
# open with OpenCV...
177
# -------------------------------
178
cvMorphologyEx(imIn, imOpen, imTmp, nl_cv, CV_MOP_OPEN)
179
180
# ------------------------------
181
# ...then build with Morph-M
182
# ------------------------------
183
184
#image conversion
185
imOut=ImageFromIplImage(imTmp)
186
187
ImUnderBuild(ImageFromIplImage(imOpen),ImageFromIplImage(imIn),nl,imOut)
188
fileWrite(imOut,"imBuildOpen.png")
189
190
# ------------------------------
191
# Show Result with OpenCV
192
# ------------------------------
193
194
ImageToIplImage(imOut,imTmp)
195
cvShowImage("Retina: Original",imIn)
196
cvShowImage("Retina: Processed",imTmp)
197
cvWaitKey(0)
198
199
cvDestroyWindow("Retina: Original")
200
cvDestroyWindow("Retina: Processed")
201
202
</code></pre> |
203
204
h4. +Basic use 2: Working with Video Sequence+
205
206
| !{width:300px}http://morphm.ensmp.fr/attachments/49/roadVideo_original.png!:http://morphm.ensmp.fr/attachments/51/myRoad_Original.avi |/2. <pre><code class="ruby">
207
208
# import opencv and Morph-M modules
209
from opencv.cv import *
210
from opencv.highgui import *
211
from morphee import *
212
213
#------------------------------
214
#Initializing a video writer:
215
#------------------------------ 
216
isColor = 1
217
fps     = 25  # or 30
218
frameW  = 256 # 744 for firewire cameras
219
frameH  = 256 # 480 for firewire cameras
220
writer = cvCreateVideoWriter("myRoad_Original.avi",-1, 
221
                    fps,cvSize(frameW,frameH),isColor)
222
writer_result = cvCreateVideoWriter("myRoad_Processed.avi",-1,
223
                           fps,cvSize(frameW,frameH),isColor)
224
225
#-----------------------------
226
#Writing the video file:
227
#-----------------------------
228
nFrames = 70;
229
for i in range(nFrames):
230
	img = cvLoadImage(images_dir+"/Sequences/route/route_%d.png"%i) 
231
 	# add the frame to the file myRoad_Original.avi	
232
	cvWriteFrame(writer,img)      
233
		
234
	# ------------------------------
235
        # Morpho operation with Morph-M
236
        # ------------------------------
237
238
        im_gray = cvCreateImage(cvGetSize(img),IPL_DEPTH_8U,1)
239
	cvCvtColor(img,im_gray, CV_BGR2GRAY)
240
		
241
	#allocate images
242
	imIn=ImageFromIplImage(im_gray)		
243
	imGrad = getSame(imIn)
244
	imWat = getSame(imIn)
245
	imIplWat = cvCloneImage(im_gray)
246
247
	# Segmentation with the waterfall algo.
248
	nl = NeighborList.neighborsHex2D
249
	ImMorphoGradient(imIn,nl,imGrad)
250
	ImWaterfallsClassical(imGrad,nl,1,imWat)
251
	arithInvertImage(imWat,imWat)
252
	
253
	ImageToIplImage(imWat,imIplWat)
254
		
255
	im_processed = cvCloneImage(img)
256
	cvCvtColor(imIplWat,im_processed, CV_GRAY2BGR)
257
258
	# add the frame to the file myRoad_Processed.avi			
259
        cvWriteFrame(writer_result,im_processed)
260
261
cvReleaseVideoWriter(writer)
262
cvReleaseVideoWriter(writer_result)
263
264
</code></pre> |
265
| !{width:300px}http://morphm.ensmp.fr/attachments/50/roadVideo_processed.png!:http://morphm.ensmp.fr/attachments/52/myRoad_Processed.avi|