Exemplo n.º 1
0
def pansharpen(vis, vis_transform, pan, pan_transform,
               pan_dtype, r_crs, dst_crs, weight,
               method="Brovey", src_nodata=0):
    """Pansharpen a lower-resolution visual band

    Parameters
    =========
    vis: ndarray, 3D with shape == (3, vh, vw)
        Visual band array with RGB bands
    vis_transform: Affine
        affine transform defining the georeferencing of the vis array
    pan: ndarray, 2D with shape == (ph, pw)
        Panchromatic band array
    pan_transform: Affine
        affine transform defining the georeferencing of the pan array
    method: string
        Algorithm for pansharpening; default Brovey

    Returns:
    ======
    pansharp: ndarray, 3D with shape == (3, ph, pw)
        pansharpened visual band
        affine transform is identical to `pan_transform`
    """
    rgb = _upsample(_create_apply_mask(vis), pan.shape, vis_transform, r_crs,
                    pan_transform, dst_crs)

    # Main Pansharpening Processing
    if method == "Brovey":
        pansharp, _ = Brovey(rgb, pan, weight, pan_dtype)
    # TODO: add other methods

    return pansharp
Exemplo n.º 2
0
def postprocess(windows):
    from . import _nodata

    windows = list(filter(None, windows))

    landsat_windows = filter(lambda sw: "LC08_" in sw[0].url, windows)
    landsat_windows = dict([(k, list(v)) for k, v in itertools.groupby(
        landsat_windows, lambda x: x[0].url.split("/")[-2])])

    pan_bands = dict(
        list(
            map(
                lambda sw: (sw[0].url.split("/")[-2], sw[1]),
                filter(lambda sw: sw[0].band == 4, filter(None, windows)),
            )))

    for source, pixels in windows:
        if pixels is None or pixels.data is None:
            continue

        if "landsat8" in source.recipes:
            scene_id = source.url.split("/")[-2]
            source = Source(
                "/".join(source.url.split("/")[0:-1]),
                source.name,
                source.resolution,
                source.band_info,
                source.meta,
                source.recipes,
                source.acquired_at,
                None,
                source.priority,
                source.coverage,
            )

            # pick out all bands for the same scene the first time it's seen
            if scene_id in landsat_windows:
                ws = filter(lambda sw: is_rgb(sw[0].band),
                            landsat_windows.pop(scene_id))
                canvas_data = np.ma.zeros(
                    (3, ) + pixels.data.shape[1:],
                    dtype=pixels.data.dtype,
                    fill_value=_nodata(np.int16),
                )
                canvas_data.mask = True

                canvas = PixelCollection(canvas_data, pixels.bounds)
                pixels = reduce(_reduce_landsat_windows, ws, canvas)

                if scene_id in pan_bands:
                    pan = pan_bands[scene_id]

                    pansharpened, _ = Brovey(pixels.data, pan.data[0], 0.2,
                                             pan.data.dtype)

                    yield source, PixelCollection(pansharpened, pixels.bounds)
                else:
                    yield source, pixels
        else:
            yield source, pixels
Exemplo n.º 3
0
def test_Brovey(rgb, pan, weight, pan_dtype):
    brovey_output, brovey_ratio = Brovey(rgb, pan, weight, pan_dtype)
    ratio = pan / ((rgb[0] + rgb[1] + rgb[2] * weight) / (2 + weight))
    output = np.clip(ratio * rgb, 0, np.iinfo(pan_dtype).max).astype(pan_dtype)
    assert np.array_equal(brovey_output, output)
    assert np.array_equal(brovey_ratio, ratio)