Advertisement
Guest User

Untitled

a guest
Jun 26th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.00 KB | None | 0 0
  1. # minimal exampe
  2. img1 = matrix(data = 0, nrow = 15, ncol = 15)
  3. img1[2:3, 2:3] = 1
  4. img1[9:10, 5:6] = 2
  5. img1[2:4, 11:13] = 3
  6. img1
  7. # Example indexed image 1
  8. # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
  9. # [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  10. # [2,] 0 1 1 0 0 0 0 0 0 0 3 3 3 0 0
  11. # [3,] 0 1 1 0 0 0 0 0 0 0 3 3 3 0 0
  12. # [4,] 0 0 0 0 0 0 0 0 0 0 3 3 3 0 0
  13. # [5,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  14. # [6,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  15. # [7,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  16. # [8,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  17. # [9,] 0 0 0 0 2 2 0 0 0 0 0 0 0 0 0
  18. #[10,] 0 0 0 0 2 2 0 0 0 0 0 0 0 0 0
  19. #[11,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  20. #[12,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  21. #[13,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  22. #[14,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  23. #[15,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  24. img2 = matrix(data = 0, nrow = 15, ncol = 15)
  25. img2[3:4, 2:4] = 1
  26. img2[10:11, 5:7] = 2
  27. img2[3:5, 11:14] = 3
  28. img2
  29. # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
  30. # [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  31. # [2,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  32. # [3,] 0 1 1 1 0 0 0 0 0 0 3 3 3 3 0
  33. # [4,] 0 1 1 1 0 0 0 0 0 0 3 3 3 3 0
  34. # [5,] 0 0 0 0 0 0 0 0 0 0 3 3 3 3 0
  35. # [6,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  36. # [7,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  37. # [8,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  38. # [9,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  39. #[10,] 0 0 0 0 2 2 2 0 0 0 0 0 0 0 0
  40. #[11,] 0 0 0 0 2 2 2 0 0 0 0 0 0 0 0
  41. #[12,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  42. #[13,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  43. #[14,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  44. #[15,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  45.  
  46. # This is the data structure I have
  47. #
  48. listOfImages2D = list(img1, img2)
  49. #x = listOfImages2D[[1]]
  50. listOfImages3D = lapply(listOfImages2D, function(x){
  51. x_out = array(data = 0, dim = c(15, 15, 3) )
  52. x_out[,,2] = x
  53. return(x_out)
  54. })
  55.  
  56. #What I want
  57. # I want all numbers to become an uniqe ID across images
  58. # All zeros should remain zero
  59. # I use img1[img1 == 0 ] = 0 #after giving uniqe IDs
  60. # Image one should ideally start at 1
  61.  
  62. # ## desired output
  63. # listOfImages2D_global_index[[1]]
  64. # # same as above
  65. #
  66. # listOfImages2D_global_index[[2]]
  67. # # [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [,14] [,15]
  68. # # [1,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  69. # # [2,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  70. # # [3,] 0 4 4 4 0 0 0 0 0 0 6 6 6 6 0
  71. # # [4,] 0 4 4 4 0 0 0 0 0 0 6 6 6 6 0
  72. # # [5,] 0 0 0 0 0 0 0 0 0 0 6 6 6 6 0
  73. # # [6,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  74. # # [7,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  75. # # [8,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  76. # # [9,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  77. # #[10,] 0 0 0 0 5 5 5 0 0 0 0 0 0 0 0
  78. # #[11,] 0 0 0 0 5 5 5 0 0 0 0 0 0 0 0
  79. # #[12,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  80. # #[13,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  81. # #[14,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  82. # #[15,] 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
  83. #
  84. # # the code should also work for 3 dimensions
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement