Search code examples
rr-rastercielab

How to convert RGB image to CIELAB using raster package in R?


I have read an RGB image using the following code

library(raster)
#Load an image
logo <- stack(system.file("external/rlogo.grd", package="raster"))
plot(logo)

Now, I want to convert the RGB image to CIELAB image. How to do it using raster R package? I know CRImage Bioconductor package has a function to implement this (convertRGBToLAB). But I want to implement it using raster R package.


Solution

  • You could use the convertColor() function:

    library(raster)
    logo <- stack(system.file("external/rlogo.grd", package="raster"))
    plot(logo)
    

    new_vals <- values(logo) / 255
    new_vals <- convertColor(new_vals, from = "sRGB", to = "Lab")
    values(logo) <- new_vals
    plot(logo)