-
Notifications
You must be signed in to change notification settings - Fork 0
/
fusion.html
922 lines (824 loc) · 37.4 KB
/
fusion.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
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta http-equiv="X-UA-Compatible" content="IE=EDGE" />
<title>Image Fusion</title>
<script src="site_libs/jquery-1.11.3/jquery.min.js"></script>
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link href="site_libs/bootstrap-3.3.5/css/yeti.min.css" rel="stylesheet" />
<script src="site_libs/bootstrap-3.3.5/js/bootstrap.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/html5shiv.min.js"></script>
<script src="site_libs/bootstrap-3.3.5/shim/respond.min.js"></script>
<style>h1 {font-size: 34px;}
h1.title {font-size: 38px;}
h2 {font-size: 30px;}
h3 {font-size: 24px;}
h4 {font-size: 18px;}
h5 {font-size: 16px;}
h6 {font-size: 12px;}
code {color: inherit; background-color: rgba(0, 0, 0, 0.04);}
pre:not([class]) { background-color: white }</style>
<script src="site_libs/jqueryui-1.11.4/jquery-ui.min.js"></script>
<link href="site_libs/tocify-1.9.1/jquery.tocify.css" rel="stylesheet" />
<script src="site_libs/tocify-1.9.1/jquery.tocify.js"></script>
<script src="site_libs/navigation-1.1/tabsets.js"></script>
<link href="site_libs/highlightjs-9.12.0/textmate.css" rel="stylesheet" />
<script src="site_libs/highlightjs-9.12.0/highlight.js"></script>
<link href="site_libs/font-awesome-5.1.0/css/all.css" rel="stylesheet" />
<link href="site_libs/font-awesome-5.1.0/css/v4-shims.css" rel="stylesheet" />
<style type="text/css">
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
div.hanging-indent{margin-left: 1.5em; text-indent: -1.5em;}
ul.task-list{list-style: none;}
</style>
<style type="text/css">code{white-space: pre;}</style>
<script type="text/javascript">
if (window.hljs) {
hljs.configure({languages: []});
hljs.initHighlightingOnLoad();
if (document.readyState && document.readyState === "complete") {
window.setTimeout(function() { hljs.initHighlighting(); }, 0);
}
}
</script>
<link rel="stylesheet" href="styles.css" type="text/css" />
<style type = "text/css">
.main-container {
max-width: 940px;
margin-left: auto;
margin-right: auto;
}
img {
max-width:100%;
}
.tabbed-pane {
padding-top: 12px;
}
.html-widget {
margin-bottom: 20px;
}
button.code-folding-btn:focus {
outline: none;
}
summary {
display: list-item;
}
pre code {
padding: 0;
}
</style>
<style type="text/css">
.dropdown-submenu {
position: relative;
}
.dropdown-submenu>.dropdown-menu {
top: 0;
left: 100%;
margin-top: -6px;
margin-left: -1px;
border-radius: 0 6px 6px 6px;
}
.dropdown-submenu:hover>.dropdown-menu {
display: block;
}
.dropdown-submenu>a:after {
display: block;
content: " ";
float: right;
width: 0;
height: 0;
border-color: transparent;
border-style: solid;
border-width: 5px 0 5px 5px;
border-left-color: #cccccc;
margin-top: 5px;
margin-right: -10px;
}
.dropdown-submenu:hover>a:after {
border-left-color: #adb5bd;
}
.dropdown-submenu.pull-left {
float: none;
}
.dropdown-submenu.pull-left>.dropdown-menu {
left: -100%;
margin-left: 10px;
border-radius: 6px 0 6px 6px;
}
</style>
<script type="text/javascript">
// manage active state of menu based on current page
$(document).ready(function () {
// active menu anchor
href = window.location.pathname
href = href.substr(href.lastIndexOf('/') + 1)
if (href === "")
href = "index.html";
var menuAnchor = $('a[href="' + href + '"]');
// mark it active
menuAnchor.tab('show');
// if it's got a parent navbar menu mark it active as well
menuAnchor.closest('li.dropdown').addClass('active');
// Navbar adjustments
var navHeight = $(".navbar").first().height() + 15;
var style = document.createElement('style');
var pt = "padding-top: " + navHeight + "px; ";
var mt = "margin-top: -" + navHeight + "px; ";
var css = "";
// offset scroll position for anchor links (for fixed navbar)
for (var i = 1; i <= 6; i++) {
css += ".section h" + i + "{ " + pt + mt + "}\n";
}
style.innerHTML = "body {" + pt + "padding-bottom: 40px; }\n" + css;
document.head.appendChild(style);
});
</script>
<!-- tabsets -->
<style type="text/css">
.tabset-dropdown > .nav-tabs {
display: inline-table;
max-height: 500px;
min-height: 44px;
overflow-y: auto;
border: 1px solid #ddd;
border-radius: 4px;
}
.tabset-dropdown > .nav-tabs > li.active:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li.active:before {
content: "";
border: none;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open:before {
content: "";
font-family: 'Glyphicons Halflings';
display: inline-block;
padding: 10px;
border-right: 1px solid #ddd;
}
.tabset-dropdown > .nav-tabs > li.active {
display: block;
}
.tabset-dropdown > .nav-tabs > li > a,
.tabset-dropdown > .nav-tabs > li > a:focus,
.tabset-dropdown > .nav-tabs > li > a:hover {
border: none;
display: inline-block;
border-radius: 4px;
background-color: transparent;
}
.tabset-dropdown > .nav-tabs.nav-tabs-open > li {
display: block;
float: none;
}
.tabset-dropdown > .nav-tabs > li {
display: none;
}
</style>
<!-- code folding -->
<style type="text/css">
#TOC {
margin: 25px 0px 20px 0px;
}
@media (max-width: 768px) {
#TOC {
position: relative;
width: 100%;
}
}
@media print {
.toc-content {
/* see https://github.com/w3c/csswg-drafts/issues/4434 */
float: right;
}
}
.toc-content {
padding-left: 30px;
padding-right: 40px;
}
div.main-container {
max-width: 1200px;
}
div.tocify {
width: 20%;
max-width: 260px;
max-height: 85%;
}
@media (min-width: 768px) and (max-width: 991px) {
div.tocify {
width: 25%;
}
}
@media (max-width: 767px) {
div.tocify {
width: 100%;
max-width: none;
}
}
.tocify ul, .tocify li {
line-height: 20px;
}
.tocify-subheader .tocify-item {
font-size: 0.90em;
}
.tocify .list-group-item {
border-radius: 0px;
}
</style>
</head>
<body>
<div class="container-fluid main-container">
<!-- setup 3col/9col grid for toc_float and main content -->
<div class="row">
<div class="col-xs-12 col-sm-4 col-md-3">
<div id="TOC" class="tocify">
</div>
</div>
<div class="toc-content col-xs-12 col-sm-8 col-md-9">
<div class="navbar navbar-inverse navbar-fixed-top" role="navigation">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
<a class="navbar-brand" href="index.html">Digital Image Processing in R</a>
</div>
<div id="navbar" class="navbar-collapse collapse">
<ul class="nav navbar-nav">
<li>
<a href="index.html">
<span class="fas fa-home"></span>
Home
</a>
</li>
<li class="dropdown">
<a href="#" class="dropdown-toggle" data-toggle="dropdown" role="button" aria-expanded="false">
<span class="fas fa-gear"></span>
More
<span class="caret"></span>
</a>
<ul class="dropdown-menu" role="menu">
<li>
<a href="lesson1.html">Data storage schema</a>
</li>
<li>
<a href="fusion.html">Image Fusion</a>
</li>
<li>
<a href="mapping.html">Crop mapping</a>
</li>
</ul>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li>
<a href="mailto:bensonkemboi@gmail.com">
<span class="fas fa-envelope fa-lg"></span>
</a>
</li>
<li>
<a href="http://github.com/bensonkenduiywo">
<span class="fab fa-github fa-lg"></span>
</a>
</li>
<li>
<a href="https://twitter.com/bensonkenduiywo">
<span class="fab fa-twitter fa-lg"></span>
</a>
</li>
<li>
<a href="https://www.linkedin.com/in/benson-kenduiywo-1a218137">
<span class="fab fa-linkedin fa-lg"></span>
</a>
</li>
</ul>
</div><!--/.nav-collapse -->
</div><!--/.container -->
</div><!--/.navbar -->
<div id="header">
<h1 class="title toc-ignore">Image Fusion</h1>
</div>
<div id="introduction" class="section level2">
<h2>Introduction</h2>
<p>This tutorial seeks to illustrate how image fusion can be conducted. We use an Unmanned Aerial Vehicle (UAV) and Sentinel 2 optical image acquired within the same area and period. <strong>Data fusion</strong> is a formal framework in which are expressed means and tools for the alliance of data originating from different sources. It aims at obtaining information of greater quality; the exact definition of “greater quality” will depend upon the application (<a href="https://doi.org/10.1007/978-1-4020-4385-7_11">Ranchin and Wald, 2010</a>).</p>
<p>The principal motivation for image fusion is to improve the quality of the information contained in the output image in a process known as synergy. A study of existing image fusion techniques and applications shows that image fusion can provide us with an output image with an improved quality. In this case, the benefits of image fusion include:</p>
<ol style="list-style-type: decimal">
<li>Extended range of operation.</li>
<li>Extended spatial and temporal coverage.</li>
<li>Reduced uncertainty.</li>
<li>Increased reliability.</li>
<li>Robust system performance.</li>
<li>Compact representation of information.</li>
</ol>
</div>
<div id="data-preparation" class="section level2">
<h2>Data preparation</h2>
<p>Load libraries, declare variables and data paths.</p>
<pre class="r"><code>rm(list=ls(all=TRUE)) #Clears R memory
unlink(".RData")
if (!require("pacman")) install.packages("pacman"); library(pacman) #package manager( loads required packages/libraries in list as below if not installed they will be installed
p_load(raster, terra)
options(warn=1)
cat("Set variables and start processing\n")</code></pre>
<pre><code>## Set variables and start processing</code></pre>
<pre class="r"><code>Root <- 'D:/JKUAT/RESEARCH_Projects/Eswatini/Data/'
Path_out <- paste0(Root,"Output/")</code></pre>
<p>Load UAV and Sentinel 2 optical images.</p>
<pre class="r"><code>path <- list.files(paste0(Root,'S2/interim/'),pattern = (".tif$"), recursive = TRUE, full.names = TRUE)
path</code></pre>
<pre><code>## [1] "D:/JKUAT/RESEARCH_Projects/Eswatini/Data/S2/interim/RT_T36JUR_20210418T073611_B02.tif"
## [2] "D:/JKUAT/RESEARCH_Projects/Eswatini/Data/S2/interim/RT_T36JUR_20210418T073611_B03.tif"
## [3] "D:/JKUAT/RESEARCH_Projects/Eswatini/Data/S2/interim/RT_T36JUR_20210418T073611_B04.tif"
## [4] "D:/JKUAT/RESEARCH_Projects/Eswatini/Data/S2/interim/RT_T36JUR_20210418T073611_B08.tif"</code></pre>
<pre class="r"><code>s <- rast(path)
s</code></pre>
<pre><code>## class : SpatRaster
## dimensions : 10980, 10980, 4 (nrow, ncol, nlyr)
## resolution : 10, 10 (x, y)
## extent : 3e+05, 409800, 6990220, 7100020 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=utm +zone=36 +south +datum=WGS84 +units=m +no_defs
## sources : RT_T36JUR_20210418T073611_B02.tif
## RT_T36JUR_20210418T073611_B03.tif
## RT_T36JUR_20210418T073611_B04.tif
## ... and 1 more source(s)
## names : RT_T36J~611_B02, RT_T36J~611_B03, RT_T36J~611_B04, RT_T36J~611_B08</code></pre>
<pre class="r"><code>path <- list.files(paste0(Root,'WingtraOne/'),pattern = (".tif$"), full.names = TRUE)
#reorder the bands to macth those in S2
paths <- path
paths[3] <- path[4]
paths[4] <- path[3]
paths</code></pre>
<pre><code>## [1] "D:/JKUAT/RESEARCH_Projects/Eswatini/Data/WingtraOne/mpolonjeni_05042021m3f1_transparent_reflectance_blue.tif"
## [2] "D:/JKUAT/RESEARCH_Projects/Eswatini/Data/WingtraOne/mpolonjeni_05042021m3f1_transparent_reflectance_green.tif"
## [3] "D:/JKUAT/RESEARCH_Projects/Eswatini/Data/WingtraOne/mpolonjeni_05042021m3f1_transparent_reflectance_red.tif"
## [4] "D:/JKUAT/RESEARCH_Projects/Eswatini/Data/WingtraOne/mpolonjeni_05042021m3f1_transparent_reflectance_nir.tif"</code></pre>
<pre class="r"><code>v <- rast(paths)
v</code></pre>
<pre><code>## class : SpatRaster
## dimensions : 12981, 6363, 4 (nrow, ncol, nlyr)
## resolution : 0.12059, 0.12059 (x, y)
## extent : 390445, 391212.3, 7070171, 7071736 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=utm +zone=36 +south +datum=WGS84 +units=m +no_defs
## sources : mpolonjeni_05042021m3f1_transparent_reflectance_blue.tif
## mpolonjeni_05042021m3f1_transparent_reflectance_green.tif
## mpolonjeni_05042021m3f1_transparent_reflectance_red.tif
## ... and 1 more source(s)
## names : mpolonj~ce_blue, mpolonj~e_green, mpolonj~nce_red, mpolonj~nce_nir</code></pre>
<p>Now check image properties and assign meaningful names to its bands.</p>
<pre class="r"><code>#Resolution
res(s)</code></pre>
<pre><code>## [1] 10 10</code></pre>
<pre class="r"><code>#Extents
ext(s)</code></pre>
<pre><code>## SpatExtent : 3e+05, 409800, 6990220, 7100020 (xmin, xmax, ymin, ymax)</code></pre>
<pre class="r"><code>#image dimensions
dim(s)</code></pre>
<pre><code>## [1] 10980 10980 4</code></pre>
<pre class="r"><code>#Number of bands
nlyr(s)</code></pre>
<pre><code>## [1] 4</code></pre>
<pre class="r"><code>names(s) <- c("b", "g","r", "nir")
s</code></pre>
<pre><code>## class : SpatRaster
## dimensions : 10980, 10980, 4 (nrow, ncol, nlyr)
## resolution : 10, 10 (x, y)
## extent : 3e+05, 409800, 6990220, 7100020 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=utm +zone=36 +south +datum=WGS84 +units=m +no_defs
## sources : RT_T36JUR_20210418T073611_B02.tif
## RT_T36JUR_20210418T073611_B03.tif
## RT_T36JUR_20210418T073611_B04.tif
## ... and 1 more source(s)
## names : b, g, r, nir</code></pre>
<pre class="r"><code>res(v)</code></pre>
<pre><code>## [1] 0.12059 0.12059</code></pre>
<pre class="r"><code>ext(v)</code></pre>
<pre><code>## SpatExtent : 390445.0352, 391212.34937, 7070171.12052, 7071736.49931 (xmin, xmax, ymin, ymax)</code></pre>
<pre class="r"><code>dim(v)</code></pre>
<pre><code>## [1] 12981 6363 4</code></pre>
<pre class="r"><code>names(v) <- c("b", "g","r", "nir")
v</code></pre>
<pre><code>## class : SpatRaster
## dimensions : 12981, 6363, 4 (nrow, ncol, nlyr)
## resolution : 0.12059, 0.12059 (x, y)
## extent : 390445, 391212.3, 7070171, 7071736 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=utm +zone=36 +south +datum=WGS84 +units=m +no_defs
## sources : mpolonjeni_05042021m3f1_transparent_reflectance_blue.tif
## mpolonjeni_05042021m3f1_transparent_reflectance_green.tif
## mpolonjeni_05042021m3f1_transparent_reflectance_red.tif
## ... and 1 more source(s)
## names : b, g, r, nir</code></pre>
<p>Crop/clip Sentinel 2 image to UAV image extents.</p>
<pre class="r"><code>s <- crop(s, ext(v), snap="near")</code></pre>
<p>Display the images side by side.</p>
<pre class="r"><code>x11()
par(mfrow = c(1, 2)) #c(bottom, left, top, right)
plotRGB(s, r="nir", g="r", b="g", stretch="lin", axes=T, mar = c(4, 5, 1.4, 0.2), main="S2", cex.axis=0.5)
box()
plotRGB(v, r="nir", g="r", b="g", stretch="lin", axes=T, mar = c(4, 5, 1.4, 0.2), main="UAV", cex.axis=0.5)
box()</code></pre>
<p><img src="fusion_files/figure-html/d5-1.png" width="672" /></p>
<p>First let us conduct a spectral fusion of S2 and UAV images. To do this we have to resample UAV image to S2 extents and another to one to 1 m.</p>
<pre class="r"><code>v_r <- resample(v, s, method='bilinear')
ext(v_r) == ext(s)</code></pre>
<pre><code>## [1] TRUE</code></pre>
<pre class="r"><code>temp <-rast(nrow=1570,ncol=760,ext(s)) #empty object to upscale UAV to 1 m resolution
res(temp) </code></pre>
<pre><code>## [1] 1 1</code></pre>
<pre class="r"><code>v1 <- resample(v, temp, method='bilinear')
res(v1) </code></pre>
<pre><code>## [1] 1 1</code></pre>
</div>
<div id="pixel-fusion" class="section level2">
<h2>Pixel fusion</h2>
<p>This sections considers fusion techniques which rely on simple pixel based operations on input image values. The assumption is that the input images are spatially and temporally aligned, semantically equivalent and radiometrically calibrated. Therefore, let us fuse the two images by multiplication and display it against the original ones.</p>
<pre class="r"><code>#Fuse by multiplication
f1 <- s * v_r
f1</code></pre>
<pre><code>## class : SpatRaster
## dimensions : 157, 76, 4 (nrow, ncol, nlyr)
## resolution : 10, 10 (x, y)
## extent : 390450, 391210, 7070170, 7071740 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=utm +zone=36 +south +datum=WGS84 +units=m +no_defs
## source : memory
## names : b, g, r, nir
## min values : 0.0002068561, 0.0005442272, 0.0003042399, 0.0094109712
## max values : 0.01279990, 0.01698387, 0.02435488, 0.18333833</code></pre>
<pre class="r"><code>#Display fused image alongside original UAV
x11()
par(mfrow = c(1, 3),mar = c(4, 5, 1.4, 0.2))
plotRGB(v, r="nir", g="r", b="g", stretch="lin", axes=T, mar = c(4, 5, 1.4, 0.2), main="UAV", cex.axis=0.7)
box()
plotRGB(s, r="nir", g="r", b="g", stretch="lin", axes=T, mar = c(4, 5, 1.4, 0.2), main="S2", cex.axis=0.7)
box()
plotRGB(f1, r="nir", g="r", b="g", stretch="lin", axes=T, mar = c(4, 5, 1.4, 0.2), main="Fused_multi", cex.axis=0.7)
box()</code></pre>
<p><img src="fusion_files/figure-html/d7-1.png" width="672" /></p>
<p>What about mean fusion (i.e. taking the mean of each pixel’s reflectance in both UAV and S2)?</p>
<pre class="r"><code>#Fuse by multiplication
f2 <- mean(s, v_r)
f2</code></pre>
<pre><code>## class : SpatRaster
## dimensions : 157, 76, 4 (nrow, ncol, nlyr)
## resolution : 10, 10 (x, y)
## extent : 390450, 391210, 7070170, 7071740 (xmin, xmax, ymin, ymax)
## coord. ref. : +proj=utm +zone=36 +south +datum=WGS84 +units=m +no_defs
## source : memory
## names : b, g, r, nir
## min values : 0.01532078, 0.02351120, 0.01861056, 0.09712517
## max values : 0.1225800, 0.1548688, 0.1717744, 0.4470450</code></pre>
<pre class="r"><code>#Display fused image alongside original UAV
x11()
par(mfrow = c(2, 2), mar = c(4, 5, 1.4, 0.2))
plotRGB(v, r="nir", g="r", b="g", stretch="lin", main="UAV", axes=T, mar = c(4, 5, 1.4, 0.2))
box()
plotRGB(s, r="nir", g="r", b="g", stretch="lin", main="S2", axes=T, mar = c(4, 5, 1.4, 0.2))
box()
plotRGB(f1, r="nir", g="r", b="g", stretch="lin", main="Fused_mult", axes=T, mar = c(4, 5, 1.4, 0.2))
box()
plotRGB(f2, r="nir", g="r", b="g", stretch="lin", main="Fused_mean", axes=T, mar = c(4, 5, 1.4, 0.2))
box()</code></pre>
<p><img src="fusion_files/figure-html/d8-1.png" width="672" /></p>
<p>Let us finally follow the fusion approach in <a href="https://ieeexplore.ieee.org/document/8812312">Zou et al (2018)</a>.</p>
<pre class="r"><code>f3 = (s/v_r)*v_r
x11()
par(mfrow = c(2, 3), mar = c(4, 5, 1.4, 0.2))
plotRGB(v, r="nir", g="r", b="g", stretch="lin", main="UAV", axes=T, mar = c(4, 5, 1.4, 0.2))
box()
plotRGB(s, r="nir", g="r", b="g", stretch="lin", main="S2", axes=T, mar = c(4, 5, 1.4, 0.2))
box()
plotRGB(f1, r="nir", g="r", b="g", stretch="lin", main="Fused_mult", axes=T, mar = c(4, 5, 1.4, 0.2))
box()
plotRGB(f2, r="nir", g="r", b="g", stretch="lin", main="Fused_mean", axes=T, mar = c(4, 5, 1.4, 0.2))
box()
plotRGB(f3, r="nir", g="r", b="g", stretch="lin", main="Zhou etal", axes=T, mar = c(4, 5, 1.4, 0.2))
box()</code></pre>
<p><img src="fusion_files/figure-html/d9-1.png" width="672" /></p>
<p>There seem to be some linear relationship between UAV and Sentinel 2 surface reflectance. However it is evident that reflectance values from UAV are higher compared to those in Sentinel 2. So what now?</p>
</div>
<div id="feature-based-fusion" class="section level2">
<h2>Feature based fusion</h2>
<p>In <em>feature fusion</em> we fuse together the features <span class="math inline">\(F_k,k \in{1,2, \dots, K}\)</span>. These features can be vegetation indices like Normalized Difference Index (NDVI) or feature maps that have been made semantically equivalent by transforming them into probabilistic <span class="math inline">\(p(m,n)\)</span>, or likelihood, maps.</p>
<p>Let us start with NDVI (<span class="math inline">\(\text{NDVI}=\frac{\text{NIR}-\text{Red}}{\text{NIR}+\text{Red}}\)</span>. First compute NDVI for both UAV and S2.</p>
<pre class="r"><code>n_v <- (subset(v_r,"nir")-subset(v_r,"r"))/(subset(v_r,"nir")+subset(v_r,"r"))
n_s <- (subset(s,"nir")-subset(s,"r"))/(subset(s,"nir")+subset(s,"r"))</code></pre>
<p>How can we fuse the NDVI index? Let us take an average of the two.</p>
<pre class="r"><code>nf <- mean(n_v, n_s)
x11()
par(mfrow = c(2, 2), mar = c(5, 5, 1.4, 0.2))#c(bottom, left, top, right)
plot(n_v, main="UAV NDVI")
plot(n_s, main="S2 NDVI")
plot(nf, main="Fused NDVI")</code></pre>
<p><img src="fusion_files/figure-html/n2-1.png" width="672" /></p>
<p>Is there any difference between S2, UAV, and the fused NDVI images as shown above?</p>
</div>
<div id="spatial-spectral-fusion" class="section level2">
<h2>Spatial-spectral fusion</h2>
<p>Previously we upsampled the UAV image in order to conduct fusion. While this reduces spectral variability it destroys spatial resolution. Therefore, in this section we will first donwsample the Satellite image to match UAV spatial resolution and then proceed to conduct image fusion. This way, we will improve both spatial and spectral information of Sentinel 2 image and spectral information for UAV.</p>
<div id="modelling-reflectance" class="section level3">
<h3>Modelling reflectance</h3>
<ol style="list-style-type: decimal">
<li>Can we improve the resolution of S2 using UAV?</li>
<li>Can we predict UAV reflectance in places not imaged by the drones?</li>
</ol>
<p>Let’s check the relationship between the two.</p>
<pre class="r"><code>x11()
par(mfrow = c(2, 2), mar = c(4, 5, 1.4, 0.2))
plot(as.vector(subset(s,'b')),as.vector(subset(v_r,'b')), xlab='S2', ylab='W1 UAV', main="Blue band",pch=16,cex=0.75, col='blue')
plot(as.vector(subset(s,'g')),as.vector(subset(v_r,'g')), xlab='S2', ylab='W1 UAV', main="Green band",pch=16,cex=0.75, col='green')
plot(as.vector(subset(s,'r')),as.vector(subset(v_r,'r')), xlab='S2', ylab='W1 UAV', main="Red band",pch=16,cex=0.75, col='red')
plot(as.vector(subset(s,'nir')),as.vector(subset(v_r,'nir')), xlab='S2', ylab='W1 UAV', main="NIR band",pch=16,cex=0.75)</code></pre>
<p><img src="fusion_files/figure-html/m1-1.png" width="672" /></p>
<p>Lets create sample points from S2 (10 m resolution) and upscaled UAV (10 m resolution) and use them to create a model that we can predict S2 reflectance based on UAV 1 m resolution. Essentially what we are doing here is to create a model that can predict S2 reflectance at a resolution of 1 m.</p>
<pre class="r"><code>set.seed(530)
x11()
points <- spatSample(v_r, 900, "random", as.points=T, na.rm=T, values=F)
plotRGB(v_r, r="nir", g="r", b="g", stretch="lin", main="UAV+sampling points", axes=T, mar = c(4, 5, 1.4, 0.2))
plot(points,add=T)</code></pre>
<p><img src="fusion_files/figure-html/m2-1.png" width="672" /></p>
<p>Now extract reflectance values from the two images.</p>
<pre class="r"><code>s2_p <- extract(s, points, drop=F)
head(s2_p)</code></pre>
<pre><code>## ID b g r nir
## 1 1 0.03010000 0.04340000 0.04370000 0.2039
## 2 2 0.02669999 0.04040000 0.03190000 0.2555
## 3 3 0.02600000 0.03800000 0.02760000 0.2727
## 4 4 0.02719999 0.03929999 0.03590000 0.1978
## 5 5 0.04079999 0.06200000 0.06999999 0.2210
## 6 6 0.04020000 0.05670000 0.06260000 0.2209</code></pre>
<pre class="r"><code>vr_p <- extract(v_r, points, drop=F)
head(vr_p)</code></pre>
<pre><code>## ID b g r nir
## 1 1 0.02557389 0.05894550 0.03758506 0.3497387
## 2 2 0.01882389 0.05033908 0.02247731 0.3220550
## 3 3 0.01982662 0.05362828 0.02300457 0.3844531
## 4 4 0.02047731 0.05194858 0.02767093 0.3193594
## 5 5 0.04172961 0.09754695 0.07302261 0.4105712
## 6 6 0.03550068 0.07566584 0.05827252 0.2950630</code></pre>
<p>Create a model.</p>
<pre class="r"><code>data <- data.frame(S2=s2_p[,-1], UAV=vr_p[,-1])
head(data)</code></pre>
<pre><code>## S2.b S2.g S2.r S2.nir UAV.b UAV.g UAV.r
## 1 0.03010000 0.04340000 0.04370000 0.2039 0.02557389 0.05894550 0.03758506
## 2 0.02669999 0.04040000 0.03190000 0.2555 0.01882389 0.05033908 0.02247731
## 3 0.02600000 0.03800000 0.02760000 0.2727 0.01982662 0.05362828 0.02300457
## 4 0.02719999 0.03929999 0.03590000 0.1978 0.02047731 0.05194858 0.02767093
## 5 0.04079999 0.06200000 0.06999999 0.2210 0.04172961 0.09754695 0.07302261
## 6 0.04020000 0.05670000 0.06260000 0.2209 0.03550068 0.07566584 0.05827252
## UAV.nir
## 1 0.3497387
## 2 0.3220550
## 3 0.3844531
## 4 0.3193594
## 5 0.4105712
## 6 0.2950630</code></pre>
<pre class="r"><code># Plot the data
plot(S2.b~UAV.b, data=data, pch=16)
# Create a linear regression model
l.model <- lm(S2.b~UAV.b, data=data)
# Add the fitted line
abline(l.model, col="red")</code></pre>
<p><img src="fusion_files/figure-html/m4-1.png" width="672" /></p>
<p>Looks like the relation within the blue band is not linear. Let’s try a non-linear SVM model.</p>
<pre class="r"><code>#SVM
library(e1071)
svm.model <- svm(S2.b~UAV.b, data=data)
svm.pred <- predict(svm.model, data)
#SVM
library(randomForest)
rfmod <- randomForest(S2.b~UAV.b, data=data)
rf.pred <- predict(rfmod, data)
x11()
plot(S2.b~UAV.b,data, pch=16)
points(data$S2.b, svm.pred, col = "blue", pch=4)
points(data$S2.b, rf.pred, col = "red", pch=4)
legend("topright",c("Data","SVM","RF"), pch= c(16, 4, 4),col=c("black", "blue","red"))</code></pre>
<p><img src="fusion_files/figure-html/m5-1.png" width="672" /></p>
</div>
<div id="predicting-sentinel-2-reflectance" class="section level3">
<h3>Predicting Sentinel 2 reflectance</h3>
<p>SVM and RF models have better characterized the relationship between S2 and UAV blue bands. Let predict high resolution S2 band from existing UAV.</p>
<pre class="r"><code>UAV <- v1[['b']]
names(UAV) <-'UAV.b'
s2h.b <- predict(UAV, rfmod, na.rm=T)
s2h.svm <- predict(UAV, svm.model, na.rm=T)
x11()
par(mfrow = c(1, 2), mar = c(4, 5, 1.4, 0.2))
plot(s2h.svm, main="svm S2 1m Blue band")
plot(s2h.b, main="RF S2 1m Blue band")</code></pre>
<p><img src="fusion_files/figure-html/m6-1.png" width="672" /></p>
<pre class="r"><code># #parallel processing in raster
# UAv <- raster(UAV)
# library(snow)
# startTime <- Sys.time()
# cat("Start time", format(startTime),"\n")
# beginCluster()
# r4 <- predict(UAV, rfmod,na.rm=T)
# endCluster()
# timeDiff <- Sys.time() - startTime
# cat("\n Processing time", format(timeDiff), "\n")</code></pre>
</div>
<div id="validation" class="section level3">
<h3>Validation</h3>
<p>But how do we know if this predictions are accurate? We can plot predicted vs actual and also compute RMSE and Mean Absolute Percentage Error (MAPE) using training data. MAPE is given as:</p>
<pre class="r"><code>MAPE <- function (y_pred, y_true){
MAPE <- mean(abs((y_true - y_pred)/y_true))
return(MAPE*100)
}</code></pre>
<p>and RMSE,</p>
<pre class="r"><code>rmse <- function(error){
sqrt(mean(error^2))
}</code></pre>
<p>So lets compute MAPE and RMSE for both methods.</p>
<pre class="r"><code>svm.rmse <- rmse(svm.pred-data$S2.b)
svm.rmse</code></pre>
<pre><code>## [1] 0.003996155</code></pre>
<pre class="r"><code>rf.rmse <- rmse(rf.pred-data$S2.b)
rf.rmse</code></pre>
<pre><code>## [1] 0.002186289</code></pre>
<pre class="r"><code>svm.mape <- MAPE(svm.pred, data$S2.b)
svm.mape</code></pre>
<pre><code>## [1] 7.315199</code></pre>
<pre class="r"><code>rf.mape <- MAPE(rf.pred, data$S2.b)
rf.mape</code></pre>
<pre><code>## [1] 4.358044</code></pre>
<p>From the validations Random Forest gives better prediction than Support Vector machines because it has low MAPE and RMSE error. In that case we can adopt the high resolution S2 image predicted/simulated by RF and fuse it with UAV image at 1 m resolution. Note that we only predict the blue band, we can model and predict the other bands and then fuse them.</p>
<pre class="r"><code>#Green band
rfmod <- randomForest(S2.g~UAV.g, data=data)
UAV=subset(v1,'g')
names(UAV) <-'UAV.g'
s2h.g <- predict(UAV, rfmod, na.rm=T)
#red band
rfmod <- randomForest(S2.r~UAV.r, data=data)
UAV=subset(v1,'r')
names(UAV) <-'UAV.r'
s2h.r <- predict(UAV, rfmod, na.rm=T)
#NIR band
rfmod <- randomForest(S2.nir~UAV.nir, data=data)
UAV=subset(v1,'nir')
names(UAV) <-'UAV.nir'
s2h.nir <- predict(UAV, rfmod, na.rm=T)
#Stack them
s2h <- stack(x=c(s2h.b,s2h.g,s2h.r,s2h.nir))
names(s2h) <- c("b", "g","r", "nir")
s2h</code></pre>
<pre><code>## class : RasterStack
## dimensions : 1570, 760, 1193200, 4 (nrow, ncol, ncell, nlayers)
## resolution : 1, 1 (x, y)
## extent : 390450, 391210, 7070170, 7071740 (xmin, xmax, ymin, ymax)
## crs : NA
## names : b, g, r, nir
## min values : 0.02305458, 0.03414621, 0.02679058, 0.12475280
## max values : 0.06060897, 0.08070807, 0.10184987, 0.30137640</code></pre>
<p>However, the predicted reflectance ranges in all bands except NIR are very low. Why could this be so? Now lets create a high resolution fused image using Zhou’s approach.</p>
</div>
<div id="fusion" class="section level3">
<h3>Fusion</h3>
<pre class="r"><code>s2h.fused <- (rast(s2h)/v1)*v1
s2h.fused</code></pre>
<pre><code>## class : SpatRaster
## dimensions : 1570, 760, 4 (nrow, ncol, nlyr)
## resolution : 1, 1 (x, y)
## extent : 390450, 391210, 7070170, 7071740 (xmin, xmax, ymin, ymax)
## coord. ref. :
## source : memory
## names : b, g, r, nir
## min values : 0.02305458, 0.03414621, 0.02679058, 0.12475280
## max values : 0.06060897, 0.08070807, 0.10184987, 0.30137640</code></pre>
<pre class="r"><code>x11()
par(mfrow = c(1, 2), mar = c(4, 5, 1.4, 0.2))
plotRGB(s2h.fused, r="nir", g="r", b="g", stretch="lin", main="UAV+S2 high res", axes=T, mar = c(4, 5, 1.4, 0.2))
box()
plotRGB(v, r="nir", g="r", b="g", stretch="lin", main="UAV", axes=T, mar = c(4, 5, 1.4, 0.2))
box()</code></pre>
<p><img src="fusion_files/figure-html/m9-1.png" width="672" /></p>
<p>The other option would be use S2 to predict UAV in areas not covered by the drone and then fuse it with S2. However this approach would require consideration of similar land-cover. For instance, we can not train the model in an area with different land-cover say cropland and predict in another area with say Forest. Food for thought.</p>
<p>But let’s step back a little, could sampling from land-cover categories have improved prediction of high resolution S2 done previously with RF? To test this, let us perform a K-means classification and sample from its land-cover map.</p>
<pre class="r"><code>image <- v_r
image[is.na(image)] <- 0
nclass <- 4
system.time(
E <- kmeans(as.data.frame(image, na.rm=F), nclass, iter.max = 100, nstart = 9)
)</code></pre>
<pre><code>## user system elapsed
## 0.07 0.00 0.08</code></pre>
<pre class="r"><code>k.map <- image[[1]]
values(k.map) <- E$cluster
k.map[is.na(v_r[[1]])] <- NA
plot(k.map)</code></pre>
<p><img src="fusion_files/figure-html/m10-1.png" width="672" /></p>
<p>Sample points using stratified random sampling based on the K-means land-cover map.</p>
<pre class="r"><code>set.seed(530)
points <- spatSample(k.map, 900, "stratified", as.points=T, na.rm=T, values=F)
x11()
plot(k.map, main="K-means +sampling points", axes=T, mar = c(4, 5, 1.4, 0.2), col=topo.colors(max(values(k.map),na.rm=T)))
plot(points,add=T)</code></pre>
<p><img src="fusion_files/figure-html/m11-1.png" width="672" /></p>
<p>Use the stratified randomly sample points to train and predict simulated high resolution S2.</p>
<pre class="r"><code>s2_p <- extract(s, points, drop=F)
head(s2_p)</code></pre>
<pre><code>## ID b g r nir
## 1 1 0.04770000 0.05939999 0.07750000 0.1492
## 2 2 0.03960000 0.05549999 0.07099999 0.2073
## 3 3 0.03320000 0.04880000 0.05010000 0.2258
## 4 4 0.03379999 0.04790000 0.06040000 0.1966
## 5 5 0.04420000 0.06200000 0.07959999 0.2172
## 6 6 0.04070000 0.05980000 0.06649999 0.2101</code></pre>
<pre class="r"><code>vr_p <- extract(v_r, points, drop=F)
head(vr_p)</code></pre>
<pre><code>## ID b g r nir
## 1 1 0.05413008 0.09205989 0.11132049 0.2734993
## 2 2 0.04446261 0.10010008 0.08397383 0.4695161
## 3 3 0.03073439 0.07668109 0.05490024 0.4001549
## 4 4 0.02435945 0.06213900 0.04357978 0.3155672
## 5 5 0.03281799 0.06734130 0.06080524 0.2811245
## 6 6 0.03464006 0.07732372 0.05422943 0.3122228</code></pre>
<pre class="r"><code>data <- data.frame(S2=s2_p[,-1], UAV=vr_p[,-1])
rfmod <- randomForest(S2.b~UAV.b, data=data)
rf.pred <- predict(rfmod, data)</code></pre>
<p>Lets evaluate if stratified random sampling improved RF accuracy.</p>
<pre class="r"><code>rf.rmse <- rmse(rf.pred-data$S2.b)
rf.rmse</code></pre>
<pre><code>## [1] 0.00193131</code></pre>
<pre class="r"><code>rf.mape <- MAPE(rf.pred, data$S2.b)
rf.mape</code></pre>
<pre><code>## [1] 4.36695</code></pre>
<p>We can see that RMSE has by a small margin compared to the case of random sampling. MAPE more or less remained constant. Thus there is a chance that sampling over different land-cover improves prediction.</p>
<p>As an assignment now predict S2 high resolution using a RF model trained on stratified random samples from K-means classifier.</p>
</div>
</div>
<div id="references" class="section level2">
<h2>References</h2>
<p>Thierry Ranchin and Lucien Wald. <em>Data Fusion in Remote Sensing of Urban and Suburban Areas</em>, pages 193–218. Springer Netherlands, Dordrecht, 2010. ISBN 978-1-4020-4385- 7. doi: 10:1007/978-1-4020-4385-7 11. URL <a href="https://doi.org/10.1007/978-1-4020-4385-7_11" class="uri">https://doi.org/10.1007/978-1-4020-4385-7_11</a>.</p>
<p>Y. Zou, G. Li and S. Wang, “The Fusion of Satellite and Unmanned Aerial Vehicle (UAV) Imagery for Improving Classification Performance,” <em>IEEE International Conference on Information and Automation (ICIA)</em>, 2018, pp. 836-841, doi: 10.1109/ICInfA.2018.8812312.</p>
</div>
<p> Created 14th May 2021 Copyright © Benson Kenduiywo, Inc. All rights reserved.</p>
</div>
</div>
</div>
<script>
// add bootstrap table styles to pandoc tables
function bootstrapStylePandocTables() {
$('tr.odd').parent('tbody').parent('table').addClass('table table-condensed');
}
$(document).ready(function () {
bootstrapStylePandocTables();
});
</script>
<!-- tabsets -->
<script>
$(document).ready(function () {
window.buildTabsets("TOC");
});
$(document).ready(function () {
$('.tabset-dropdown > .nav-tabs > li').click(function () {
$(this).parent().toggleClass('nav-tabs-open');
});
});
</script>
<!-- code folding -->
<script>
$(document).ready(function () {
// move toc-ignore selectors from section div to header
$('div.section.toc-ignore')
.removeClass('toc-ignore')
.children('h1,h2,h3,h4,h5').addClass('toc-ignore');
// establish options
var options = {
selectors: "h1,h2,h3",
theme: "bootstrap3",
context: '.toc-content',
hashGenerator: function (text) {
return text.replace(/[.\\/?&!#<>]/g, '').replace(/\s/g, '_');
},
ignoreSelector: ".toc-ignore",
scrollTo: 0
};
options.showAndHide = true;
options.smoothScroll = true;
// tocify
var toc = $("#TOC").tocify(options).data("toc-tocify");
});
</script>
<!-- dynamically load mathjax for compatibility with self-contained -->
<script>
(function () {
var script = document.createElement("script");
script.type = "text/javascript";
script.src = "https://mathjax.rstudio.com/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML";
document.getElementsByTagName("head")[0].appendChild(script);
})();
</script>
</body>
</html>