R中的伪彩色 [英] pseudocolors in R

查看:156
本文介绍了R中的伪彩色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想把这种16位TIFF图像作为输入:

I'd like to take this kind of 16-bit TIFF image as input:

然后将灰度转换为彩虹伪彩色,如下所示:

then turn the grayscale into rainbow pseudocolors like this:

并添加一个将结果图像再次导出为TIFF图像之前的颜色键。

and add a color key before exporting the resulting image again as TIFF image.

有没有办法将灰度图像转换为R中的伪彩色图像?

Is there a way to turn a grayscale image to a pseudocolor image in R?

推荐答案

我已经针对这个问题制定了以下解决方案(使用下图 http://www.biomedimaging.org/BookImages/GeneExpressionCy3.tif 作为示例)。以下代码读取灰度 TIFF 图像,逆时针旋转90°并使用调色板生成带伪彩色的图像。

I've worked out the following solution for this problem (using the following image http://www.biomedimaging.org/BookImages/GeneExpressionCy3.tif as an example). The following code reads the grayscale TIFF image, rotates it 90° counterclockwise and uses a colour palette to generate an image with pseudocolors.

library(tiff)
library(RColorBrewer)

img <- readTIFF("example.tif")

colnum <- 256

cols <- ceiling(img[,,1] * (colnum - 1) + 1)
# If img consists of a 2D-array: cols <- ceiling(img * (colnum - 1) + 1).
# Use cols <- ceiling(t(img[,,1] * (colnum - 1) + 1)) if you don't want to
# rotate the image.

#pal <- colorRampPalette(c("darkblue", "skyblue", "yellow", "red", "darkred"),
#   bias = 1)(colnum) as suggested by @lukeA
pal <- colorRampPalette(brewer.pal(9, "YlOrBr"), space = 'rgb')(colnum)

# Create lookup-table to match the palette colours with the numeric values
lut <- data.frame(col = 1:colnum, pal, stringsAsFactors = FALSE)
cols2 <- lut[match(cols, lut[,1]),2]
dim(cols2) <- dim(cols)

img.width <- dim(cols)[1]
img.height <- dim(cols)[2]

tiff("example_coloured.tiff", width = img.width, height = img.height,
  units = "px", res = 300)
layout(matrix(c(1,2), nrow = 1), widths = c(4,1), heights = c(4,4))
layout.show(2)

par(mar = c(0,0,0,0))
image(matrix(1:(img.height * img.width), ncol = img.height, nrow = img.width),
  col = cols2, xaxt = "n", yaxt = "n", frame.plot = FALSE)

par(mar = c(0,0.1,0,0))
image(t(matrix(1:img.width, ncol = 1, nrow = img.width)), 
  col = pal, xaxt = "n", yaxt = "n", frame.plot = FALSE)

dev.off()

我唯一遗漏的是如何添加一些文字(例如-high / -low of the ends)到颜色键。我会很感激任何提示。

The only thing I'm still missing is how to add some text (e.g. -high / -low at the ends) to the colour key. I would be grateful for any hints.

这篇关于R中的伪彩色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆