颜色空间转换用于图像的R代码部分

huangapple go评论58阅读模式
英文:

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")

EDIT: https://entuedu-my.sharepoint.com/:i:/g/personal/bchua024_e_ntu_edu_sg/Ee74d3QJH0FGk5OivZDobx0B9qrwOaNqVx8xnCJW20uxPQ?e=SCaouY

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 &lt;- RGB(1, 0, 0)
as(rgbcol, &quot;LAB&quot;)

EDIT

For the updated question:

# get a tiff image as a RGBA array
library(tiff)
Rlogo &lt;- system.file(&quot;img&quot;, &quot;Rlogo.tiff&quot;, package = &quot;tiff&quot;)
img &lt;- readTIFF(Rlogo)
# remove the fourth channel (A - the transparency)
img &lt;- img[, , -4]

# convert the `img` array to LAB
library(colorspace)
img &lt;- 
  apply(img, 1:2, function(rgb) as(RGB(rgb[1], rgb[2], rgb[3]), &quot;LAB&quot;)@coords)
# restore the order of the dimensions
img &lt;- aperm(img, c(2, 3, 1))

huangapple
  • 本文由 发表于 2023年2月6日 12:11:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/75357274.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定