我有一个图像处理程序的数据,我需要重新组合在一起。我的图像数据最初是在显微镜载玻片上的18(6 x 3)个孔的阵列,我需要对这些孔进行一致编号,以便我可以在以后识别每个孔中的孔。我有井的近似x和y位置(以像素为单位),我想从左到右,然后从上到下排序(看起来最简单),并将它们编号为1到18。
像这样: [,1] [,2] [,3] [,4] [,5] [,6]
[1,] 1 2 3 4 5 6
[2,] 7 8 9 10 11 12
[3,] 13 14 15 16 17 18
警告:
- 所有井都没有被图像处理程序拾取,所以有时候不到18个。
- 同一行或列中的所有井都在完全相同的x或y上,它们似乎在20-40像素内浮动。
- 并非所有图像都以相同的点或完全相同的放大率为中心,因此位置不够可靠,不足以编码范围。
数据示例:
Well BBXLeft BBYTop
1 0 39 637
2 1 43 1218
3 2 596 630
4 3 610 1212
5 4 1161 633
6 5 1164 1207
7 6 1710 623
8 7 1715 1202
9 8 2267 620
10 9 2271 1199
11 10 2824 617
12 11 2845 1197
13 12 35 57
14 13 593 53
15 14 1709 45
16 15 2262 41
17 16 2820 38
或者来自dput
的可重复形式:
wells <- structure(list(Well = 0:16, BBXLeft = c(39L, 43L, 596L, 610L,1161L, 1164L, 1710L, 1715L, 2267L, 2271L, 2824L, 2845L, 35L, 593L, 1709L, 2262L, 2820L), BBYTop = c(637L, 1218L, 630L, 1212L, 633L, 1207L, 623L, 1202L, 620L, 1199L, 617L, 1197L, 57L, 53L, 45L, 41L, 38L)), .Names = c("Well", "BBXLeft", "BBYTop"), class = "data.frame", row.names = c(NA, -17L))
井从图像分析程序中出现故障 这个例子很好地遗漏了#15(如果从0开始计数则为14)
理想的输出将是一个新的列,左右,上下井数(甚至“战舰”坐标)
很抱歉,如果这是一个家庭作业的问题,但我真的不知道如何有效地做到这一点,而不用硬编码截止。我正在使用R,因为它是我掌握的唯一语言,我还有其他代码可以解决这个问题。
答案 0 :(得分:3)
以下代码将帮助您根据像素位置对井进行分类。
但是,或许更重要的是以下内容 -
这些是针对像素位置绘制的WellNumber标签。 这种标签方案是故意的,还是在标记图像数据时井位被洗牌?
为了对数据进行排序,我们只需要一个自然的中断,例如图像大小。在下面的示例中,我选择了500,但您可以根据需要进行调整。
# sort & plot the pixel corners, to get an idea for where the boundaries re
sort(wells$BBXLeft)
sort(wells$BBYTop)
plot(x=wells$BBXLeft, y=wells$BBYTop)
# add an NA for the missing value (we cant just ignore it)
wells[18,] <- c(NA, 1100, 30)
imgSize <- 500
nrows <- 3
ncols <- 6
# Assign row and col number based on boundaries set every 'imgSize'
# Note that rows are reversed, top to bottom
wells$row <- cut(wells$BBYTop, breaks=seq(imgSize*nrows, 0, -imgSize), label=1:nrows)
wells$col <- cut(wells$BBXLeft, breaks=seq(0, imgSize*ncols, imgSize), label=1:ncols)
# your battleship coordinates
wells$battleship <- paste0("(", wells$row, ", ", wells$col, ")")
# then to sort it, sorting by rows, then by cols
orderedWells <- wells$WellNo[order(wells$row, wells$col)]
# if you want to lay it out nicely, use a matrix
matrix(orderedWells, nrow=nrows, ncol=ncols, byrow=TRUE)
# [,1] [,2] [,3] [,4] [,5] [,6]
# [1,] 12 13 NA 14 15 16
# [2,] 0 2 4 6 8 10
# [3,] 1 3 5 7 9 11