Last active
December 23, 2024 09:17
-
-
Save kadyb/2bb2ee62b901739d68fbd50e765a0a64 to your computer and use it in GitHub Desktop.
Revisions
-
kadyb revised this gist
Dec 23, 2024 . 1 changed file with 3 additions and 3 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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 = 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 = terra::clamp(x, lower = 0, upper = 1, values = TRUE) x = terra::app(x, fun = sAdj, cores = cores) x = sRGB(satEnh(x, sat)) return(x) -
kadyb revised this gist
Dec 22, 2024 . 1 changed file with 2 additions and 0 deletions.There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) { -
kadyb created this gist
Dec 22, 2024 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal 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) }