-
Notifications
You must be signed in to change notification settings - Fork 0
/
PythonAPI.html
667 lines (508 loc) · 48.4 KB
/
PythonAPI.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
<!DOCTYPE html>
<!--[if IE 8]><html class="no-js lt-ie9" lang="en" > <![endif]-->
<!--[if gt IE 8]><!--> <html class="no-js" lang="en" > <!--<![endif]-->
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Python API — Orfeo ToolBox 6.7.0 documentation</title>
<link rel="stylesheet" href="_static/css/theme.css" type="text/css" />
<link rel="stylesheet" href="_static/pygments.css" type="text/css" />
<link rel="stylesheet" href="_static/css/otb_theme.css" type="text/css" />
<link rel="index" title="Index" href="genindex.html" />
<link rel="search" title="Search" href="search.html" />
<link rel="next" title="QGIS interface" href="QGISInterface.html" />
<link rel="prev" title="Graphical interface" href="GraphicalInterface.html" />
<script src="_static/js/modernizr.min.js"></script>
</head>
<body class="wy-body-for-nav">
<div class="wy-grid-for-nav">
<nav data-toggle="wy-nav-shift" class="wy-nav-side">
<div class="wy-side-scroll">
<div class="wy-side-nav-search">
<a href="index.html">
<img src="_static/logo-with-text.png" class="logo" alt="Logo"/>
</a>
<div class="version">
6.7.0
</div>
<div role="search">
<form id="rtd-search-form" class="wy-form" action="search.html" method="get">
<input type="text" name="q" placeholder="Search docs" />
<input type="hidden" name="check_keywords" value="yes" />
<input type="hidden" name="area" value="default" />
</form>
</div>
</div>
<div class="wy-menu wy-menu-vertical" data-spy="affix" role="navigation" aria-label="main navigation">
<p class="caption"><span class="caption-text">Get Started</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="Installation.html">Installation</a></li>
<li class="toctree-l1"><a class="reference internal" href="Monteverdi.html">Monteverdi</a></li>
</ul>
<p class="caption"><span class="caption-text">Applications</span></p>
<ul class="current">
<li class="toctree-l1"><a class="reference internal" href="CliInterface.html">Command-line interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="GraphicalInterface.html">Graphical interface</a></li>
<li class="toctree-l1 current"><a class="current reference internal" href="#">Python API</a><ul>
<li class="toctree-l2"><a class="reference internal" href="#basics">Basics</a></li>
<li class="toctree-l2"><a class="reference internal" href="#numpy-array-processing">Numpy array processing</a></li>
<li class="toctree-l2"><a class="reference internal" href="#in-memory-connection">In-memory connection</a></li>
<li class="toctree-l2"><a class="reference internal" href="#interactions-with-otb-pipeline">Interactions with OTB pipeline</a></li>
<li class="toctree-l2"><a class="reference internal" href="#corner-cases">Corner cases</a><ul>
<li class="toctree-l3"><a class="reference internal" href="#calling-updateparameters">Calling UpdateParameters()</a></li>
<li class="toctree-l3"><a class="reference internal" href="#no-metadata-in-numpy-arrays">No metadata in NumPy arrays</a></li>
</ul>
</li>
</ul>
</li>
<li class="toctree-l1"><a class="reference internal" href="QGISInterface.html">QGIS interface</a></li>
<li class="toctree-l1"><a class="reference internal" href="Applications.html">All Applications</a></li>
</ul>
<p class="caption"><span class="caption-text">Recipes</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="recipes/optpreproc.html">From raw image to calibrated product</a></li>
<li class="toctree-l1"><a class="reference internal" href="recipes/sarprocessing.html">SAR processing</a></li>
<li class="toctree-l1"><a class="reference internal" href="recipes/residual_registration.html">Residual registration</a></li>
<li class="toctree-l1"><a class="reference internal" href="recipes/improc.html">Image processing</a></li>
<li class="toctree-l1"><a class="reference internal" href="recipes/contrast_enhancement.html">Enhance local contrast</a></li>
<li class="toctree-l1"><a class="reference internal" href="recipes/pbclassif.html">Classification</a></li>
<li class="toctree-l1"><a class="reference internal" href="recipes/featextract.html">Feature extraction</a></li>
<li class="toctree-l1"><a class="reference internal" href="recipes/stereo.html">Stereoscopic reconstruction</a></li>
<li class="toctree-l1"><a class="reference internal" href="recipes/hyperspectral.html">Hyperspectral image processing</a></li>
</ul>
<p class="caption"><span class="caption-text">C++ API</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="C++/SystemOverview.html">System Overview</a></li>
<li class="toctree-l1"><a class="reference internal" href="C++/Tutorial.html">Building simple OTB code</a></li>
<li class="toctree-l1"><a class="reference internal" href="C++/UserGuide.html">User Guide</a></li>
<li class="toctree-l1"><a class="reference internal" href="C++/AboutBandMathX.html">About BandMathX</a></li>
<li class="toctree-l1"><a class="reference internal" href="C++/Examples.html">C++ Examples</a></li>
<li class="toctree-l1"><a class="reference internal" href="C++/DeveloperGuide.html">Developer Guide</a></li>
</ul>
<p class="caption"><span class="caption-text">Advanced use</span></p>
<ul>
<li class="toctree-l1"><a class="reference internal" href="EnvironmentVariables.html">Environment variables</a></li>
<li class="toctree-l1"><a class="reference internal" href="ExtendedFilenames.html">Extended filenames</a></li>
<li class="toctree-l1"><a class="reference internal" href="CompilingOTBFromSource.html">Compiling OTB from source</a></li>
<li class="toctree-l1"><a class="reference internal" href="FAQ.html">Frequently Asked Questions</a></li>
<li class="toctree-l1"><a class="reference internal" href="Contributors.html">Contributors</a></li>
</ul>
</div>
</div>
</nav>
<section data-toggle="wy-nav-shift" class="wy-nav-content-wrap">
<nav class="wy-nav-top" aria-label="top navigation">
<i data-toggle="wy-nav-top" class="fa fa-bars"></i>
<a href="index.html">Orfeo ToolBox</a>
</nav>
<div class="wy-nav-content">
<div class="rst-content">
<div role="navigation" aria-label="breadcrumbs navigation">
<ul class="wy-breadcrumbs">
<li><a href="index.html">Docs</a> »</li>
<li>Python API</li>
<li class="wy-breadcrumbs-aside">
<a href="https://gitlab.orfeo-toolbox.org/orfeotoolbox/OTB/blob/develop/Documentation/Cookbook/rst/PythonAPI.rst" class="fa fa-gitlab"> Edit on GitLab</a>
</li>
</ul>
<hr/>
</div>
<div role="main" class="document" itemscope="itemscope" itemtype="http://schema.org/Article">
<div itemprop="articleBody">
<div class="section" id="python-api">
<h1>Python API<a class="headerlink" href="#python-api" title="Permalink to this headline">¶</a></h1>
<p>The applications can also be accessed from Python, through a module
named <code class="docutils literal notranslate"><span class="pre">otbApplication</span></code>. However, there are technical requirements to use it.
If you use OTB through standalone packages, you should use the supplied
environment script <code class="docutils literal notranslate"><span class="pre">otbenv</span></code> to properly setup variables such as
<code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code> and <code class="docutils literal notranslate"><span class="pre">OTB_APPLICATION_PATH</span></code> (on Unix systems, don’t forget to
source the script). In other cases, you should set these variables depending on
your configuration.</p>
<p>On Unix systems, it is typically available in the <code class="docutils literal notranslate"><span class="pre">/usr/lib/otb/python</span></code>
directory. Depending on how you installed OTB, you may need to configure the
environment variable <code class="docutils literal notranslate"><span class="pre">PYTHONPATH</span></code> to include this directory so that the module
becomes available from Python.</p>
<p>On Windows, you can install the <code class="docutils literal notranslate"><span class="pre">otb-python</span></code> package, and the module
will be automatically available from an OSGeo4W shell.</p>
<p>As for the command line and GUI launchers, the path to the application
modules needs to be properly set with the <code class="docutils literal notranslate"><span class="pre">OTB_APPLICATION_PATH</span></code>
environment variable. The standard location on Unix systems is
<code class="docutils literal notranslate"><span class="pre">/usr/lib/otb/applications</span></code>. On Windows, the applications are
available in the <code class="docutils literal notranslate"><span class="pre">otb-bin</span></code> OSGeo4W package, and the environment is
configured automatically so <code class="docutils literal notranslate"><span class="pre">OTB_APPLICATION_PATH</span></code> doesn’t need to be modified
<code class="docutils literal notranslate"><span class="pre">OTB_APPLICATION_PATH</span></code>.</p>
<p>Once your environment is set, you can use OTB applications from Python, just
like this small example:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Example on the use of the Smoothing application</span>
<span class="c1"># The python module providing access to OTB applications is otbApplication</span>
<span class="kn">import</span> <span class="nn">otbApplication</span> <span class="kn">as</span> <span class="nn">otb</span>
<span class="c1"># Let's create the application with codename "Smoothing"</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">otb</span><span class="o">.</span><span class="n">Registry</span><span class="o">.</span><span class="n">CreateApplication</span><span class="p">(</span><span class="s2">"Smoothing"</span><span class="p">)</span>
<span class="c1"># We set its parameters</span>
<span class="n">app</span><span class="o">.</span><span class="n">SetParameterString</span><span class="p">(</span><span class="s2">"in"</span><span class="p">,</span> <span class="s2">"my_input_image.tif"</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">SetParameterString</span><span class="p">(</span><span class="s2">"type"</span><span class="p">,</span> <span class="s2">"mean"</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">SetParameterString</span><span class="p">(</span><span class="s2">"out"</span><span class="p">,</span> <span class="s2">"my_output_image.tif"</span><span class="p">)</span>
<span class="c1"># This will execute the application and save the output file</span>
<span class="n">app</span><span class="o">.</span><span class="n">ExecuteAndWriteOutput</span><span class="p">()</span>
</pre></div>
</div>
<div class="section" id="basics">
<h2>Basics<a class="headerlink" href="#basics" title="Permalink to this headline">¶</a></h2>
<p>In the <code class="docutils literal notranslate"><span class="pre">otbApplication</span></code> module, two main classes can be manipulated :</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">Registry</span></code>, which provides access to the list of available
applications, and can create applications.</li>
<li><code class="docutils literal notranslate"><span class="pre">Application</span></code>, the base class for all applications. This allows one to
interact with an application instance created by the <code class="docutils literal notranslate"><span class="pre">Registry</span></code>.</li>
</ul>
<p>Here is one example of how to use Python to run the <code class="docutils literal notranslate"><span class="pre">Smoothing</span></code>
application, changing the algorithm at each iteration.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="c1"># Example on the use of the Smoothing application</span>
<span class="c1">#</span>
<span class="c1"># We will use sys.argv to retrieve arguments from the command line.</span>
<span class="c1"># Here, the script will accept an image file as first argument,</span>
<span class="c1"># and the basename of the output files, without extension.</span>
<span class="kn">from</span> <span class="nn">sys</span> <span class="kn">import</span> <span class="n">argv</span>
<span class="c1"># The python module providing access to OTB applications is otbApplication</span>
<span class="kn">import</span> <span class="nn">otbApplication</span>
<span class="c1"># otbApplication.Registry can tell you what application are available</span>
<span class="k">print</span><span class="p">(</span><span class="s1">'Available applications: '</span><span class="p">)</span>
<span class="k">print</span> <span class="p">(</span><span class="nb">str</span><span class="p">(</span> <span class="n">otbApplication</span><span class="o">.</span><span class="n">Registry</span><span class="o">.</span><span class="n">GetAvailableApplications</span><span class="p">()))</span>
<span class="c1"># Let's create the application "Smoothing"</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">otbApplication</span><span class="o">.</span><span class="n">Registry</span><span class="o">.</span><span class="n">CreateApplication</span><span class="p">(</span><span class="s2">"Smoothing"</span><span class="p">)</span>
<span class="c1"># We print the keys of all its parameters</span>
<span class="k">print</span> <span class="p">(</span><span class="n">app</span><span class="o">.</span><span class="n">GetParametersKeys</span><span class="p">())</span>
<span class="c1"># First, we set the input image filename</span>
<span class="n">app</span><span class="o">.</span><span class="n">SetParameterString</span><span class="p">(</span><span class="s2">"in"</span><span class="p">,</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="c1"># The smoothing algorithm can be set with the "type" parameter key</span>
<span class="c1"># and can take 3 values: 'mean', 'gaussian', 'anidif'</span>
<span class="k">for</span> <span class="nb">type</span> <span class="ow">in</span> <span class="p">[</span><span class="s1">'mean'</span><span class="p">,</span> <span class="s1">'gaussian'</span><span class="p">,</span> <span class="s1">'anidif'</span><span class="p">]:</span>
<span class="k">print</span><span class="p">(</span><span class="s1">'Running with '</span> <span class="o">+</span> <span class="nb">type</span> <span class="o">+</span> <span class="s1">' smoothing type'</span><span class="p">)</span>
<span class="c1"># Now we configure the smoothing algorithm</span>
<span class="n">app</span><span class="o">.</span><span class="n">SetParameterString</span><span class="p">(</span><span class="s2">"type"</span><span class="p">,</span> <span class="nb">type</span><span class="p">)</span>
<span class="c1"># Set the output filename, using the algorithm type to differentiate the outputs</span>
<span class="n">app</span><span class="o">.</span><span class="n">SetParameterString</span><span class="p">(</span><span class="s2">"out"</span><span class="p">,</span> <span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span> <span class="o">+</span> <span class="nb">type</span> <span class="o">+</span> <span class="s2">".tif"</span><span class="p">)</span>
<span class="c1"># This will execute the application and save the output to argv[2]</span>
<span class="n">app</span><span class="o">.</span><span class="n">ExecuteAndWriteOutput</span><span class="p">()</span>
</pre></div>
</div>
<p>If you want to handle the parameters from a Python dictionary, you can use the
functions <em>SetParameters()</em> and <em>GetParameters()</em>.</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">params</span> <span class="o">=</span> <span class="p">{</span><span class="s2">"in"</span><span class="p">:</span><span class="s2">"myInput.tif"</span><span class="p">,</span> <span class="s2">"type.mean.radius"</span><span class="p">:</span><span class="mi">4</span><span class="p">}</span>
<span class="n">app</span><span class="o">.</span><span class="n">SetParameters</span><span class="p">(</span><span class="n">params</span><span class="p">)</span>
<span class="n">params2</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">GetParameters</span><span class="p">()</span>
</pre></div>
</div>
</div>
<div class="section" id="numpy-array-processing">
<h2>Numpy array processing<a class="headerlink" href="#numpy-array-processing" title="Permalink to this headline">¶</a></h2>
<p>Input and output images to any OTB application in the form of NumPy array is now possible in OTB Python wrapping.
The Python wrapping only exposes OTB Application engine module (called <em>ApplicationEngine</em>) which allows one to access existing C++ applications.
Due to blissful nature of ApplicationEngine’s loading mechanism no specific wrapping is required for each application.</p>
<p>NumPy extension to Python wrapping allows data exchange to application as an array rather than a disk file.
Of course, it is possible to load an image from file and then convert it to NumPy
array or just provide a file as explained in the previous section via
Application.SetParameterString(…).</p>
<p>The bridge between NumPy and OTB makes it easy to plug OTB into any image processing chain via Python code that uses
GIS/Image processing tools such as GDAL, GRASS GIS, OSSIM that can deal with NumPy.</p>
<p>Below code reads an input image using Python Pillow library (fork of PIL) and convert it to
NumPy array. The NumPy array is used as an input to the application via
<em>SetImageFromNumpyArray(…)</em> method. The application used in this example is
ExtractROI. After extracting a small area the output image is taken as NumPy
array with <em>GetImageFromNumpyArray(…)</em> method thus avoid writing output to a
temporary file.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">sys</span>
<span class="kn">import</span> <span class="nn">os</span>
<span class="kn">import</span> <span class="nn">numpy</span> <span class="k">as</span> <span class="nn">np</span>
<span class="kn">import</span> <span class="nn">otbApplication</span>
<span class="kn">from</span> <span class="nn">PIL</span> <span class="k">import</span> <span class="n">Image</span> <span class="k">as</span> <span class="n">PILImage</span>
<span class="n">pilimage</span> <span class="o">=</span> <span class="n">PILImage</span><span class="o">.</span><span class="n">open</span><span class="p">(</span><span class="s1">'poupees.jpg'</span><span class="p">)</span>
<span class="n">npimage</span> <span class="o">=</span> <span class="n">np</span><span class="o">.</span><span class="n">asarray</span><span class="p">(</span><span class="n">pilimage</span><span class="p">)</span>
<span class="n">inshow</span><span class="p">(</span><span class="n">pilimage</span><span class="p">)</span>
<span class="n">ExtractROI</span> <span class="o">=</span> <span class="n">otbApplication</span><span class="o">.</span><span class="n">Registry</span><span class="o">.</span><span class="n">CreateApplication</span><span class="p">(</span><span class="s1">'ExtractROI'</span><span class="p">)</span>
<span class="n">ExtractROI</span><span class="o">.</span><span class="n">SetImageFromNumpyArray</span><span class="p">(</span><span class="s1">'in'</span><span class="p">,</span> <span class="n">npimage</span><span class="p">)</span>
<span class="n">ExtractROI</span><span class="o">.</span><span class="n">SetParameterInt</span><span class="p">(</span><span class="s1">'startx'</span><span class="p">,</span> <span class="mi">140</span><span class="p">)</span>
<span class="n">ExtractROI</span><span class="o">.</span><span class="n">SetParameterInt</span><span class="p">(</span><span class="s1">'starty'</span><span class="p">,</span> <span class="mi">120</span><span class="p">)</span>
<span class="n">ExtractROI</span><span class="o">.</span><span class="n">SetParameterInt</span><span class="p">(</span><span class="s1">'sizex'</span><span class="p">,</span> <span class="mi">150</span><span class="p">)</span>
<span class="n">ExtractROI</span><span class="o">.</span><span class="n">SetParameterInt</span><span class="p">(</span><span class="s1">'sizey'</span><span class="p">,</span> <span class="mi">150</span><span class="p">)</span>
<span class="n">ExtractROI</span><span class="o">.</span><span class="n">Execute</span><span class="p">()</span>
<span class="n">ExtractOutput</span> <span class="o">=</span> <span class="n">ExtractROI</span><span class="o">.</span><span class="n">GetImageAsNumpyArray</span><span class="p">(</span><span class="s1">'out'</span><span class="p">)</span>
<span class="n">output_pil_image</span> <span class="o">=</span> <span class="n">PILImage</span><span class="o">.</span><span class="n">fromarray</span><span class="p">(</span><span class="n">np</span><span class="o">.</span><span class="n">uint8</span><span class="p">(</span><span class="n">ExtractOutput</span><span class="p">))</span>
<span class="n">imshow</span><span class="p">(</span><span class="n">output_pil_image</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="in-memory-connection">
<h2>In-memory connection<a class="headerlink" href="#in-memory-connection" title="Permalink to this headline">¶</a></h2>
<p>Applications are often used as part of larger processing
workflows. Chaining applications currently requires to write/read back
images between applications, resulting in heavy I/O operations and a
significant amount of time dedicated to writing temporary files.</p>
<p>Since OTB 5.8, it is possible to connect an output image parameter
from one application to the input image parameter of the next
parameter. This results in the wiring of the internal ITK/OTB
pipelines together, permitting image streaming between the
applications. Consequently, this removes the need of writing temporary
images and improves performance. Only the last application of the processing chain is responsible
for writing the final result images.</p>
<p>In-memory connection between applications is available both at the C++
API level and using the Python bindings.</p>
<p>Here is a Python code sample which connects several applications together:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">otbApplication</span> <span class="kn">as</span> <span class="nn">otb</span>
<span class="n">app1</span> <span class="o">=</span> <span class="n">otb</span><span class="o">.</span><span class="n">Registry</span><span class="o">.</span><span class="n">CreateApplication</span><span class="p">(</span><span class="s2">"Smoothing"</span><span class="p">)</span>
<span class="n">app2</span> <span class="o">=</span> <span class="n">otb</span><span class="o">.</span><span class="n">Registry</span><span class="o">.</span><span class="n">CreateApplication</span><span class="p">(</span><span class="s2">"Smoothing"</span><span class="p">)</span>
<span class="n">app3</span> <span class="o">=</span> <span class="n">otb</span><span class="o">.</span><span class="n">Registry</span><span class="o">.</span><span class="n">CreateApplication</span><span class="p">(</span><span class="s2">"Smoothing"</span><span class="p">)</span>
<span class="n">app4</span> <span class="o">=</span> <span class="n">otb</span><span class="o">.</span><span class="n">Registry</span><span class="o">.</span><span class="n">CreateApplication</span><span class="p">(</span><span class="s2">"ConcatenateImages"</span><span class="p">)</span>
<span class="n">app1</span><span class="o">.</span><span class="n">IN</span> <span class="o">=</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="n">app1</span><span class="o">.</span><span class="n">Execute</span><span class="p">()</span>
<span class="c1"># Connection between app1.out and app2.in</span>
<span class="n">app2</span><span class="o">.</span><span class="n">SetParameterInputImage</span><span class="p">(</span><span class="s2">"in"</span><span class="p">,</span><span class="n">app1</span><span class="o">.</span><span class="n">GetParameterOutputImage</span><span class="p">(</span><span class="s2">"out"</span><span class="p">))</span>
<span class="c1"># Execute call is mandatory to wire the pipeline and expose the</span>
<span class="c1"># application output. It does not write image</span>
<span class="n">app2</span><span class="o">.</span><span class="n">Execute</span><span class="p">()</span>
<span class="n">app3</span><span class="o">.</span><span class="n">IN</span> <span class="o">=</span> <span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span>
<span class="c1"># Execute call is mandatory to wire the pipeline and expose the</span>
<span class="c1"># application output. It does not write image</span>
<span class="n">app3</span><span class="o">.</span><span class="n">Execute</span><span class="p">()</span>
<span class="c1"># Connection between app2.out, app3.out and app4.il using images list</span>
<span class="n">app4</span><span class="o">.</span><span class="n">AddImageToParameterInputImageList</span><span class="p">(</span><span class="s2">"il"</span><span class="p">,</span><span class="n">app2</span><span class="o">.</span><span class="n">GetParameterOutputImage</span><span class="p">(</span><span class="s2">"out"</span><span class="p">));</span>
<span class="n">app4</span><span class="o">.</span><span class="n">AddImageToParameterInputImageList</span><span class="p">(</span><span class="s2">"il"</span><span class="p">,</span><span class="n">app3</span><span class="o">.</span><span class="n">GetParameterOutputImage</span><span class="p">(</span><span class="s2">"out"</span><span class="p">));</span>
<span class="n">app4</span><span class="o">.</span><span class="n">OUT</span> <span class="o">=</span> <span class="n">argv</span><span class="p">[</span><span class="mi">2</span><span class="p">]</span>
<span class="c1"># Call to ExecuteAndWriteOutput() both wires the pipeline and</span>
<span class="c1"># actually writes the output, only necessary for last application of</span>
<span class="c1"># the chain.</span>
<span class="n">app4</span><span class="o">.</span><span class="n">ExecuteAndWriteOutput</span><span class="p">()</span>
</pre></div>
</div>
<p><strong>Note:</strong> Streaming will only work properly if the application internal
implementation does not break it, for instance by using an internal
writer to write intermediate data. In this case, execution should
still be correct, but some intermediate data will be read or written.</p>
</div>
<div class="section" id="interactions-with-otb-pipeline">
<h2>Interactions with OTB pipeline<a class="headerlink" href="#interactions-with-otb-pipeline" title="Permalink to this headline">¶</a></h2>
<p>[Since OTB 6.6]</p>
<p>The application framework has been extended in order to provide ways to
interact with the pipelines inside each application. It applies only to
applications that use input or output images. Let’s check which
functions are available in the <code class="docutils literal notranslate"><span class="pre">Application</span></code> class. There are lots of getter
functions:</p>
<table border="1" class="docutils">
<colgroup>
<col width="46%" />
<col width="54%" />
</colgroup>
<thead valign="bottom">
<tr class="row-odd"><th class="head">Function name</th>
<th class="head">return value</th>
</tr>
</thead>
<tbody valign="top">
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">GetImageOrigin(...)</span></code></td>
<td>origin of the image (physical position
of the first pixel center)</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">GetImageSpacing(...)</span></code></td>
<td>signed spacing of the image</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">GetImageSize(...)</span></code></td>
<td>size of the LargestPossibleRegion</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">GetImageNbBands(...)</span></code></td>
<td>number of components per pixel</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">GetImageProjection(...)</span></code></td>
<td>Projection WKT string</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">GetImageKeywordlist(...)</span></code></td>
<td>Ossim keywordlist (sensor model)</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">GetImageMetaData(...)</span></code></td>
<td>the entire MetaDataDictionary</td>
</tr>
<tr class="row-odd"><td><code class="docutils literal notranslate"><span class="pre">GetImageRequestedRegion(...)</span></code></td>
<td>requested region</td>
</tr>
<tr class="row-even"><td><code class="docutils literal notranslate"><span class="pre">GetImageBasePixelType(...)</span></code></td>
<td>pixel type of the underlying
Image/VectorImage.</td>
</tr>
</tbody>
</table>
<p>All these getters functions use the following arguments:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">key</span></code>: a string containing the key of the image parameter</li>
<li><code class="docutils literal notranslate"><span class="pre">idx</span></code>: an optional index (default is 0) that can be used to access ImageList
parameters transparently</li>
</ul>
<p>There is also a function to send orders to the pipeline:</p>
<blockquote>
<div><code class="docutils literal notranslate"><span class="pre">PropagateRequestedRegion(key,</span> <span class="pre">region,</span> <span class="pre">idx=0)</span></code>: sets a given RequestedRegion
on the image and propagate it, returns the memory print estimation. This function
can be used to measure the requested portion of input images necessary to produce
an extract of the full output.</div></blockquote>
<p>Note: a requested region (like other regions in the C++ API of otb::Image) consists
of an image index and a size, which defines a rectangular extract of
the full image.</p>
<p>This set of functions has been used to enhance the bridge between OTB images
and Numpy arrays. There are now import and export functions available in
Python that preserve the metadata of the image during conversions to Numpy
arrays:</p>
<ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">ExportImage(self,</span> <span class="pre">key)</span></code>: exports an output image parameter into a Python
dictionary.</li>
<li><code class="docutils literal notranslate"><span class="pre">ImportImage(self,</span> <span class="pre">key,</span> <span class="pre">dict,</span> <span class="pre">index=0)</span></code>: imports the image from a Python
dictionary into an image parameter (as a monoband image).</li>
<li><code class="docutils literal notranslate"><span class="pre">ImportVectorImage(self,</span> <span class="pre">key,</span> <span class="pre">dict,</span> <span class="pre">index=0)</span></code>: imports the image from a
Python dictionary into an image parameter (as a multiband image).</li>
</ul>
<p>The Python dictionary used has the following entries:</p>
<blockquote>
<div><ul class="simple">
<li><code class="docutils literal notranslate"><span class="pre">'array'</span></code>: the Numpy array containing the pixel buffer</li>
<li><code class="docutils literal notranslate"><span class="pre">'origin'</span></code>: origin of the image</li>
<li><code class="docutils literal notranslate"><span class="pre">'spacing'</span></code>: signed spacing of the image</li>
<li><code class="docutils literal notranslate"><span class="pre">'size'</span></code>: full size of the image</li>
<li><code class="docutils literal notranslate"><span class="pre">'region'</span></code>: region of the image present in the buffer</li>
<li><code class="docutils literal notranslate"><span class="pre">'metadata'</span></code>: metadata dictionary (contains projection, sensor model,…)</li>
</ul>
</div></blockquote>
<p>Now some basic Q&A about this interface:</p>
<blockquote>
<div><p>Q: What portion of the image is exported to Numpy array?
A: By default, the whole image is exported. If you had a non-empty requested
region (the result of calling PropagateRequestedRegion()), then this region
is exported.</p>
<p>Q: What is the difference between ImportImage and ImportVectorImage?
A: The first one is here for Applications that expect a monoband otb::Image.
In most cases, you will use the second one: ImportVectorImage.</p>
<p>Q: What kind of objects are there in this dictionary export?
A: The array is a numpy.ndarray. The other fields are wrapped
objects from the OTB library but you can interact with them in a
Python way: they support <code class="docutils literal notranslate"><span class="pre">len()</span></code> and <code class="docutils literal notranslate"><span class="pre">str()</span></code> operator, as well as
bracket operator <code class="docutils literal notranslate"><span class="pre">[]</span></code>. Some of them also have a <code class="docutils literal notranslate"><span class="pre">keys()</span></code> function just like
dictionaries.</p>
</div></blockquote>
<p>This interface allows you to export OTB images (or extracts) to Numpy array,
process them by other means, and re-import them with preserved metadata. Please
note that this is different from an in-memory connection.</p>
<p>Here is a small example of what can be done:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">otbApplication</span> <span class="kn">as</span> <span class="nn">otb</span>
<span class="c1"># Create a smoothing application</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">otb</span><span class="o">.</span><span class="n">Registry</span><span class="o">.</span><span class="n">CreateApplication</span><span class="p">(</span><span class="s2">"Smoothing"</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">SetParameterString</span><span class="p">(</span><span class="s2">"in"</span><span class="p">,</span><span class="n">argv</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span>
<span class="c1"># only call Execute() to setup the pipeline, not ExecuteAndWriteOutput() which would</span>
<span class="c1"># run it and write the output image</span>
<span class="n">app</span><span class="o">.</span><span class="n">Execute</span><span class="p">()</span>
<span class="c1"># Setup a special requested region</span>
<span class="n">myRegion</span> <span class="o">=</span> <span class="n">otb</span><span class="o">.</span><span class="n">itkRegion</span><span class="p">()</span>
<span class="n">myRegion</span><span class="p">[</span><span class="s1">'size'</span><span class="p">][</span><span class="mi">0</span><span class="p">]</span> <span class="o">=</span> <span class="mi">20</span>
<span class="n">myRegion</span><span class="p">[</span><span class="s1">'size'</span><span class="p">][</span><span class="mi">1</span><span class="p">]</span> <span class="o">=</span> <span class="mi">25</span>
<span class="n">myRegion</span><span class="p">[</span><span class="s1">'index'</span><span class="p">]</span><span class="o">.</span><span class="n">Fill</span><span class="p">(</span><span class="mi">10</span><span class="p">)</span>
<span class="n">ram</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">PropagateRequestedRegion</span><span class="p">(</span><span class="s2">"out"</span><span class="p">,</span><span class="n">myRegion</span><span class="p">)</span>
<span class="c1"># Check the requested region on the input image</span>
<span class="k">print</span><span class="p">(</span><span class="n">app</span><span class="o">.</span><span class="n">GetImageRequestedRegion</span><span class="p">(</span><span class="s2">"in"</span><span class="p">))</span>
<span class="c1"># Create a ReadImageInfo application</span>
<span class="n">app2</span> <span class="o">=</span> <span class="n">otb</span><span class="o">.</span><span class="n">Registry</span><span class="o">.</span><span class="n">CreateApplication</span><span class="p">(</span><span class="s2">"ReadImageInfo"</span><span class="p">)</span>
<span class="c1"># export "out" from Smoothing and import it as "in" in ReadImageInfo</span>
<span class="n">ex</span> <span class="o">=</span> <span class="n">app</span><span class="o">.</span><span class="n">ExportImage</span><span class="p">(</span><span class="s2">"out"</span><span class="p">)</span>
<span class="n">app2</span><span class="o">.</span><span class="n">ImportVectorImage</span><span class="p">(</span><span class="s2">"in"</span><span class="p">,</span> <span class="n">ex</span><span class="p">)</span>
<span class="n">app2</span><span class="o">.</span><span class="n">Execute</span><span class="p">()</span>
<span class="c1"># Check the result of ReadImageInfo</span>
<span class="n">someKeys</span> <span class="o">=</span> <span class="p">[</span><span class="s1">'sizex'</span><span class="p">,</span> <span class="s1">'sizey'</span><span class="p">,</span> <span class="s1">'spacingx'</span><span class="p">,</span> <span class="s1">'spacingy'</span><span class="p">,</span> <span class="s1">'sensor'</span><span class="p">,</span> <span class="s1">'projectionref'</span><span class="p">]</span>
<span class="k">for</span> <span class="n">key</span> <span class="ow">in</span> <span class="n">someKeys</span><span class="p">:</span>
<span class="k">print</span><span class="p">(</span><span class="n">key</span> <span class="o">+</span> <span class="s1">' : '</span> <span class="o">+</span> <span class="nb">str</span><span class="p">(</span><span class="n">app2</span><span class="o">.</span><span class="n">GetParameterValue</span><span class="p">(</span><span class="n">key</span><span class="p">)))</span>
<span class="c1"># Only a portion of "out" was exported but ReadImageInfo is still able to detect the</span>
<span class="c1"># correct full size of the image</span>
</pre></div>
</div>
</div>
<div class="section" id="corner-cases">
<h2>Corner cases<a class="headerlink" href="#corner-cases" title="Permalink to this headline">¶</a></h2>
<p>There are a few corner cases to be aware of when using Python wrappers. They are
often limitations, that one day may be solved in future versions. If it
happens, this documentation will report the OTB version that fixes the issue.</p>
<div class="section" id="calling-updateparameters">
<h3>Calling UpdateParameters()<a class="headerlink" href="#calling-updateparameters" title="Permalink to this headline">¶</a></h3>
<p>These wrappers are made as a mirror of the C++ API, so there is a function
<code class="docutils literal notranslate"><span class="pre">UpdateParameters()</span></code>. Its role is to update parameters that depend on others.
It is called at least once at the beginning of <code class="docutils literal notranslate"><span class="pre">Execute()</span></code>.</p>
<p>In command line and GUI launchers, this functions gets called each time a
parameter of the application is modified. In Python, this mechanism is not
automated: there are cases where you may have to call it yourself.</p>
<p>Let’s take an example with the application <code class="docutils literal notranslate"><span class="pre">PolygonClassStatictics</span></code>. In this
application, the choices available in the parameter <code class="docutils literal notranslate"><span class="pre">field</span></code> depend on the list
of fields actually present in the vector file <code class="docutils literal notranslate"><span class="pre">vec</span></code>. If you try to set the
parameters <code class="docutils literal notranslate"><span class="pre">vec</span></code> and <code class="docutils literal notranslate"><span class="pre">field</span></code>, you will get an error:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">otbApplication</span> <span class="kn">as</span> <span class="nn">otb</span>
<span class="n">app</span> <span class="o">=</span> <span class="n">otb</span><span class="o">.</span><span class="n">Registry</span><span class="o">.</span><span class="n">CreateApplication</span><span class="p">(</span><span class="s2">"PolygonClassStatistics"</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">SetParameterString</span><span class="p">(</span><span class="s2">"vec"</span><span class="p">,</span><span class="s2">"../../src/OTB-Data/Input/Classification/variousVectors.sqlite"</span><span class="p">)</span>
<span class="n">app</span><span class="o">.</span><span class="n">SetParameterString</span><span class="p">(</span><span class="s2">"field"</span><span class="p">,</span> <span class="s2">"label"</span><span class="p">)</span>
</pre></div>
</div>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">Traceback</span> <span class="p">(</span><span class="n">most</span> <span class="n">recent</span> <span class="n">call</span> <span class="n">last</span><span class="p">):</span>
<span class="n">File</span> <span class="s2">"<stdin>"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">1</span><span class="p">,</span> <span class="ow">in</span> <span class="o"><</span><span class="n">module</span><span class="o">></span>
<span class="n">File</span> <span class="s2">"/home/gpasero/Projet_OTB/build/OTB/lib/otb/python/otbApplication.py"</span><span class="p">,</span> <span class="n">line</span> <span class="mi">897</span><span class="p">,</span> <span class="ow">in</span> <span class="n">SetParameterString</span>
<span class="k">def</span> <span class="nf">SetParameterString</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">):</span> <span class="k">return</span> <span class="n">_otbApplication</span><span class="o">.</span><span class="n">Application_SetParameterString</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="o">*</span><span class="n">args</span><span class="p">)</span>
<span class="ne">RuntimeError</span><span class="p">:</span> <span class="ne">Exception</span> <span class="n">thrown</span> <span class="ow">in</span> <span class="n">otbApplication</span> <span class="n">Application_SetParameterString</span><span class="p">:</span> <span class="o">/</span><span class="n">home</span><span class="o">/</span><span class="n">gpasero</span><span class="o">/</span><span class="n">Projet_OTB</span><span class="o">/</span><span class="n">src</span><span class="o">/</span><span class="n">OTB</span><span class="o">/</span><span class="n">Modules</span><span class="o">/</span><span class="n">Wrappers</span><span class="o">/</span><span class="n">ApplicationEngine</span><span class="o">/</span><span class="n">src</span><span class="o">/</span><span class="n">otbWrapperListViewParameter</span><span class="o">.</span><span class="n">cxx</span><span class="p">:</span><span class="mi">141</span><span class="p">:</span>
<span class="n">itk</span><span class="p">::</span><span class="n">ERROR</span><span class="p">:</span> <span class="n">ListViewParameter</span><span class="p">(</span><span class="mh">0x149da10</span><span class="p">):</span> <span class="n">Cannot</span> <span class="n">find</span> <span class="n">label</span>
</pre></div>
</div>
<p>The error says that the choice <code class="docutils literal notranslate"><span class="pre">label</span></code> is not recognized, because <code class="docutils literal notranslate"><span class="pre">UpdateParameters()</span></code>
was not called after setting the vector file. The solution is to call it before
setting the <code class="docutils literal notranslate"><span class="pre">field</span></code> parameter:</p>
<div class="highlight-python notranslate"><div class="highlight"><pre><span></span><span class="n">app</span><span class="o">.</span><span class="n">UpdateParameters</span><span class="p">()</span>
<span class="n">app</span><span class="o">.</span><span class="n">SetParameterString</span><span class="p">(</span><span class="s2">"field"</span><span class="p">,</span> <span class="s2">"label"</span><span class="p">)</span>
</pre></div>
</div>
</div>
<div class="section" id="no-metadata-in-numpy-arrays">
<h3>No metadata in NumPy arrays<a class="headerlink" href="#no-metadata-in-numpy-arrays" title="Permalink to this headline">¶</a></h3>
<p>With the NumPy module, it is possible to convert images between OTB and NumPy
arrays. For instance, when converting from OTB to NumPy array:</p>
<ul class="simple">
<li>An <code class="docutils literal notranslate"><span class="pre">Update()</span></code> of the underlying <code class="docutils literal notranslate"><span class="pre">otb::VectorImage</span></code> is requested. Be aware
that the full image is generated.</li>
<li>The pixel buffer is copied into a <code class="docutils literal notranslate"><span class="pre">numpy.array</span></code></li>
</ul>
<p>As you can see, there is no export of the metadata, such as origin, spacing,
geographic projection. It means that if you want to re-import a NumPy array back into OTB,
the image won’t have any of these metadata. This can pose problems for applications
that relate to geometry, projections, and also calibration.</p>
<p>Future developments will probably offer a more adapted structure to import and
export images between OTB and the Python world.</p>
</div>
</div>
</div>
</div>
</div>
<footer>
<div class="rst-footer-buttons" role="navigation" aria-label="footer navigation">
<a href="QGISInterface.html" class="btn btn-neutral float-right" title="QGIS interface" accesskey="n" rel="next">Next <span class="fa fa-arrow-circle-right"></span></a>
<a href="GraphicalInterface.html" class="btn btn-neutral" title="Graphical interface" accesskey="p" rel="prev"><span class="fa fa-arrow-circle-left"></span> Previous</a>
</div>
<hr/>
<div role="contentinfo">
<p>
© Copyright 2019 CNES. The OTB CookBook is licensed under a Creative Commons Attribution-ShareAlike 4.0 International license (CC-BY-SA).
</p>
</div>
Built with <a href="http://sphinx-doc.org/">Sphinx</a> using a <a href="https://github.com/rtfd/sphinx_rtd_theme">theme</a> provided by <a href="https://readthedocs.org">Read the Docs</a>.
</footer>
</div>
</div>
</section>
</div>
<script type="text/javascript" id="documentation_options" data-url_root="./" src="_static/documentation_options.js"></script>
<script type="text/javascript" src="_static/jquery.js"></script>
<script type="text/javascript" src="_static/underscore.js"></script>
<script type="text/javascript" src="_static/doctools.js"></script>
<script type="text/javascript" src="_static/js/versions.js"></script>
<script type="text/javascript" src="_static/js/theme.js"></script>
<script type="text/javascript">
jQuery(function () {
SphinxRtdTheme.Navigation.enable(true);
});
</script>
</body>
</html>