我有一张图片,它由每个像素中的坐标(x,y)和一些强度参数组成.它的形状不规则.它还可能有一些洞.这是我拥有的矩阵的一个例子:

set.seed(20)
test <- expand.grid(1:25, 1:50)
test$Int <- rnorm(nrow(test), 7, 1)
edge_rows <- which(test$Var1 %in% c(1, 25) | test$Var2 %in% c(1, 50))
rows_to_remove <- sample(edge_rows, 50) 
test <- test[-rows_to_remove, ]
edge_rows <- which(test$Var1 %in% c(2, 24) | test$Var2 %in% c(2, 49))
rows_to_remove <- sample(edge_rows, 25) 
test <- test[-rows_to_remove, ]
rows_to_remove <- sample(edge_rows, 75) 
test <- test[-sample(nrow(test), 50), ]
ggplot(test) +
    geom_tile(aes(Var1, Var2, fill = Int))

没有(x,y,Int)注释且被其他像素包围的像素被认为是洞,我正在寻找仅针对边缘的索引.

提前感谢您

推荐答案

我有点不清楚您的"结果边缘指数",但会try /建议:

library(terra)
test_rast = terra::rast(test) # your data as above

plot(boundaries(test_rast, classes = FALSE, inner = TRUE)) # or
# test_bound_inner <- boundaries(test_rast, classes = FALSE, inner = TRUE)
# plot is wrapping the boundaries function but doesn't result in an object, just the plot
# plot(test_bound_inner) 
# retains test_bound_inner as an object in your session

enter image description here

或者:

plot(boundaries(test_rast, classes = FALSE, inner = FALSE))
# test_bound_inF <- boundaries(test_rast, classes = FALSE, inner = FALSE)

enter image description here

关于x/y索引的一些 idea -这里使用上面不太喜欢的第二张图像.

test_bound_inF_mtx <- as.matrix(test_bound_inF, wide = TRUE)
> which(test_bound_inF_mtx == 0, arr.ind = TRUE)
        row col
   [1,]   1   1
   [2,]   3   1
   [3,]   5   1
   [4,]  10   1
   [5,]  12   1
   [6,]  13   1
   [7,]  14   1
   [8,]  15   1
   [9,]  16   1
  [10,]  17   1
<---snip--->

但是,如果总体轨迹进一步对齐连续切片,(例如如果构建3D精细化),您可能需要?terra::?xyFromCell的某种变体,以给出像素重心的x/y.切片可以简单地与my_many_slices <- c(slice1, slice1, slice3 & etc)"堆叠".

R相关问答推荐

如何使用Cicerone指南了解R Shiny中传单 map 的元素?

R形式的一维数字线/箱形图样式图表

基于现有类创建类的打印方法(即,打印tibles更长时间)

如何根据组大小应用条件过滤?

在R中替换函数中的特定符号

整数成随机顺序与约束R?

单个轮廓重叠条的单独图例

在R中使用数据集名称

在连续尺度上转置标签[瀑布图,R]

为了网络分析目的,将数据框转换为长格式列联表

如何从容器函数中提取conf并添加到ggplot2中?

如何对r中包含特定(未知)文本的行求和?

优化从每个面的栅格中提取值

我如何使用tidyselect来传递一个符号数组,比如Pivot_Long?

如何使用FormC使简单算术运算得到的数字是正确的?

仅当后续值与特定值匹配时,才在列中回填Nas

如何根据未知数的多列排除重复行

根据排名的顶点属性调整曲线图布局(&Q)

如何为包创建自定义roxygen2标签?

从字符串列中的向量中查找第一个匹配的单词