You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I just found the colorize function in the terra package, and I was trying to desaturate an RGB image.
Until now, I used to apply a custom function with app(). But it is even with parallel slow, so I wondered how to change the saturation with colorize.
saturation <- function(rgb, s = .5){
hsl <- plotwidgets::rgb2hsl(as.matrix(rgb))
hsl[2, ] <- s
rgb_new <- as.vector(t(plotwidgets::hsl2rgb(hsl)))
return(rgb_new)
}
I finally tried the following:
library(terra)
r <- rast(system.file("ex/logo.tif", package="terra"))
rh <- colorize(r, to = "hsl")
rh[[2]] <- rh[[2]]*0.1
rh
colorize(rh, to = "rgb")
Error: [] input color scheme should be one of 'hsv', 'hsi' or 'hsl'
Unfortunately, when I convert the hsl back to RGB, I get an error about the color scheme. How should I proceed?
Thanks!!
The text was updated successfully, but these errors were encountered:
It took a couple of small adjustments to make this work.
The app solution with plotwidgets::hsl2rgb
saturation <- function(rgb, s = .5){
hsl <- plotwidgets::rgb2hsl(as.matrix(rgb))
hsl[2, ] <- s
as.vector(t(plotwidgets::hsl2rgb(hsl)))
}
library(terra)
r <- rast(system.file("ex/logo.tif", package="terra"))
a <- app(r, saturation)
And now with colorize.
rh <- colorize(r, to = "hsl")
# you can now replace all values of a layer like this
rh[[2]] <- .5
# but you need to reset the colorspace
set.RGB(rh, 1:3, "hsl")
b <- colorize(rh, to = "rgb")
The need to use set.RGB in between the two colorize steps is not documented in the help file for colorize which just says you can use "rgb" as the to argument for colorize. I was completely stuck trying to do the same thing until I found this issue.
I just found the colorize function in the terra package, and I was trying to desaturate an RGB image.
Until now, I used to apply a custom function with app(). But it is even with parallel slow, so I wondered how to change the saturation with colorize.
I finally tried the following:
Unfortunately, when I convert the hsl back to RGB, I get an error about the color scheme. How should I proceed?
Thanks!!
The text was updated successfully, but these errors were encountered: