如何将convertcolor函数应用于数据中列出的RGB值。
我正在运行一个实验,需要比较从两个不同时间点选择的RGB值的一致性。为此,我需要将RGB转换为XYZ值,我知道我可以使用ConvertColor进行。
然而!我无法找到能够使用ConvertColor将多个存储在数据中的多个RGB值转换为XYZ的有效方法。我有一个问题在于我不知道如何将RGB值存储为数字值而不是字符的事实。
我的data.table命名syn_rgb将被列出如下:
id | grapheme | rgb_1 | rgb_2 |
---|---|---|---|
s01 | a | (37,13,219)(39,23,211 | ( 50,3,19 |
) s01 | b | ) | (40,23,23,11) |
S02 S02 | A | (43,57,89)(50,50,78 | ) |
S02 | B | (48,59,199) | (50,63,178) |
S03 | A | (100,123,209) | (89,112,200) |
S03 | B | ( 89,120,199) | (87,119,175) |
rgb_1
是第一次给出的RGB,rgb_2
第二次给定的RGB。
这是我到目前为止尝试的:
syn_XYZ <- synRGB(,. (s_xyz = convertColor(RGB_1, from = "sRGB", to = "XYZ", clip = TRUE)), by =. (ID, Grapheme)]
在这里,我正在尝试将rgb_1
中列出的所有RGB值转换为xyz
每个参与者的值在syn_xyz
中。
我尝试使用as.numeric
和strtoi
将RGB值从字符转换为数字,但它无法想象。
我对编程仍然相对较新,因此,如果有一种比尝试这样做的要高得多的方法,我将非常感谢,因为这是我想到如何做的唯一方法。
谢谢
I'm running an experiment where I need to compare the consistency of RGB values selected from two different time points. To do this, I need to convert RGB to XYZ values which I know I can do using ConvertColor.
However! I can't figure out an efficient way of being able to convert multiple RGB values stored in data.table into XYZ using ConvertColor. I have a feeling the problem lies in the fact that I can't figure out how to store RGB values as numeric values rather than characters.
My data.table named syn_RGB will be set out as follows:
ID | Grapheme | RGB_1 | RGB_2 |
---|---|---|---|
S01 | A | (37, 13, 219) | (39, 23, 211) |
S01 | B | (50, 3, 19) | (40, 23, 11) |
S02 | A | (43, 57, 89) | (50, 50, 78) |
S02 | B | (48, 59, 199) | (50, 63, 178) |
S03 | A | (100, 123, 209) | (89, 112, 200) |
S03 | B | (89, 120, 199) | (87, 119, 175) |
RGB_1
is the RGB given for the first time, RGB_2
the RGB given the second time.
This is what I've tried so far:
syn_XYZ <- synRGB(,. (s_xyz = convertColor(RGB_1, from = "sRGB", to = "XYZ", clip = TRUE)), by =. (ID, Grapheme)]
Here I'm trying to convert all the RGB values listed in RGB_1
into XYZ
values for each participant and each grapheme and store them in syn_XYZ
.
I've tried converting the RGB values from characters to numeric using as.numeric
and strtoi
but it doesn't work as you can imagine.
I'm still relatively new to programming so if there's a far more efficient way than trying to do it like this, I would be very appreciative since this is the only way I can think of how to do it.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为
data.table
对此没有任何“有效”的东西,而是对字符串向量的处理。虽然看起来很笨拙,但它的作用是:漫步,大部分是内而外:
regMatches(。gregexpr(“ \\ d+”,。))
将看起来像数字的东西提取到单个数字中;即,“(37,13,219)”
转换为c(“ 37”,“ 13”,“ 219”)
;这将返回列表
,其长度与nrow(Syn_rgb)
lapply(。,as.integer)
将字符串列表转换为列表整数例如,列表(C(“ 37”,“ 13”,“ 219”),C(“ 52”,“ 3”,“ 19”),...) (C(37,13,219),C(52,3,19),...)
。do.call(rbind,。)
将向量列表转换为矩阵,然后asplit(。,2)
将其转换为列列表根据我们的需要。do.call(rgb,。)
用rgb
函数(一次)调用asplit(。,2)
的三个值中的每个值列表作为其参数,添加list的参数(MaxColorValue = 255)
(由于RGB
通常希望其值在[0,1] )。
I don't think
data.table
has anything "efficient" for this, it's just a handling of a string vector. As clunky as it looks, this works:Walk through, mostly inside-out:
regmatches(., gregexpr("\\d+", .))
extracts things that look like numbers into individual numbers; i.e.,"(37, 13, 219)"
is converted intoc("37", "13", "219")
; this returns alist
with the same length asnrow(syn_RGB)
lapply(., as.integer)
converts the list of strings into a list of integer; e.g.,list(c("37", "13", "219"), c("52", "3", "19"), ...)
intolist(c(37, 13, 219), c(52, 3, 19), ...)
.do.call(rbind, .)
converts that list of vectors into a matrix, and thenasplit(., 2)
converts it to a list of columns as we need.do.call(rgb, .)
calls thergb
function (once) with each of the three values of theasplit(., 2)
list as its arguments, adding the argument oflist(maxColorValue=255)
(sincergb
normally wants its values to be on[0,1]
).