File size: 6,752 Bytes
db89723
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8d928e8
 
 
db89723
8d928e8
db89723
8d928e8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
db89723
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

from pathlib import Path

import numpy as np
import rasterio
from rasterio.transform import Affine
from PIL import Image


# ==========================================================
# Read Sentinel-2 RGB bands
# ==========================================================

def read_sentinel_rgb(
    b02_path,
    b03_path,
    b04_path
):
    """
    Read Sentinel-2 B02, B03 and B04 GeoTIFF bands.

    Band mapping:

        B04 -> Red
        B03 -> Green
        B02 -> Blue

    Returns
    -------
    rgb : numpy.ndarray
        RGB array with shape (H, W, 3)

    profile : rasterio profile
        Original geospatial metadata.
    """

    b02_path = Path(b02_path)
    b03_path = Path(b03_path)
    b04_path = Path(b04_path)

    with rasterio.open(b02_path) as src_b02:
        b02 = src_b02.read(1)
        profile = src_b02.profile.copy()
        transform = src_b02.transform
        crs = src_b02.crs
        width = src_b02.width
        height = src_b02.height

    with rasterio.open(b03_path) as src_b03:
        b03 = src_b03.read(1)

    with rasterio.open(b04_path) as src_b04:
        b04 = src_b04.read(1)

    # Make sure all bands have identical dimensions.
    if b02.shape != b03.shape or b02.shape != b04.shape:
        raise ValueError(
            "B02, B03 and B04 dimensions do not match."
        )

    # ------------------------------------------------------
    # Sentinel-2 RGB order:
    # Red   = B04
    # Green = B03
    # Blue  = B02
    # ------------------------------------------------------

    rgb = np.stack(
        [
            b04,
            b03,
            b02
        ],
        axis=-1
    )

    return rgb, profile


# ==========================================================
# Convert raw Sentinel data to visualization RGB
# ==========================================================

def normalize_rgb(
    rgb,
    lower_percentile=2,
    upper_percentile=98
):
    """
    Convert Sentinel-2 reflectance/DN values to uint8 RGB.

    Percentile normalization is performed independently
    for each channel.
    """

    rgb = rgb.astype(np.float32)

    result = np.zeros_like(
        rgb,
        dtype=np.uint8
    )

    for channel in range(3):

        band = rgb[:, :, channel]

        low = np.percentile(
            band,
            lower_percentile
        )

        high = np.percentile(
            band,
            upper_percentile
        )

        if high <= low:
            result[:, :, channel] = np.clip(
                band,
                0,
                255
            ).astype(np.uint8)

        else:

            normalized = (
                (band - low) /
                (high - low)
            )

            normalized = np.clip(
                normalized,
                0,
                1
            )

            result[:, :, channel] = (
                normalized * 255
            ).astype(np.uint8)

    return result


# ==========================================================
# GeoTIFF writer
# ==========================================================

def save_sr_geotiff(
    sr_image,
    source_profile,
    output_path,
    scale=4
):
    """
    Save a super-resolved RGB image as a GeoTIFF.

    The geographic extent remains unchanged while
    the pixel dimensions increase by `scale`.

    Parameters
    ----------
    sr_image : PIL.Image.Image or numpy.ndarray

    source_profile : dict
        Rasterio profile from the original Sentinel image.

    output_path : str or Path

    scale : int
        Super-resolution scale factor.
    """

    output_path = Path(output_path)
    output_path.parent.mkdir(
        parents=True,
        exist_ok=True
    )

    # ------------------------------------------------------
    # Convert image to numpy
    # ------------------------------------------------------

    if isinstance(
        sr_image,
        Image.Image
    ):
        rgb = np.asarray(
            sr_image.convert("RGB")
        )

    else:
        rgb = np.asarray(
            sr_image
        )

    if rgb.ndim != 3 or rgb.shape[2] != 3:
        raise ValueError(
            "sr_image must have shape (H, W, 3)."
        )

    rgb = np.clip(
        rgb,
        0,
        255
    ).astype(np.uint8)

    height, width, _ = rgb.shape

    # ------------------------------------------------------
    # Original transform
    # ------------------------------------------------------

    original_transform = source_profile[
        "transform"
    ]

    # ------------------------------------------------------
    # Scale pixel size by 1/scale
    #
    # Geographic bounds stay unchanged.
    # ------------------------------------------------------

    new_transform = (
        original_transform *
        Affine.scale(
            1.0 / scale,
            1.0 / scale
        )
    )

    # ------------------------------------------------------
    # Build output profile
    # ------------------------------------------------------

    profile = source_profile.copy()

    profile.update(
        {
            "driver": "GTiff",
            "height": height,
            "width": width,
            "count": 3,
            "dtype": "uint8",
            "transform": new_transform,
            "compress": "deflate",
            "photometric": "RGB",
            "tiled": True,
            "blockxsize": 256,
            "blockysize": 256,
        }
    )

    # Remove incompatible fields where necessary.
    profile.pop(
        "nodata",
        None
    )

    # ------------------------------------------------------
    # Write GeoTIFF
    # ------------------------------------------------------
    # Remove block settings when not using tiled GeoTIFF
    profile.pop("blockxsize", None)
    profile.pop("blockysize", None)
    profile.pop("tiled", None)

    with rasterio.open(
        output_path,
        "w",
        **profile
    ) as dst:

        dst.write(
            rgb[:, :, 0],
            1
        )

        dst.write(
            rgb[:, :, 1],
            2
        )

        dst.write(
            rgb[:, :, 2],
            3
        )

    return output_path


# ==========================================================
# Convenience function
# ==========================================================

def sentinel_rgb_from_bands(
    b02_path,
    b03_path,
    b04_path
):
    """
    Read B02/B03/B04 and return a normalized PIL RGB image
    together with the source GeoTIFF profile.
    """

    rgb, profile = read_sentinel_rgb(
        b02_path,
        b03_path,
        b04_path
    )

    rgb_uint8 = normalize_rgb(rgb)

    image = Image.fromarray(
        rgb_uint8,
        mode="RGB"
    )

    return image, profile