2013-08-03 35 views
2

我有一些非常微弱的對比度和相當多的噪音的成像數據,當我用線性色階顯示時,它不能很好地顯示。在imageJ或photoshop等成像軟件中,可以調節以非線性方式增加對比度的色調曲線,並有效地延伸某些感興趣區域的比例以查看更多細節。帶伽瑪參數的漸變色標?

作爲這種非線性調整參數的一個最簡單情況,@BrianDiggs指出bias參數爲colorRamp,這仍然需要先前將數據轉換爲[0,1]。 我想將非線性尺度推廣到x^gamma以外的其他函數,因此下面的函數實際上並不使用biascolorRamp中,而是在數據端進行轉換。

我覺得我正在重新發明輪子; R中是否有連續色彩比例的工具?

回答

0

這裏是一個可能的解決方案,

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