gpt4 book ai didi

r - 具有 Gamma 参数的渐变色标?

转载 作者:行者123 更新时间:2023-12-05 08:00:34 26 4
gpt4 key购买 nike

我有一些对比度非常微弱且有相当多噪点的成像数据,当我使用线性色标显示时,显示效果不佳。在 imageJ 或 photoshop 等成像软件中,有一条色调曲线,可以对其进行调整以非线性方式增强对比度,并有效地拉伸(stretch)某些感兴趣区域的比例以查看更多细节。

作为这种非线性调整参数的最简单情况,@BrianDiggs 指出了 bias colorRamp 的参数,这仍然需要先前将数据转换为 [0, 1]。我想将非线性比例推广到除 x^gamma 以外的其他泛函,因此下面的函数实际上并没有使用 biascolorRamp但在数据端进行转换。

我觉得我正在重新发明轮子; R中已经有这样的连续色标工具了吗?

最佳答案

这是一个可能的解决方案,

set.seed(123)
x <- sort(runif(1e4, min=-20 , max=120))

library(scales) # rescale function

curve_pal <- function (x, colours = rev(blues9),
fun = function(x) x^gamma,
n=10, gamma=1)
{
# function that maps [0,1] -> colours
palfun <- colorRamp(colors=colours)

# now divide the data in n equi-spaced regions, mapped linearly to [0,1]
xcuts <- cut(x, breaks=seq(min(x), max(x), length=n))
xnum <- as.numeric(xcuts)

# need to work around NA values that make colorRamp/rgb choke
testNA <- is.na(xnum)
xsanitised <- ifelse(testNA, 0, fun(rescale(xnum)))

# non-NA values in [0,1] get assigned their colour
ifelse(testNA, NA, rgb(palfun(xsanitised), maxColorValue=255))
}

library(gridExtra)
grid.newpage()
grid.arrange(rasterGrob(curve_pal(x, gamma=0.5), wid=1, heig=1, int=F),
rasterGrob(curve_pal(x, gamma=1), wid=1, heig=1, int=F),
rasterGrob(curve_pal(x, gamma=2), wid=1, heig=1, int=F),
nrow=1)

enter image description here

关于r - 具有 Gamma 参数的渐变色标?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18036849/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com