繁体   English   中英

R 中 plot 图像堆栈的优雅方式

[英]Elegant way to plot image stacks in R

我正在尝试可视化 3D 数组切片(即图像堆栈)。 令人惊讶的是,我找不到任何带有内置 function 的 package 可以轻松完成这项工作。

我为一些虚拟数据提供了一个带有我自己的解决方案的示例代码,但我想知道是否有更简单且计算成本更低的解决方法? 一些大型计算机断层扫描 (CT) 扫描数据对我的机器造成了相当大的影响。

示例代码:

library(leaflet) #color palette
library(rgl) #3d visualisation

#example data
slides = array(data = 0,
               dim = c(200,200,15))

#add some intensity values
for(i in 1:dim(slides)[3]) slides[20:180,20:180,i] = i*10

#voxel dimensions for x/y/z
dims = c(1,1,20) #adjust z dimension for visualisation

#all possible x/y/z combinations
xyz = expand.grid(1:dim(slides)[1],1:dim(slides)[2],1:dim(slides)[3]) 

#find the intensity value for each data point
value = apply(xyz, 1, function(x) slides[x[1],x[2],x[3]]) 
#invert values
value = max(value) - value 

#apply the voxel dimensions to x/y/z
xyz = t(t(xyz) * dims) 

#convert intensity values to colors
colb = colorNumeric(palette = "Greys",
                   domain = c(0,max(value)))
col = colb(value)

#plot image stack
plot3d(xyz, col = col, aspect = FALSE, axes = FALSE, xlab = "", ylab = "", zlab = "")  

结果:

在此处输入图像描述

rgl中使用纹理而不是绘图点会快得多。 例如:

library(rgl) #3d visualisation

#example data
slides = array(data = 0,
               dim = c(200,200,15))

#add some intensity values

n <- dim(slides)[3]

for (i in 1:n) slides[20:180,20:180,i] = i*10

# Now plot each slide to a separate file to use as a texture

filenames <- character(n)
for (i in 1:n) {
  filenames[i] <- tempfile(fileext = ".png")
  png(filenames[i], width=200, height=200)
  raster <- as.raster(slides[,,i], max = max(slides))
  par(mar = c(0,0,0,0))
  plot(raster)
  dev.off()
}

# Now plot one 3D quad per slide
open3d()
#> glX 
#>   1
texturequad <- cbind(c(0, 1, 1, 0), c(0, 0, 1, 1))
quad <- cbind(texturequad*200, 0)
for (i in 1:n) {
  quad[,3] <- i
  quads3d(quad, texcoords = texturequad, texture = filenames[i],
          col = "white", lit = FALSE)
}

# Set the shape of the display as desired
aspect3d(1,1,2)

lowlevel()   # Just to get reprex to show it

使用reprex v2.0.2创建于 2022-09-27

我没有用你的调色板; 如果需要,您应该将slides的条目设置为颜色值而不是数值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM