英文:
Colorspace transformation for an image in R
问题
I want to transform the colorspace of a .TIFF file from RGB to lαβ in R. There is base package grDevices
which allows you to change the colorspace using the function convertColor
. This doesn't have any option for lαβ.
If there are any existing functions or libraries it'll be very helpful.
I tried the following approach, but it has different lαβ tables for each of the three colors, i.e., Red, Green, and Blue:
library(schemr)
HCC1_lab_r <- rgb_to_lab(HCC1[,,1], transformation = "sRGB")
EDIT: Link
here's the link to the image I'm trying to convert.
英文:
I want to transform the colorspace of a .TIFF file from RGB to lαβ in R. There is base package grDevices
which allows you to change the colorspace using the function convertColor
. This doesn't have any option for lαβ.
If there are any existing functions or libraries it'll be very helpful.
I tried the following approach, but it has different lαβ tables for each of the three colors, i.e., Red, Green, and Blue:
library(schemr)
HCC1_lab_r <- rgb_to_lab(HCC1[,,1], transformation = "sRGB")
here's the link to the image I'm trying to convert.
答案1
得分: 1
你可以使用colorspace包进行此转换,如下所示:
library(colorspace)
rgbcol <- RGB(1, 0, 0)
as(rgbcol, "LAB")
编辑
对于更新的问题:
# 获取一个tiff图像作为RGBA数组
library(tiff)
Rlogo <- system.file("img", "Rlogo.tiff", package = "tiff")
img <- readTIFF(Rlogo)
# 移除第四个通道(A - 透明度)
img <- img[, , -4]
# 将`img`数组转换为LAB
library(colorspace)
img <-
apply(img, 1:2, function(rgb) as(RGB(rgb[1], rgb[2], rgb[3]), "LAB")@coords)
# 恢复维度的顺序
img <- aperm(img, c(2, 3, 1))
英文:
You can use the colorspace package to do such a conversion, as follows:
library(colorspace)
rgbcol <- RGB(1, 0, 0)
as(rgbcol, "LAB")
EDIT
For the updated question:
# get a tiff image as a RGBA array
library(tiff)
Rlogo <- system.file("img", "Rlogo.tiff", package = "tiff")
img <- readTIFF(Rlogo)
# remove the fourth channel (A - the transparency)
img <- img[, , -4]
# convert the `img` array to LAB
library(colorspace)
img <-
apply(img, 1:2, function(rgb) as(RGB(rgb[1], rgb[2], rgb[3]), "LAB")@coords)
# restore the order of the dimensions
img <- aperm(img, c(2, 3, 1))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论