{"id":2979,"date":"2019-07-25T16:40:36","date_gmt":"2019-07-25T14:40:36","guid":{"rendered":"https:\/\/blogs.fu-berlin.de\/reseda\/?page_id=2979"},"modified":"2019-08-22T15:56:36","modified_gmt":"2019-08-22T13:56:36","slug":"quality-band","status":"publish","type":"page","link":"https:\/\/blogs.fu-berlin.de\/reseda\/quality-band\/","title":{"rendered":"Cloud Masking for Sentinel-2 images"},"content":{"rendered":"<p><span class=\"tlid-translation translation\" lang=\"en\"><span class=\"\" title=\"\">If there are clouds or cloud shadows on your sentinel-2 scene, they can be mask out using the <em>quality scene classification band<\/em> of your scene.<br \/>\n<\/span><\/span><br \/>\n<a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2019\/07\/Quality_scene_1_KF.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1710 size-full\" src=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2019\/07\/Quality_scene_1_KF.png\" alt=\"\" width=\"1560\" height=\"892\" \/><\/a><\/p>\n<div style=\"margin: -30px 0 20px 0;text-align: center\"><span style=\"color: #686868;font-size: small\">Sentinel-2 scene (true color) with clouds and cloud shadows<\/span><\/div>\n<div><\/div>\n<div>\n<p>Image quality band and the classes we want to delete for our mask (Values): 3 (cloud shadows), 7 (unclassified), 8 (cloud medium probability), 9 (cloud high probability), 10 (thin cirrus) and 11 (snow or ice). For other scenes, you have to adjust the classes if necessary.<\/p>\n<p><a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2019\/07\/Quality_scene_KF.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1710 size-full\" src=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2019\/07\/Quality_scene_KF.png\" alt=\"\" width=\"1560\" height=\"892\" \/><\/a><\/p>\n<div style=\"margin: -30px 0 20px 0;text-align: center\"><span style=\"color: #686868;font-size: small\">Sentinel-2 scene with quality scene classification<\/span><\/div>\n<div><\/div>\n<div>\n<p><a href=\"https:\/\/box.fu-berlin.de\/s\/ztHcGQtXMKJbcXN\/download?path=%2FSentinel_Data&amp;files=subset_0_of_S2A_MSIL2A_20190626T102031_N0212_R065_T32UQD_20190626T125319_resampled.tif\" target=\"_blank\" rel=\"noopener\">Here<\/a> you can download a Sentinel-2 image for executing this pre-processing step. For the scene which we used in the pre-processing before is no cloud correction necessary.<\/p>\n<h1><strong>Processing steps in RStudio:<\/strong><\/h1>\n<p>Open R-Studio and install, open the required packages and set your working directory:<\/p>\n<pre class=\"theme:amityreseda lang:r decode:true \">install.packages(\"raster\")\r\ninstall.packages(\"rgdal\")\r\n\r\nlibrary(raster)\r\nlibrary(rgdal)\r\n\r\nsetwd(\"D:\\\\elearning\\\\exchange\\\\R\")<\/pre>\n<p>&nbsp;<\/p>\n<p>Open and plot the image:<\/p>\n<pre class=\"lang:r decode:true \">sen2 &lt;- stack(\"subset_0_of_S2A_MSIL2A_20190626T102031_N0212_R065_T32UQD_20190626T125319_resampled.tif\")\r\nplot(sen2)\r\nplotRGB(sen2, 4,3,2, stretch=\"lin\")<\/pre>\n<p>&nbsp;<\/p>\n<p>Seperate spectral bands and classification (band 1 to 12 is the mulispectral Senitnel- 2 scene, band 13 is the quality classification band):<\/p>\n<pre class=\"lang:r decode:true\">sen2_bands &lt;- sen2[[-13]]\r\nsen2_mask &lt;- sen2[[13]]\r\nplot(sen2_mask)<\/pre>\n<p>&nbsp;<\/p>\n<p>Which pixels do we want to mask?<\/p>\n<pre class=\"lang:r decode:true \">plot(sen2_mask)\r\nsen2_mask_combi &lt;- sen2_mask\r\nsen2_mask_combi[sen2_mask == 3 |sen2_mask == 7 |sen2_mask == 8 | sen2_mask == 9 |sen2_mask == 10 |sen2_mask == 11 ] &lt;- NA\r\n\r\nplot(sen2_mask_combi)\r\nwriteRaster(sen2_mask_combi, \"sen2_mask.tif\")<\/pre>\n<p><a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2019\/07\/Maske.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1710 \" src=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2019\/07\/Maske.png\" alt=\"\" width=\"628\" height=\"509\" \/><\/a><\/p>\n<div style=\"margin: -30px 0 20px 0;text-align: center\"><span style=\"color: #686868;font-size: small\">Mask without the classes: 3,7,8,9,10,11<br \/>\n<\/span><\/div>\n<p>&nbsp;<\/p>\n<p>Apply mask:<\/p>\n<pre class=\"lang:r decode:true\">sen2_bands_masked &lt;- raster::mask(sen2_bands,sen2_mask_combi)\r\nplotRGB(sen2_bands_masked, 4,3,2,stretch=\"lin\")\r\nwriteRaster(sen2_bands_masked, \"sen2_masked.tif\")<\/pre>\n<p><a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2019\/07\/Maske_applied.png\"><img loading=\"lazy\" decoding=\"async\" class=\"aligncenter wp-image-1710 \" src=\"https:\/\/blogs.fu-berlin.de\/reseda\/files\/2019\/07\/Maske_applied.png\" alt=\"\" width=\"628\" height=\"509\" \/><\/a><\/p>\n<div style=\"margin: -30px 0 20px 0;text-align: center\"><span style=\"color: #686868;font-size: small\">Applied mask<br \/>\n<\/span><\/div>\n<p>&nbsp;<\/p>\n<p>&#8230;or the whole part in short:<\/p>\n<pre class=\"lang:r decode:true \">sen2_bands_masked_a &lt;- sen2_bands\r\nsen2_bands_masked_a [sen2_mask == 3 |sen2_mask == 7 |sen2_mask == 8 | sen2_mask == 9 |sen2_mask == 10 |sen2_mask == 11 ] &lt;- NA\r\nwriteRaster(sen2_bands_masked_a, \"sen2_masked_alternativ.tif\")<\/pre>\n<p>&nbsp;<\/p>\n<hr style=\"height: 4px;background-color: #6b9e1f\" \/>\n<div style=\"font-family: 'Noto Sans', sans-serif;line-height: 1.2;text-align: right\"><span style=\"font-size: 12px;color: #bfbfbf\"><strong><em>NEXT<\/em><\/strong><\/span><br \/>\n<a style=\"text-decoration: none\" href=\"https:\/\/blogs.fu-berlin.de\/reseda\/sar-data\/\"><span style=\"font-size: 30px;color: #6b9e1f\"><strong><em>SAR DATA<\/em><\/strong><\/span><\/a><\/div>\n<p>&nbsp;<\/p>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>If there are clouds or cloud shadows on your sentinel-2 scene, they can be mask out using the quality scene classification band of your scene. Sentinel-2 scene (true color) with clouds and cloud shadows Image quality band and the classes we want to delete for our mask (Values): 3 (cloud shadows), 7 (unclassified), 8 (cloud &hellip; <a href=\"https:\/\/blogs.fu-berlin.de\/reseda\/quality-band\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Cloud Masking for Sentinel-2 images&#8221;<\/span><\/a><\/p>\n","protected":false},"author":3432,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"class_list":["post-2979","page","type-page","status-publish","hentry"],"_links":{"self":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/2979","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/users\/3432"}],"replies":[{"embeddable":true,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/comments?post=2979"}],"version-history":[{"count":33,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/2979\/revisions"}],"predecessor-version":[{"id":3061,"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/pages\/2979\/revisions\/3061"}],"wp:attachment":[{"href":"https:\/\/blogs.fu-berlin.de\/reseda\/wp-json\/wp\/v2\/media?parent=2979"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}