我正在使用R和EBImage包编程一个用于图像分析的闪亮应用程序。 经过一些困难,我有一些不错的成绩,但在时间绘制computeFeatures的结果,我有一些奇怪的行为:DataTableOutput中的混沌顺序和多帧图像中的奇怪computeFeature图
- 我不得不明令“细胞”栏才能正常目前datatableoutput数据。
- 图形具有“帧”因子的倒序。
- 分布图显示帧不共享图像的通用像素尺寸。看到帧之间没有坐标x和坐标y的重叠。这意味着像框架不在同一个位置。
下面是一个例子:
library(EBImage)
library(shiny)
library(tidyverse)
library(DT)
ui <- basicPage(
column(
width = 3,
h3("Images"),
displayOutput("nuc"),
displayOutput("nucbw")
),
column(
width = 9,
h3("Cell Features"),
DT::dataTableOutput("basicfeatures"),
hr(),
fluidRow(
column(
width = 4,
plotOutput("plot1")
),
column(
width = 8,
plotOutput("plot2")
)
)
)
)
server <- function(input, output) {
# Load Image
nuc <- readImage(system.file("images", "nuclei.tif", package="EBImage"))
# Segmented Image
nucbw <- bwlabel(nuc > 0.5)
# Display Original Image
output$nuc <- renderDisplay(display(nuc))
# Display Segmented Image
output$nucbw <- renderDisplay(display(nucbw))
# Compute Features
features <- reactive({
# Create empty dataframe
data <- data.frame()
# Obect to save total cell number
ntotal <- 0L
# Compute for each frame
for (i in 1:numberOfFrames(nuc)){
nobjects <- max(nucbw[,,i])
cell <- seq.int(from = ntotal + 1L, length.out = nobjects)
ntotal <- ntotal + nobjects
# Create frame column to know the frame where each cell belongs
frame <- rep(paste("Frame", i, sep = " "), nobjects)
# Create features dataframe
x1 <- computeFeatures.basic(nucbw[,,i], nuc[,,i])
x2 <- computeFeatures.shape(nucbw[,,i], nuc[,,i])
x3 <- computeFeatures.moment(nucbw[,,i], nuc[,,i])
# Binding dataframe for each frame
bind <- cbind(cell, frame, x1, x2, x3)
# Binding dataframe different frame
data <- rbind(bind, data)
}
# Convert "cell" to numeric
cell <- as.numeric(as.character(data$cell))
# "frame" remains untouched
frame <- data$frame
# Convert computeFeatures to numeric
temp <- as.data.frame(data.matrix(data[,-c(1,2)]))
# Binding to a unique data frame
data <- cbind(cell, frame, temp)
data
})
# Render Features Table
output$basicfeatures <- DT::renderDataTable(
features(),
rownames = FALSE,
caption = "Cell Features",
extensions = list(
"ColReorder" = NULL,
"FixedHeader" = NULL
),
options = list(
pageLength = 10,
colReorder = TRUE,
fixedHeader = TRUE,
scrollX = TRUE,
order = list(0, 'asc')
)
)
data2 <- reactive({
features() %>%
group_by(frame) %>%
summarise(n = n())
})
output$plot1<- renderPlot({
g <- ggplot(data2(), aes(x = frame, y = n)) +
geom_col(aes(fill = frame), color = "black") +
labs(title = "NUMBER OF CELLS PER FRAME", x = "FRAME", y = "NUMBER OF CELLS") +
theme_bw(base_size = 16) +
theme(legend.position = "none") +
theme(axis.text = element_text(size = 14))
g
})
output$plot2<- renderPlot({
g <- ggplot(features(), aes(x = m.cx, y = m.cy, fill = frame, color = frame)) +
geom_point(shape = 21, size = 4, alpha = 0.2) +
labs(title = "DISTRIBUTION OF CELLS", x = "COORDINATE X", y = "COORDINATE Y") +
theme_bw(base_size = 16) +
theme(
rect = element_rect(colour = "red"),
strip.background = element_rect(colour = "black", fill = "white"),
strip.text.x = element_text(colour = "black"),
strip.text.y = element_text(colour = "black")
) +
facet_wrap(~ frame, nrow = 1)
g
})
}
shinyApp(ui, server)
执行'data < - rbind(data,bind)'而不是'data < - rbind(bind,data)'应该可以解决与订单相关的问题。 – SBista
你说得对。它解决了我的问题的前两点。谢谢 – Archymedes