imagej_tiff.py 15.4 KB
Newer Older
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
1 2
#!/usr/bin/env python3

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
'''
/**
 * @file imagej_tiff.py
 * @brief open multi layer tiff files, display layers and parse meta data
 * @par <b>License</b>:
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
'''

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

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
27 28 29 30 31 32 33 34 35
'''
  Notes:
    - Pillow 5.1.0. Version 4.1.1 throws error (VelueError):
      ~$ (sudo) pip3 install Pillow --upgrade
      ~$ python3
      >>> import PIL
      >>> PIL.PILLOW_VERSION
      '5.1.0'
'''
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
36

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
37 38 39 40 41
from PIL import Image
import xml.etree.ElementTree as ET
import numpy as np
import matplotlib.pyplot as plt

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
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
import sys
import xml.dom.minidom as minidom

import time

#http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
class bcolors:
    HEADER = '\033[95m'
    OKBLUE = '\033[94m'
    OKGREEN = '\033[92m'
    WARNING = '\033[38;5;214m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'
    BOLDWHITE = '\033[1;37m'
    UNDERLINE = '\033[4m'

# reshape to tiles
def get_tile_images(image, width=8, height=8):
  _nrows, _ncols, depth = image.shape
  _size = image.size
  _strides = image.strides

  nrows, _m = divmod(_nrows, height)
  ncols, _n = divmod(_ncols, width)
  if _m != 0 or _n != 0:
    return None

  return np.lib.stride_tricks.as_strided(
    np.ravel(image),
    shape=(nrows, ncols, height, width, depth),
    strides=(height * _strides[0], width * _strides[1], *_strides),
    writeable=False
  )

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
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
# TiffFile has no len exception
#import imageio

#from libtiff import TIFF
'''
Description:
    Reads a tiff files with multiple layers that were saved by imagej
Methods:
    .getstack(items=[])
        returns np.array, layers are stacked along depth - think of RGB channels
        @items - if empty = all, if not - items[i] - can be layer index or layer's label name
    .channel(index)
        returns np.array of a single layer
    .show_images(items=[])
        @items - if empty = all, if not - items[i] - can be layer index or layer's label name
    .show_image(index)
Examples:
#1

'''
class imagej_tiff:

  # imagej stores labels lengths in this tag
  __TIFF_TAG_LABELS_LENGTHS = 50838
  # imagej stores labels conents in this tag
  __TIFF_TAG_LABELS_STRINGS = 50839

  # init
105
  def __init__(self,filename, layers = None, tile_list = None):
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
106 107 108 109 110 111 112 113 114
    # file name
    self.fname = filename
    tif = Image.open(filename)
    # total number of layers in tiff
    self.nimages = tif.n_frames
    # labels array
    self.labels = []
    # infos will contain xml data Elphel stores in some of tiff files
    self.infos = []
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
115 116 117 118 119 120
    # dictionary from decoded infos[0] xml data
    self.props = {}

    # bits per sample, type int
    self.bpp = tif.tag[258][0]

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
121 122
    self.__split_labels(tif.n_frames,tif.tag)
    self.__parse_info()
123 124 125 126
    try:
        self.nan_bug = self.props['VERSION']== '1.0' # data between min and max is mapped to 0..254 instead of  1.255
    except:
        self.nan_bug = False # other files, not ML ones
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
127 128 129
    # image layers stacked along depth - (think RGB)
    self.image = []

130
    if layers is None:
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
131
    # fill self.image
132 133 134 135 136 137 138 139 140 141 142 143 144 145
        for i in range(self.nimages):
          tif.seek(i)
          a = np.array(tif)
          a = np.reshape(a,(a.shape[0],a.shape[1],1))
    
          #a = a[:,:,np.newaxis]
    
          # scale for 8-bits
          # exclude layer named 'other'
          if self.bpp==8:
            _min = self.data_min
            _max = self.data_max
            _MIN = 1
            _MAX = 255
146 147 148 149 150 151
            if (self.nan_bug):
                _MIN = 0
                _MAX = 254
            else:
                if self.labels[i]!='other':
                    a[a==0]=np.nan
152 153
            a = a.astype(float)
            if self.labels[i]!='other':
154
#              a[a==0]=np.nan
155 156 157 158 159 160 161 162
              a = (_max-_min)*(a-_MIN)/(_MAX-_MIN)+_min
          # init
          if i==0:
            self.image = a
          # stack along depth (think of RGB channels)
          else:
            self.image = np.append(self.image,a,axis=2)
    else:
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
        if tile_list is None:
            indx = 0
            for layer in layers:
                tif.seek(self.labels.index(layer)) 
                a = np.array(tif)
                if not indx:
                    self.image = np.empty((a.shape[0],a.shape[1],len(layers)),a.dtype)
                self.image[...,indx] = a
                indx += 1
        else:
            other_label = "other"
#            print(tile_list)
            num_tiles =  len(tile_list)
            num_layers = len(layers)
            tiles_corr = np.empty((num_tiles,num_layers,self.tileH*self.tileW),dtype=float)
#            tiles_other=np.empty((num_tiles,3),dtype=float)
            tiles_other=self.gettilesvalues(
                     tif = tif,
                     tile_list=tile_list,
                     label=other_label)
            for nl,label in enumerate(layers):
                tif.seek(self.labels.index(label))
                layer = np.array(tif) # 8 or 32 bits
                tilesX = layer.shape[1]//self.tileW
                for nt,tl in enumerate(tile_list):
                    ty = tl // tilesX
                    tx = tl % tilesX
#                    tiles_corr[nt,nl] = np.ravel(layer[self.tileH*ty:self.tileH*(ty+1),self.tileW*tx:self.tileW*(tx+1)])
                    a = np.ravel(layer[self.tileH*ty:self.tileH*(ty+1),self.tileW*tx:self.tileW*(tx+1)])
                    #convert from int8
                    if self.bpp==8:
                        a = a.astype(float)
                        if np.isnan(tiles_other[nt][0]):
                            # print("Skipping NaN tile ",tl)
                            a[...] = np.nan
                        else:
                            _min = self.data_min
                            _max = self.data_max
                            _MIN = 1
                            _MAX = 255
                            if (self.nan_bug):
                                _MIN = 0
                                _MAX = 254
                            else:    
                                a[a==0] = np.nan
                            a = (_max-_min)*(a-_MIN)/(_MAX-_MIN)+_min
                    tiles_corr[nt,nl] = a    
                    pass
                pass
            self.corr2d =           tiles_corr
            self.target_disparity = tiles_other[...,0]
            self.gt_ds =            tiles_other[...,1:3]
            pass
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
216 217 218 219

    # init done, close the image
    tif.close()

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241
  # label == tiff layer name
  def getvalues(self,label=""):
    l = self.getstack([label],shape_as_tiles=True)
    res = np.empty((l.shape[0],l.shape[1],3))

    for i in range(res.shape[0]):
      for j in range(res.shape[1]):
        # 9x9 -> 81x1
        m = np.ravel(l[i,j])
        if self.bpp==32:
          res[i,j,0] = m[0]
          res[i,j,1] = m[2]
          res[i,j,2] = m[4]
        elif self.bpp==8:
          res[i,j,0] = ((m[0]-128)*256+m[1])/128
          res[i,j,1] = ((m[2]-128)*256+m[3])/128
          res[i,j,2] = (m[4]*256+m[5])/65536.0
        else:
          res[i,j,0] = np.nan
          res[i,j,1] = np.nan
          res[i,j,2] = np.nan

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
242 243 244 245 246 247 248
    # NaNize
    a = res[:,:,0]
    a[a==-256] = np.nan
    b = res[:,:,1]
    b[b==-256] = np.nan
    c = res[:,:,2]
    c[c==0] = np.nan
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282
    return res

  # 3 values per tile: target disparity, GT disparity, GT confidence
  def gettilesvalues(self,
                     tif,
                     tile_list,
                     label=""):
    res = np.empty((len(tile_list),3),dtype=float)
    tif.seek(self.labels.index(label))
    layer = np.array(tif) # 8 or 32 bits
    tilesX = layer.shape[1]//self.tileW
    for i,tl in enumerate(tile_list):
        ty = tl // tilesX
        tx = tl % tilesX
        m = np.ravel(layer[self.tileH*ty:self.tileH*(ty+1),self.tileW*tx:self.tileW*(tx+1)])
        if self.bpp==32:
          res[i,0] = m[0]
          res[i,1] = m[2]
          res[i,2] = m[4]
        elif self.bpp==8:
          res[i,0] = ((m[0]-128)*256+m[1])/128
          res[i,1] = ((m[2]-128)*256+m[3])/128
          res[i,2] = (m[4]*256+m[5])/65536.0
        else:
          res[i,0] = np.nan
          res[i,1] = np.nan
          res[i,2] = np.nan
    # NaNize
    a = res[...,0]
    a[a==-256] = np.nan
    b = res[...,1]
    b[b==-256] = np.nan
    c = res[...,2]
    c[c==0] = np.nan
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
283

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
284 285 286
    return res


287 288


Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
289 290
  # get ordered stack of images by provided items
  # by index or label name
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
291
  def getstack(self,items=[],shape_as_tiles=False):
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
292 293
    a = ()
    if len(items)==0:
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
294
      b = self.image
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
295 296 297 298 299 300 301 302
    else:
      for i in items:
        if type(i)==int:
          a += (self.image[:,:,i],)
        elif type(i)==str:
          j = self.labels.index(i)
          a += (self.image[:,:,j],)
      # stack along depth
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
303 304 305 306
      b = np.stack(a,axis=2)

    if shape_as_tiles:
      b = get_tile_images(b,self.tileW,self.tileH)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
307

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
308
    return b
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355

  # get np.array of a channel
  # * do not handle out of bounds
  def channel(self,index):
      return self.image[:,:,index]


  # display images by index or label
  def show_images(self,items=[]):

    # show listed only
    if len(items)>0:
      for i in items:
        if type(i)==int:
          self.show_image(i)
        elif type(i)==str:
          j = self.labels.index(i)
          self.show_image(j)
    # show all
    else:
      for i in range(self.nimages):
        self.show_image(i)


  # display single image
  def show_image(self,index):

    # display using matplotlib

    t = self.image[:,:,index]
    mytitle = "("+str(index+1)+" of "+str(self.nimages)+") "+self.labels[index]
    fig = plt.figure()
    fig.canvas.set_window_title(self.fname+": "+mytitle)
    fig.suptitle(mytitle)
    #plt.imshow(t,cmap=plt.get_cmap('gray'))
    plt.imshow(t)
    plt.colorbar()

    # display using Pillow - need to scale

    # remove NaNs - no need
    #t[np.isnan(t)]=np.nanmin(t)
    # scale to [min/max*255:255] range
    #t = (1-(t-np.nanmax(t))/(t-np.nanmin(t)))*255
    #tmp_im = Image.fromarray(t)
    #tmp_im.show()

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
356
  # puts etrees in infoss
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
357 358 359 360 361 362 363 364
  def __parse_info(self):

    infos = []
    for info in self.infos:
      infos.append(ET.fromstring(info))

    self.infos = infos

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
365 366 367 368
    # specifics
    # properties dictionary
    pd = {}

369 370 371 372 373 374 375 376 377 378 379 380
    if infos:
        for child in infos[0]:
          #print(child.tag+"::::::"+child.text)
          pd[child.tag] = child.text
    
        self.props = pd
    
        # tiles are squares
        self.tileW = int(self.props['tileWidth'])
        self.tileH = int(self.props['tileWidth'])
        self.data_min = float(self.props['data_min'])
        self.data_max = float(self.props['data_max'])
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407

  # makes arrays of labels (strings) and unparsed xml infos
  def __split_labels(self,n,tag):

    # list
    tag_lens = tag[self.__TIFF_TAG_LABELS_LENGTHS]
    # string
    tag_labels = tag[self.__TIFF_TAG_LABELS_STRINGS].decode()
    # remove 1st element: it's something like IJIJlabl..
    tag_labels = tag_labels[tag_lens[0]:]
    tag_lens = tag_lens[1:]

    # the last ones are images labels
    # normally the difference is expected to be 0 or 1
    skip = len(tag_lens) - n

    self.labels = []
    self.infos = []
    for l in tag_lens:
      string = tag_labels[0:l].replace('\x00','')
      if skip==0:
        self.labels.append(string)
      else:
        self.infos.append(string)
        skip -= 1
      tag_labels = tag_labels[l:]

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
408

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
409 410
#MAIN
if __name__ == "__main__":
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
411 412 413 414

  try:
    fname = sys.argv[1]
  except IndexError:
415 416 417
    fname = "/mnt/dde6f983-d149-435e-b4a2-88749245cc6c/home/eyesis/x3d_data/data_sets/train/1527182807_896892/v02/ml/1527182807_896892-ML_DATA-08B-O-FZ0.05-OFFS0.40000.tiff"
#    fname = "1521849031_093189-ML_DATA-32B-O-OFFS1.0.tiff"
#    fname = "1521849031_093189-ML_DATA-08B-O-OFFS1.0.tiff"
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
418

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
419
  #fname = "1521849031_093189-DISP_MAP-D0.0-46.tif"
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
420 421 422 423
  #fname = "1526905735_662795-ML_DATA-08B-AIOTD-OFFS2.0.tiff"
  #fname = "test.tiff"

  print(bcolors.BOLDWHITE+"time: "+str(time.time())+bcolors.ENDC)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
424 425 426

  ijt = imagej_tiff(fname)

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
427 428
  print(bcolors.BOLDWHITE+"time: "+str(time.time())+bcolors.ENDC)

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
429 430
  print("TIFF stack labels: "+str(ijt.labels))
  #print(ijt.infos)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
431 432 433 434 435

  rough_string = ET.tostring(ijt.infos[0], "utf-8")
  reparsed = minidom.parseString(rough_string)
  print(reparsed.toprettyxml(indent="\t"))

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
436
  #print(ijt.props)
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
437 438 439 440 441 442

  # needed properties:
  print("Tiles shape: "+str(ijt.tileW)+"x"+str(ijt.tileH))
  print("Data min: "+str(ijt.data_min))
  print("Data max: "+str(ijt.data_max))

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
443 444
  print(ijt.image.shape)

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
445 446 447 448 449 450 451 452 453 454 455 456 457 458
  # layer order: ['diagm-pair', 'diago-pair', 'hor-pairs', 'vert-pairs', 'other']
  # now split this into tiles:

  #tiles = get_tile_images(ijt.image,ijt.tileW,ijt.tileH)
  #print(tiles.shape)

  tiles = ijt.getstack(['diagm-pair','diago-pair','hor-pairs','vert-pairs'],shape_as_tiles=True)
  print("Stack of images shape: "+str(tiles.shape))

  print(bcolors.BOLDWHITE+"time: "+str(time.time())+bcolors.ENDC)
  # provide layer name
  values = ijt.getvalues(label='other')
  print("Stack of values shape: "+str(values.shape))

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
459 460 461 462 463 464 465 466
  # each tile's disparity:

  fig = plt.figure()
  fig.suptitle("Estimated Disparity")
  plt.imshow(values[:,:,0])
  plt.colorbar()

  fig = plt.figure()
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
467
  fig.suptitle("Esitmated+Residual disparity")
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
468 469 470 471 472 473 474 475
  plt.imshow(values[:,:,1])
  plt.colorbar()

  fig = plt.figure()
  fig.suptitle("Residual disparity confidence")
  plt.imshow(values[:,:,2])
  plt.colorbar()

Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
  print(bcolors.BOLDWHITE+"time: "+str(time.time())+bcolors.ENDC)
  #print(values)

  #print(value_tiles[131,162].flatten())
  #print(np.ravel(value_tiles[131,162]))

  #values = np.empty((vt.shape[0],vt.shape[1],3))

  #for i in range(values.shape[0]):
  #  for j in range(values.shape[1]):
  #    values[i,j,0] = get_v1()


  #print(tiles[121,160,:,:,0].shape)
  #_nrows = int(ijt.image.shape[0] / ijt.tileH)
  #_ncols = int(ijt.image.shape[1] / ijt.tileW)
  #_nrows = 32
  #_ncols = 32
  #print(str(_nrows)+" "+str(_ncols))
  #fig, ax = plt.subplots(nrows=_nrows, ncols=_ncols)
  #for i in range(_nrows):
  #  for j in range(_ncols):
  #    ax[i,j].imshow(tiles[i+100,j,:,:,0])
  #    ax[i,j].set_axis_off()

  #for i in range(5):
  #  fig = plt.figure()
  #  plt.imshow(tiles[121,160,:,:,i])
  #  plt.colorbar()

  #ijt.show_images(['other'])
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
507 508

  #ijt.show_images([0,3])
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
509
  #ijt.show_images(['X-corr','Y-corr'])
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
510 511
  #ijt.show_images(['R-vign',3])

512
  ijt.show_images()
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
513
  plt.show()
Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
514 515 516 517




Oleg Dzhimiev's avatar
Oleg Dzhimiev committed
518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556

  # Examples

  # 1: get default stack of images
  #a = ijt.getstack()
  #print(a.shape)

  # 2: get defined ordered stack of images by tiff image index or by label name
  #a = ijt.getstack([1,2,'X-corr'])
  #print(a.shape)

  # 3: will throw an error if there's no such label
  #a = ijt.getstack([1,2,'Unknown'])
  #print(a.shape)

  # 4: will throw an error if index is out of bounds
  #a = ijt.getstack([1,2,'X-corr'])
  #print(a.shape)

  # 5: dev excercise
  #a = np.array([[1,2],[3,4]])
  #b = np.array([[5,6],[7,8]])
  #c = np.array([[10,11],[12,13]])

  #print("test1:")
  #ka = (a,b,c)
  #d = np.stack(ka,axis=2)

  #print(d)

  #print("test2:")
  #e = np.stack((d[:,:,1],d[:,:,0]),axis=2)
  #print(e)