pack_tile.py 9.81 KB
Newer Older
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
1 2 3 4 5 6 7 8
#!/usr/bin/env python3

__copyright__ = "Copyright 2018, Elphel, Inc."
__license__   = "GPL-3.0+"
__email__     = "oleg@elphel.com"

import numpy as np

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
9 10
# pack from 9x9x4 to 100x1
# type 1, all the same
11
def pack_tile_type1(tile):
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
12

13
  out = np.empty(100)
14

15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125
  # pack diagm-pair
  l = np.ravel(tile[:,:,0])
  out[ 0] = 1.0*l[ 2]+1.0*l[ 3]+1.0*l[ 4]+1.0*l[ 5]+1.0*l[6]
  out[ 1] = 1.0*l[10]+1.0*l[11]+1.0*l[19]+1.0*l[20]
  out[ 2] = 1.0*l[12]+1.0*l[13]+1.0*l[14]
  out[ 3] = 1.0*l[15]+1.0*l[16]+1.0*l[24]+1.0*l[25]
  out[ 4] = 1.0*l[18]+1.0*l[27]+1.0*l[36]+1.0*l[45]+1.0*l[54]
  out[ 5] = 1.0*l[21]+1.0*l[22]+1.0*l[23]
  out[ 6] = 1.0*l[26]+1.0*l[35]+1.0*l[44]+1.0*l[53]+1.0*l[62]
  out[ 7] = 1.0*l[28]+1.0*l[37]+1.0*l[46]
  out[ 8] = 1.0*l[29]+1.0*l[38]+1.0*l[47]
  out[ 9] = 1.0*l[30]
  out[10] = 1.0*l[31]
  out[11] = 1.0*l[32]
  out[12] = 1.0*l[33]+1.0*l[42]+1.0*l[51]
  out[13] = 1.0*l[34]+1.0*l[43]+1.0*l[52]
  out[14] = 1.0*l[39]
  out[15] = 1.0*l[40]
  out[16] = 1.0*l[41]
  out[17] = 1.0*l[48]
  out[18] = 1.0*l[49]
  out[19] = 1.0*l[50]
  out[20] = 1.0*l[55]+1.0*l[56]+1.0*l[64]+1.0*l[65]
  out[21] = 1.0*l[57]+1.0*l[58]+1.0*l[59]
  out[22] = 1.0*l[60]+1.0*l[61]+1.0*l[69]+1.0*l[70]
  out[23] = 1.0*l[66]+1.0*l[67]+1.0*l[68]
  out[24] = 1.0*l[74]+1.0*l[75]+1.0*l[76]+1.0*l[77]+1.0*l[78]

  # pack diago-pair
  l = np.ravel(tile[:,:,1])
  out[25] = 1.0*l[ 2]+1.0*l[ 3]+1.0*l[ 4]+1.0*l[ 5]+1.0*l[6]
  out[26] = 1.0*l[10]+1.0*l[11]+1.0*l[19]+1.0*l[20]
  out[27] = 1.0*l[12]+1.0*l[13]+1.0*l[14]
  out[28] = 1.0*l[15]+1.0*l[16]+1.0*l[24]+1.0*l[25]
  out[29] = 1.0*l[18]+1.0*l[27]+1.0*l[36]+1.0*l[45]+1.0*l[54]
  out[30] = 1.0*l[21]+1.0*l[22]+1.0*l[23]
  out[31] = 1.0*l[26]+1.0*l[35]+1.0*l[44]+1.0*l[53]+1.0*l[62]
  out[32] = 1.0*l[28]+1.0*l[37]+1.0*l[46]
  out[33] = 1.0*l[29]+1.0*l[38]+1.0*l[47]
  out[34] = 1.0*l[30]
  out[35] = 1.0*l[31]
  out[36] = 1.0*l[32]
  out[37] = 1.0*l[33]+1.0*l[42]+1.0*l[51]
  out[38] = 1.0*l[34]+1.0*l[43]+1.0*l[52]
  out[39] = 1.0*l[39]
  out[40] = 1.0*l[40]
  out[41] = 1.0*l[41]
  out[42] = 1.0*l[48]
  out[43] = 1.0*l[49]
  out[44] = 1.0*l[50]
  out[45] = 1.0*l[55]+1.0*l[56]+1.0*l[64]+1.0*l[65]
  out[46] = 1.0*l[57]+1.0*l[58]+1.0*l[59]
  out[47] = 1.0*l[60]+1.0*l[61]+1.0*l[69]+1.0*l[70]
  out[48] = 1.0*l[66]+1.0*l[67]+1.0*l[68]
  out[49] = 1.0*l[74]+1.0*l[75]+1.0*l[76]+1.0*l[77]+1.0*l[78]

  # pack hor-pairs
  l = np.ravel(tile[:,:,2])
  out[50] = 1.0*l[ 2]+1.0*l[ 3]+1.0*l[ 4]+1.0*l[ 5]+1.0*l[6]
  out[51] = 1.0*l[10]+1.0*l[11]+1.0*l[19]+1.0*l[20]
  out[52] = 1.0*l[12]+1.0*l[13]+1.0*l[14]
  out[53] = 1.0*l[15]+1.0*l[16]+1.0*l[24]+1.0*l[25]
  out[54] = 1.0*l[18]+1.0*l[27]+1.0*l[36]+1.0*l[45]+1.0*l[54]
  out[55] = 1.0*l[21]+1.0*l[22]+1.0*l[23]
  out[56] = 1.0*l[26]+1.0*l[35]+1.0*l[44]+1.0*l[53]+1.0*l[62]
  out[57] = 1.0*l[28]+1.0*l[37]+1.0*l[46]
  out[58] = 1.0*l[29]+1.0*l[38]+1.0*l[47]
  out[59] = 1.0*l[30]
  out[60] = 1.0*l[31]
  out[61] = 1.0*l[32]
  out[62] = 1.0*l[33]+1.0*l[42]+1.0*l[51]
  out[63] = 1.0*l[34]+1.0*l[43]+1.0*l[52]
  out[64] = 1.0*l[39]
  out[65] = 1.0*l[40]
  out[66] = 1.0*l[41]
  out[67] = 1.0*l[48]
  out[68] = 1.0*l[49]
  out[69] = 1.0*l[50]
  out[70] = 1.0*l[55]+1.0*l[56]+1.0*l[64]+1.0*l[65]
  out[71] = 1.0*l[57]+1.0*l[58]+1.0*l[59]
  out[72] = 1.0*l[60]+1.0*l[61]+1.0*l[69]+1.0*l[70]
  out[73] = 1.0*l[66]+1.0*l[67]+1.0*l[68]
  out[74] = 1.0*l[74]+1.0*l[75]+1.0*l[76]+1.0*l[77]+1.0*l[78]

  # pack vert-pairs
  l = np.ravel(tile[:,:,3])
  out[75] = 1.0*l[ 2]+1.0*l[ 3]+1.0*l[ 4]+1.0*l[ 5]+1.0*l[6]
  out[76] = 1.0*l[10]+1.0*l[11]+1.0*l[19]+1.0*l[20]
  out[77] = 1.0*l[12]+1.0*l[13]+1.0*l[14]
  out[78] = 1.0*l[15]+1.0*l[16]+1.0*l[24]+1.0*l[25]
  out[79] = 1.0*l[18]+1.0*l[27]+1.0*l[36]+1.0*l[45]+1.0*l[54]
  out[80] = 1.0*l[21]+1.0*l[22]+1.0*l[23]
  out[81] = 1.0*l[26]+1.0*l[35]+1.0*l[44]+1.0*l[53]+1.0*l[62]
  out[82] = 1.0*l[28]+1.0*l[37]+1.0*l[46]
  out[83] = 1.0*l[29]+1.0*l[38]+1.0*l[47]
  out[84] = 1.0*l[30]
  out[85] = 1.0*l[31]
  out[86] = 1.0*l[32]
  out[87] = 1.0*l[33]+1.0*l[42]+1.0*l[51]
  out[88] = 1.0*l[34]+1.0*l[43]+1.0*l[52]
  out[89] = 1.0*l[39]
  out[90] = 1.0*l[40]
  out[91] = 1.0*l[41]
  out[92] = 1.0*l[48]
  out[93] = 1.0*l[49]
  out[94] = 1.0*l[50]
  out[95] = 1.0*l[55]+1.0*l[56]+1.0*l[64]+1.0*l[65]
  out[96] = 1.0*l[57]+1.0*l[58]+1.0*l[59]
  out[97] = 1.0*l[60]+1.0*l[61]+1.0*l[69]+1.0*l[70]
  out[98] = 1.0*l[66]+1.0*l[67]+1.0*l[68]
  out[99] = 1.0*l[74]+1.0*l[75]+1.0*l[76]+1.0*l[77]+1.0*l[78]
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
126 127 128

  return out

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
# pack from 9x9x4 to 104x1
# type 1, all the same
def pack_tile_type2(tile):

  out = np.empty(104)

  # pack diagm-pair (not tested)
  l = np.ravel(tile[:,:,0])
  out[ 0] = 1.0*l[ 0]+1.0*l[ 1]+1.0*l[ 2]+1.0*l[ 9]+1.0*l[10]+1.0*l[18]
  out[ 1] = 1.0*l[ 3]+1.0*l[11]+1.0*l[19]+1.0*l[27]
  out[ 2] = 1.0*l[ 4]+1.0*l[12]+1.0*l[20]+1.0*l[28]+1.0*l[36]
  out[ 3] = 1.0*l[ 5]+1.0*l[ 6]+1.0*l[14]
  out[ 4] = 1.0*l[ 7]+1.0*l[15]+1.0*l[23]
  out[ 5] = 1.0*l[ 8]+1.0*l[16]+1.0*l[24]
  out[ 6] = 1.0*l[13]+1.0*l[21]+1.0*l[29]+1.0*l[37]
  out[ 7] = 1.0*l[17]+1.0*l[25]+1.0*l[33]
  out[ 8] = 1.0*l[22]+1.0*l[30]+1.0*l[38]
  out[ 9] = 1.0*l[26]+1.0*l[34]+1.0*l[35]
  out[10] = 1.0*l[31]
  out[11] = 1.0*l[32]
  out[12] = 1.0*l[39]
  out[13] = 1.0*l[40]
  out[14] = 1.0*l[41]
  out[15] = 1.0*l[42]+1.0*l[50]+1.0*l[58]
  out[16] = 1.0*l[43]+1.0*l[51]+1.0*l[59]+1.0*l[67]
  out[17] = 1.0*l[44]+1.0*l[52]+1.0*l[60]+1.0*l[68]+1.0*l[76]
  out[18] = 1.0*l[45]+1.0*l[46]+1.0*l[54]
  out[19] = 1.0*l[47]+1.0*l[55]+1.0*l[63]
  out[20] = 1.0*l[48]
  out[21] = 1.0*l[49]
  out[22] = 1.0*l[53]+1.0*l[61]+1.0*l[69]+1.0*l[77]
  out[23] = 1.0*l[56]+1.0*l[64]+1.0*l[72]
  out[24] = 1.0*l[57]+1.0*l[65]+1.0*l[73]
  out[25] = 1.0*l[62]+1.0*l[70]+1.0*l[71]+1.0*l[78]+1.0*l[79]+1.0*l[80]
  out[26] = 1.0*l[66]+1.0*l[74]+1.0*l[75]

  # pack diago-pair (not tested)
  l = np.ravel(tile[:,:,1])
  out[27] = 1.0*l[ 0]+1.0*l[10]+1.0*l[20]
  out[28] = 1.0*l[ 1]+1.0*l[11]+1.0*l[21]
  out[29] = 1.0*l[ 2]+1.0*l[ 3]+1.0*l[12]
  out[30] = 1.0*l[ 4]+1.0*l[14]+1.0*l[24]+1.0*l[34]+1.0*l[44]
  out[31] = 1.0*l[ 5]+1.0*l[15]+1.0*l[25]+1.0*l[35]
  out[32] = 1.0*l[ 6]+1.0*l[ 7]+1.0*l[ 8]+1.0*l[16]+1.0*l[17]+1.0*l[26]
  out[33] = 1.0*l[ 9]+1.0*l[19]+1.0*l[29]
  out[34] = 1.0*l[13]+1.0*l[23]+1.0*l[43]
  out[35] = 1.0*l[18]+1.0*l[27]+1.0*l[28]
  out[36] = 1.0*l[22]+1.0*l[32]+1.0*l[42]
  out[37] = 1.0*l[30]
  out[38] = 1.0*l[31]
  out[39] = 1.0*l[36]+1.0*l[46]+1.0*l[56]+1.0*l[66]+1.0*l[76]
  out[40] = 1.0*l[37]+1.0*l[47]+1.0*l[57]+1.0*l[67]
  out[41] = 1.0*l[38]+1.0*l[48]+1.0*l[58]
  out[42] = 1.0*l[39]
  out[43] = 1.0*l[40]
  out[44] = 1.0*l[41]
  out[45] = 1.0*l[45]+1.0*l[55]+1.0*l[65]+1.0*l[75]
  out[46] = 1.0*l[49]
  out[47] = 1.0*l[50]
  out[48] = 1.0*l[51]+1.0*l[61]+1.0*l[71]
  out[49] = 1.0*l[52]+1.0*l[53]+1.0*l[62]
  out[50] = 1.0*l[54]+1.0*l[63]+1.0*l[64]+1.0*l[72]+1.0*l[73]+1.0*l[74]
  out[51] = 1.0*l[59]+1.0*l[69]+1.0*l[79]
  out[52] = 1.0*l[60]+1.0*l[70]+1.0*l[80]
  out[53] = 1.0*l[68]+1.0*l[77]+1.0*l[78]

  # pack hor-pairs
  l = np.ravel(tile[:,:,2])
  out[54] = 1.0*l[ 0]+1.0*l[ 1]+1.0*l[ 9]+1.0*l[10]+1.0*l[18]+1.0*l[27]+1.0*l[36]+1.0*l[45]+1.0*l[54]+1.0*l[63]+1.0*l[64]+1.0*l[72]+1.0*l[73]
  out[55] = 1.0*l[ 2]+1.0*l[11]+1.0*l[20]
  out[56] = 1.0*l[ 3]+1.0*l[12]+1.0*l[21]
  out[57] = 1.0*l[ 4]+1.0*l[13]+1.0*l[22]
  out[58] = 1.0*l[ 5]+1.0*l[14]+1.0*l[23]
  out[59] = 1.0*l[ 6]+1.0*l[15]+1.0*l[24]
  out[60] = 1.0*l[ 7]+1.0*l[ 8]+1.0*l[16]+1.0*l[17]+1.0*l[26]+1.0*l[35]+1.0*l[44]+1.0*l[53]+1.0*l[62]+1.0*l[70]+1.0*l[71]+1.0*l[79]+1.0*l[80]
  out[61] = 1.0*l[19]+1.0*l[28]+1.0*l[37]+1.0*l[46]+1.0*l[55]
  out[62] = 1.0*l[25]+1.0*l[34]+1.0*l[43]+1.0*l[52]+1.0*l[61]
  out[63] = 1.0*l[29]+1.0*l[38]+1.0*l[47]
  out[64] = 1.0*l[30]
  out[65] = 1.0*l[31]
  out[66] = 1.0*l[32]
  out[67] = 1.0*l[33]+1.0*l[42]+1.0*l[51]
  out[68] = 1.0*l[39]
  out[69] = 1.0*l[40]
  out[70] = 1.0*l[41]
  out[71] = 1.0*l[48]
  out[72] = 1.0*l[49]
  out[73] = 1.0*l[50]
  out[74] = 1.0*l[56]+1.0*l[65]+1.0*l[74]
  out[75] = 1.0*l[57]+1.0*l[66]+1.0*l[75]
  out[76] = 1.0*l[58]+1.0*l[67]+1.0*l[76]
  out[77] = 1.0*l[59]+1.0*l[68]+1.0*l[77]
  out[78] = 1.0*l[60]+1.0*l[69]+1.0*l[78]

  # pack vert-pairs
  l = np.ravel(tile[:,:,3])
  out[79]  = 1.0*l[ 0]+1.0*l[ 1]+1.0*l[ 2]+1.0*l[ 3]+1.0*l[ 4]+1.0*l[ 5]+1.0*l[ 6]+1.0*l[ 7]+1.0*l[ 8]+1.0*l[ 9]+1.0*l[10]+1.0*l[11]+1.0*l[16]+1.0*l[17]
  out[80]  = 1.0*l[11]+1.0*l[12]+1.0*l[13]+1.0*l[14]+1.0*l[15]
  out[81]  = 1.0*l[18]+1.0*l[19]+1.0*l[20]
  out[82]  = 1.0*l[21]+1.0*l[22]+1.0*l[23]
  out[83]  = 1.0*l[24]+1.0*l[25]+1.0*l[26]
  out[84]  = 1.0*l[27]+1.0*l[28]+1.0*l[29]
  out[85]  = 1.0*l[30]
  out[86]  = 1.0*l[31]
  out[87]  = 1.0*l[32]
  out[88]  = 1.0*l[33]+1.0*l[34]+1.0*l[35]
  out[89]  = 1.0*l[36]+1.0*l[37]+1.0*l[38]
  out[90]  = 1.0*l[39]
  out[91]  = 1.0*l[40]
  out[92]  = 1.0*l[41]
  out[93]  = 1.0*l[42]+1.0*l[43]+1.0*l[44]
  out[94]  = 1.0*l[45]+1.0*l[46]+1.0*l[47]
  out[95]  = 1.0*l[48]
  out[96]  = 1.0*l[49]
  out[97]  = 1.0*l[50]
  out[98]  = 1.0*l[51]+1.0*l[52]+1.0*l[53]
  out[99]  = 1.0*l[54]+1.0*l[55]+1.0*l[56]
  out[100] = 1.0*l[57]+1.0*l[58]+1.0*l[59]
  out[101] = 1.0*l[60]+1.0*l[61]+1.0*l[62]
  out[102] = 1.0*l[63]+1.0*l[64]+1.0*l[70]+1.0*l[71]+1.0*l[72]+1.0*l[73]+1.0*l[74]+1.0*l[75]+1.0*l[76]+1.0*l[77]+1.0*l[78]+1.0*l[79]+1.0*l[80]
  out[103] = 1.0*l[65]+1.0*l[66]+1.0*l[67]+1.0*l[68]+1.0*l[69]

  return out

253 254 255
# pack single
def pack_tile(tile):
  return pack_tile_type1(tile)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
256

257
# pack all tiles
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
258 259 260 261 262 263 264 265
def pack(tiles,ptype=1):

  if   ptype==1:
    pack_func = pack_tile_type1
  elif ptype==2:
    pack_func = pack_tile_type2

  output = np.array([[pack_func(tiles[i,j]) for j in range(tiles.shape[1])] for i in range(tiles.shape[0])])
266
  return output
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
267

268

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
# tiles are already packed
def get_tile_with_neighbors(tiles,i,j,radius):

  out = np.array([])
  # max
  Y,X = tiles.shape[0:2]
  #print(str(Y)+" "+str(X))

  for k in range(2*radius+1):

    y = i+k-radius
    if   y<0:
      y = 0
    elif y>(Y-1):
      y = Y-1

    for l in range(2*radius+1):

      x = j+l-radius
      if   x<0:
        x = 0
      elif x>(X-1):
        x = X-1

      out = np.append(out,tiles[y,x])

  return out