寻找图片边缘

问题描述 投票:0回答:1

我有一张图片,由坐标 (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) 注释并被其他像素包围的像素被认为是孔,我正在寻找仅用于边缘的索引。

提前谢谢您

r image
1个回答
4
投票

我有点不清楚你的“边缘结果索引”,但会尝试/建议:

library(terra)
test_rast = terra::rast(test) # your data as above
plot(boundaries(test_rast, classes = FALSE, inner = TRUE))

或:

plot(boundaries(test_rast, classes = FALSE, inner = FALSE))

关于 x/y 索引的一些想法 - 这里使用您不太喜欢的上面的第二张图片。

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)
即可“堆叠”切片。

© www.soinside.com 2019 - 2024. All rights reserved.