Я хочу создать «глобальный» идентификатор двумерных / трехмерных ядерных масок в списке изображений.Когда я сегментирую свои ячейки, они нумеруются от 1: numberOfCellsInEachImage.но я хотел бы иметь уникальный идентификатор для каждой ячейки на всех изображениях.
Я уже пытался создать forloop с глобальным счетчиком и назначенными идентификаторами в соответствии со счетчиком, но это стало грязной большой вещью, котораяЯ не доверяю полностью
# minimal exampe
img1 = matrix(data = 0, nrow = 15, ncol = 15)
img1[2:3, 2:3] = 1
img1[9:10, 5:6] = 2
img1[2:4, 11:13] = 3
img1
# Example indexed image 1
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
# [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [2,] 0 1 1 0 0 0 0 0 0 0 3 3 3 0 0
# [3,] 0 1 1 0 0 0 0 0 0 0 3 3 3 0 0
# [4,] 0 0 0 0 0 0 0 0 0 0 3 3 3 0 0
# [5,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [6,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [7,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [8,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [9,] 0 0 0 0 2 2 0 0 0 0 0 0 0 0 0
#[10,] 0 0 0 0 2 2 0 0 0 0 0 0 0 0 0
#[11,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#[12,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#[13,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#[14,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#[15,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
img2 = matrix(data = 0, nrow = 15, ncol = 15)
img2[3:4, 2:4] = 1
img2[10:11, 5:7] = 2
img2[3:5, 11:14] = 3
img2
# [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
# [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [2,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [3,] 0 1 1 1 0 0 0 0 0 0 3 3 3 3 0
# [4,] 0 1 1 1 0 0 0 0 0 0 3 3 3 3 0
# [5,] 0 0 0 0 0 0 0 0 0 0 3 3 3 3 0
# [6,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [7,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [8,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# [9,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#[10,] 0 0 0 0 2 2 2 0 0 0 0 0 0 0 0
#[11,] 0 0 0 0 2 2 2 0 0 0 0 0 0 0 0
#[12,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#[13,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#[14,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#[15,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# This is the data structure I have
#
listOfImages2D = list(img1, img2)
#x = listOfImages2D[[1]]
listOfImages3D = lapply(listOfImages2D, function(x){
x_out = array(data = 0, dim = c(15, 15, 3) )
x_out[,,2] = x
return(x_out)
})
#What I want
# I want all numbers to become an uniqe ID across images
# All zeros should remain zero
# I use img1[img1 == 0 ] = 0 #after giving uniqe IDs
# Image one should ideally start at 1
# ## desired output
# listOfImages2D_global_index[[1]]
# # same as above
#
# listOfImages2D_global_index[[2]]
# # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
# # [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# # [2,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# # [3,] 0 4 4 4 0 0 0 0 0 0 6 6 6 6 0
# # [4,] 0 4 4 4 0 0 0 0 0 0 6 6 6 6 0
# # [5,] 0 0 0 0 0 0 0 0 0 0 6 6 6 6 0
# # [6,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# # [7,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# # [8,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# # [9,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# #[10,] 0 0 0 0 5 5 5 0 0 0 0 0 0 0 0
# #[11,] 0 0 0 0 5 5 5 0 0 0 0 0 0 0 0
# #[12,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# #[13,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# #[14,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
# #[15,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
#
# # the code should also work for 3 dimensions