Skip to content

Instantly share code, notes, and snippets.

@kadyb
Last active December 23, 2024 09:17
Show Gist options
  • Select an option

  • Save kadyb/2bb2ee62b901739d68fbd50e765a0a64 to your computer and use it in GitHub Desktop.

Select an option

Save kadyb/2bb2ee62b901739d68fbd50e765a0a64 to your computer and use it in GitHub Desktop.

Revisions

  1. kadyb revised this gist Dec 23, 2024. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions enhanceRGB.R
    Original file line number Diff line number Diff line change
    @@ -33,7 +33,7 @@ enhanceRGB = function(x, maxR = 3, midR = 0.13, sat = 1.2, gamma = 1.8,

    sRGB = function(c) {
    thresh = 0.0031308
    c = ifel(
    c = terra::ifel(
    c <= thresh,
    12.92 * c,
    (1.055 * c**0.41666666666) - 0.055
    @@ -42,8 +42,8 @@ enhanceRGB = function(x, maxR = 3, midR = 0.13, sat = 1.2, gamma = 1.8,
    }

    ## processing ----------------------------------------------------------------
    x = clamp(x, lower = 0, upper = 1, values = TRUE)
    x = app(x, fun = sAdj, cores = cores)
    x = terra::clamp(x, lower = 0, upper = 1, values = TRUE)
    x = terra::app(x, fun = sAdj, cores = cores)
    x = sRGB(satEnh(x, sat))
    return(x)

  2. kadyb revised this gist Dec 22, 2024. 1 changed file with 2 additions and 0 deletions.
    2 changes: 2 additions & 0 deletions enhanceRGB.R
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,5 @@
    # Original source: https://custom-scripts.sentinel-hub.com/sentinel-2/l2a_optimized/

    enhanceRGB = function(x, maxR = 3, midR = 0.13, sat = 1.2, gamma = 1.8,
    cores = 1) {

  3. kadyb created this gist Dec 22, 2024.
    48 changes: 48 additions & 0 deletions enhanceRGB.R
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,48 @@
    enhanceRGB = function(x, maxR = 3, midR = 0.13, sat = 1.2, gamma = 1.8,
    cores = 1) {

    ## functions -----------------------------------------------------------------
    sAdj = function(a) {
    adjGamma(adj(a, midR, 1, maxR))
    }

    gOff = 0.01
    gOffPow = gOff**gamma
    gOffRange = (1 + gOff)**gamma - gOffPow

    adjGamma = function(b) {
    ((b + gOff)**gamma - gOffPow) / gOffRange
    }

    adj = function(a, tx, ty, maxC) {
    ar = a / maxC
    ar * (ar * (tx / maxC + ty - 1) - ty) / (ar * (2 * tx / maxC - 1) - tx / maxC)
    }

    satEnh = function(r, ...) {
    avgS = mean(terra::global(r, fun = "mean", na.rm = TRUE)[["mean"]])
    f = function(band, ...) {avgS + band * sat}
    r[[1]] = terra::lapp(r[[1]], fun = f, cores = cores)
    r[[2]] = terra::lapp(r[[2]], fun = f, cores = cores)
    r[[3]] = terra::lapp(r[[3]], fun = f, cores = cores)
    r = terra::clamp(r, lower = 0, upper = 1, values = TRUE)
    return(r)
    }

    sRGB = function(c) {
    thresh = 0.0031308
    c = ifel(
    c <= thresh,
    12.92 * c,
    (1.055 * c**0.41666666666) - 0.055
    )
    return(c)
    }

    ## processing ----------------------------------------------------------------
    x = clamp(x, lower = 0, upper = 1, values = TRUE)
    x = app(x, fun = sAdj, cores = cores)
    x = sRGB(satEnh(x, sat))
    return(x)

    }