【问题标题】:gradient colour scale with gamma parameter?带有伽马参数的渐变色标?
【发布时间】:2013-08-04 21:38:58
【问题描述】:

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

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

我觉得我在重新发明轮子; R中是否已经有这种用于连续色标的工具?

【问题讨论】:

    标签: r graphics colors gradient


    【解决方案1】:

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

    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)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-10-31
      • 2017-06-24
      • 1970-01-01
      • 1970-01-01
      • 2022-10-04
      • 2021-12-23
      • 2019-01-25
      • 1970-01-01
      相关资源
      最近更新 更多