Loading imagej_tiff.py +423 −433 Original line number Original line Diff line number Diff line Loading @@ -80,7 +80,7 @@ def get_tile_images(image, width=8, height=8): #from libtiff import TIFF #from libtiff import TIFF ''' ''' Description: Description: Reads a tiff files with multiple layers that were saved by imagej Reads TIFF files with multiple layers that were saved by imagej Methods: Methods: .getstack(items=[]) .getstack(items=[]) returns np.array, layers are stacked along depth - think of RGB channels returns np.array, layers are stacked along depth - think of RGB channels Loading @@ -95,12 +95,10 @@ Examples: ''' ''' class imagej_tiff: class imagej_tiff: # imagej stores labels lengths in this tag # imagej stores labels lengths in this tag __TIFF_TAG_LABELS_LENGTHS = 50838 __TIFF_TAG_LABELS_LENGTHS = 50838 # imagej stores labels conents in this tag # imagej stores labels conents in this tag __TIFF_TAG_LABELS_STRINGS = 50839 __TIFF_TAG_LABELS_STRINGS = 50839 # init # init def __init__(self,filename, layers = None, tile_list = None): def __init__(self,filename, layers = None, tile_list = None): # file name # file name Loading Loading @@ -133,10 +131,7 @@ class imagej_tiff: tif.seek(i) tif.seek(i) a = np.array(tif) a = np.array(tif) a = np.reshape(a,(a.shape[0],a.shape[1],1)) a = np.reshape(a,(a.shape[0],a.shape[1],1)) #a = a[:,:,np.newaxis] #a = a[:,:,np.newaxis] # scale for 8-bits # exclude layer named 'other' # exclude layer named 'other' if self.bpp==8: if self.bpp==8: _min = self.data_min _min = self.data_min Loading @@ -151,9 +146,7 @@ class imagej_tiff: a[a==0]=np.nan a[a==0]=np.nan a = a.astype(float) a = a.astype(float) if self.labels[i]!='other': if self.labels[i]!='other': # a[a==0]=np.nan a = (_max-_min)*(a-_MIN)/(_MAX-_MIN)+_min a = (_max-_min)*(a-_MIN)/(_MAX-_MIN)+_min # init if i==0: if i==0: self.image = a self.image = a # stack along depth (think of RGB channels) # stack along depth (think of RGB channels) Loading Loading @@ -213,10 +206,15 @@ class imagej_tiff: self.target_disparity = tiles_other[...,0] self.target_disparity = tiles_other[...,0] self.gt_ds = tiles_other[...,1:3] self.gt_ds = tiles_other[...,1:3] pass pass # init done, close the image # init done, close the image tif.close() if (self.props['VERSION']== 2.0): # self.tileH = self.image.shape[0]//self.props['tileStepY'] # self.tileW = self.image.shape[1]//self.props['tileStepX'] self.tileH = self.props['tileStepY'] self.tileW = self.props['tileStepX'] pass tif.close() # label == tiff layer name # label == tiff layer name def getvalues(self,label=""): def getvalues(self,label=""): l = self.getstack([label],shape_as_tiles=True) l = self.getstack([label],shape_as_tiles=True) Loading Loading @@ -283,11 +281,8 @@ class imagej_tiff: return res return res # get ordered stack of images by provided items # get ordered stack of images by provided items # by index or label name # by index or label name. Divides into [self.tileH][self.tileW] tiles def getstack(self,items=[],shape_as_tiles=False): def getstack(self,items=[],shape_as_tiles=False): a = () a = () if len(items)==0: if len(items)==0: Loading @@ -301,17 +296,30 @@ class imagej_tiff: a += (self.image[:,:,j],) a += (self.image[:,:,j],) # stack along depth # stack along depth b = np.stack(a,axis=2) b = np.stack(a,axis=2) if shape_as_tiles: if shape_as_tiles: b = get_tile_images(b,self.tileW,self.tileH) b = get_tile_images(b,self.tileW,self.tileH) return b return b def trimStack (self, stack, radius = 0): if (radius == 0): radius=self.props['corrRadius'] corr_side = 2*radius+1 return stack[:,:,:,:corr_side,:corr_side] # get np.array of a channel # get np.array of a channel # * do not handle out of bounds # * do not handle out of bounds def channel(self,index): def channel(self,index): return self.image[:,:,index] return self.image[:,:,index] def getCorrsMeta(self,items=[]): stack0 = self.getstack(items,shape_as_tiles=True) stack = np.moveaxis(stack0, 4, 0) # slices - first index radius=self.props['corrRadius'] num_meta=self.props['numMeta'] corr_side = 2*radius+1 corr_tiles = stack[:,:,:,:corr_side,:corr_side] meta = stack[:,:,:,-1,:num_meta] return corr_tiles, meta/self.props['tileMetaScale'] # display images by index or label # display images by index or label def show_images(self,items=[]): def show_images(self,items=[]): Loading @@ -332,7 +340,6 @@ class imagej_tiff: # display single image # display single image def show_image(self,index): def show_image(self,index): # display using matplotlib # display using matplotlib t = self.image[:,:,index] t = self.image[:,:,index] Loading @@ -359,7 +366,6 @@ class imagej_tiff: infos = [] infos = [] for info in self.infos: for info in self.infos: infos.append(ET.fromstring(info)) infos.append(ET.fromstring(info)) self.infos = infos self.infos = infos # specifics # specifics Loading @@ -372,16 +378,46 @@ class imagej_tiff: pd[child.tag] = child.text pd[child.tag] = child.text self.props = pd self.props = pd file_version = float(self.props['VERSION']) # tiles are squares if (file_version < 2.0): # tiles are squares (older version self.tileW = int(self.props['tileWidth']) self.tileW = int(self.props['tileWidth']) self.tileH = int(self.props['tileWidth']) self.tileH = int(self.props['tileWidth']) self.data_min = float(self.props['data_min']) self.data_min = float(self.props['data_min']) self.data_max = float(self.props['data_max']) self.data_max = float(self.props['data_max']) else: floats=['dispOffsetLow','tileMetaScale','disparity_low','dispOffset', 'fatZero','disparity_pwr','VERSION','dispOffsetHigh', 'disparity_high'] ints = ['metaGTConfidence','tileMetaSlice','indexReference','metaLastDiff', 'metaGTDisparity','metaFracValid', 'numScenes','metaTargetDisparity', 'disparity_steps','tileStepX', 'tileStepY', "corrRadius","numMeta"] bools=['randomize_offsets'] for key in pd: val = pd[key] if key in bools: if (val == '1') or (val == 'true') or (val == 'True'): pd[key] = 1 else: pd[key] = 0 pass elif key in ints: pd[key] = int(pd[key]) elif key in floats: pd[key] = float(pd[key]) try: pd['corrRadius'] = pd['corrRadius'] # not yet exists except: pd['corrRadius'] = 7 try: pd['numMeta'] = pd['numMeta'] # not yet exists except: pd['numMeta'] = 6 pass # makes arrays of labels (strings) and unparsed xml infos # makes arrays of labels (strings) and unparsed xml infos def __split_labels(self,n,tag): def __split_labels(self,n,tag): # list # list tag_lens = tag[self.__TIFF_TAG_LABELS_LENGTHS] tag_lens = tag[self.__TIFF_TAG_LABELS_LENGTHS] # string # string Loading @@ -405,21 +441,13 @@ class imagej_tiff: skip -= 1 skip -= 1 tag_labels = tag_labels[l:] tag_labels = tag_labels[l:] #MAIN #MAIN if __name__ == "__main__": if __name__ == "__main__": try: try: fname = sys.argv[1] fname = sys.argv[1] except IndexError: except IndexError: 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 = "/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 = "/home/elphel/lwir16-proc/proc1/models/1626032208_613623/v01/ml32/1626032208_613623-ML-AUX-RND-DOFFS-5.000.tiff" # fname = "1521849031_093189-ML_DATA-08B-O-OFFS1.0.tiff" #fname = "1521849031_093189-DISP_MAP-D0.0-46.tif" #fname = "1526905735_662795-ML_DATA-08B-AIOTD-OFFS2.0.tiff" #fname = "test.tiff" print(bcolors.BOLDWHITE+"time: "+str(time.time())+bcolors.ENDC) print(bcolors.BOLDWHITE+"time: "+str(time.time())+bcolors.ENDC) ijt = imagej_tiff(fname) ijt = imagej_tiff(fname) Loading @@ -432,32 +460,28 @@ if __name__ == "__main__": rough_string = ET.tostring(ijt.infos[0], "utf-8") rough_string = ET.tostring(ijt.infos[0], "utf-8") reparsed = minidom.parseString(rough_string) reparsed = minidom.parseString(rough_string) print(reparsed.toprettyxml(indent="\t")) print(reparsed.toprettyxml(indent="\t")) #print(ijt.props) # needed properties: # needed properties: print("Tiles shape: "+str(ijt.tileW)+"x"+str(ijt.tileH)) print("Tiles shape: "+str(ijt.tileW)+"x"+str(ijt.tileH)) try: print("Data min: "+str(ijt.data_min)) print("Data min: "+str(ijt.data_min)) print("Data max: "+str(ijt.data_max)) print("Data max: "+str(ijt.data_max)) except: print("Data min/max are not provided") print(ijt.image.shape) print(ijt.image.shape) # tiles,tiles_meta = ijt.getCorrsMeta(['0-1','1-2','2-3','3-4']) tiles,tiles_meta = ijt.getCorrsMeta([]) print("Corr stack shape: "+str(tiles.shape)) print("Meta stack shape: "+str(tiles_meta.shape)) # layer order: ['diagm-pair', 'diago-pair', 'hor-pairs', 'vert-pairs', 'other'] exit (0) # now split this into tiles: # # each tile's disparity: #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 # provide layer name values = ijt.getvalues(label='other') values = ijt.getvalues(label='other') print("Stack of values shape: "+str(values.shape)) print("Stack of values shape: "+str(values.shape)) # each tile's disparity: fig = plt.figure() fig = plt.figure() fig.suptitle("Estimated Disparity") fig.suptitle("Estimated Disparity") plt.imshow(values[:,:,0]) plt.imshow(values[:,:,0]) Loading @@ -474,40 +498,6 @@ if __name__ == "__main__": plt.colorbar() plt.colorbar() print(bcolors.BOLDWHITE+"time: "+str(time.time())+bcolors.ENDC) 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']) #ijt.show_images([0,3]) #ijt.show_images(['X-corr','Y-corr']) #ijt.show_images(['R-vign',3]) ijt.show_images() ijt.show_images() plt.show() plt.show() Loading Loading
imagej_tiff.py +423 −433 Original line number Original line Diff line number Diff line Loading @@ -80,7 +80,7 @@ def get_tile_images(image, width=8, height=8): #from libtiff import TIFF #from libtiff import TIFF ''' ''' Description: Description: Reads a tiff files with multiple layers that were saved by imagej Reads TIFF files with multiple layers that were saved by imagej Methods: Methods: .getstack(items=[]) .getstack(items=[]) returns np.array, layers are stacked along depth - think of RGB channels returns np.array, layers are stacked along depth - think of RGB channels Loading @@ -95,12 +95,10 @@ Examples: ''' ''' class imagej_tiff: class imagej_tiff: # imagej stores labels lengths in this tag # imagej stores labels lengths in this tag __TIFF_TAG_LABELS_LENGTHS = 50838 __TIFF_TAG_LABELS_LENGTHS = 50838 # imagej stores labels conents in this tag # imagej stores labels conents in this tag __TIFF_TAG_LABELS_STRINGS = 50839 __TIFF_TAG_LABELS_STRINGS = 50839 # init # init def __init__(self,filename, layers = None, tile_list = None): def __init__(self,filename, layers = None, tile_list = None): # file name # file name Loading Loading @@ -133,10 +131,7 @@ class imagej_tiff: tif.seek(i) tif.seek(i) a = np.array(tif) a = np.array(tif) a = np.reshape(a,(a.shape[0],a.shape[1],1)) a = np.reshape(a,(a.shape[0],a.shape[1],1)) #a = a[:,:,np.newaxis] #a = a[:,:,np.newaxis] # scale for 8-bits # exclude layer named 'other' # exclude layer named 'other' if self.bpp==8: if self.bpp==8: _min = self.data_min _min = self.data_min Loading @@ -151,9 +146,7 @@ class imagej_tiff: a[a==0]=np.nan a[a==0]=np.nan a = a.astype(float) a = a.astype(float) if self.labels[i]!='other': if self.labels[i]!='other': # a[a==0]=np.nan a = (_max-_min)*(a-_MIN)/(_MAX-_MIN)+_min a = (_max-_min)*(a-_MIN)/(_MAX-_MIN)+_min # init if i==0: if i==0: self.image = a self.image = a # stack along depth (think of RGB channels) # stack along depth (think of RGB channels) Loading Loading @@ -213,10 +206,15 @@ class imagej_tiff: self.target_disparity = tiles_other[...,0] self.target_disparity = tiles_other[...,0] self.gt_ds = tiles_other[...,1:3] self.gt_ds = tiles_other[...,1:3] pass pass # init done, close the image # init done, close the image tif.close() if (self.props['VERSION']== 2.0): # self.tileH = self.image.shape[0]//self.props['tileStepY'] # self.tileW = self.image.shape[1]//self.props['tileStepX'] self.tileH = self.props['tileStepY'] self.tileW = self.props['tileStepX'] pass tif.close() # label == tiff layer name # label == tiff layer name def getvalues(self,label=""): def getvalues(self,label=""): l = self.getstack([label],shape_as_tiles=True) l = self.getstack([label],shape_as_tiles=True) Loading Loading @@ -283,11 +281,8 @@ class imagej_tiff: return res return res # get ordered stack of images by provided items # get ordered stack of images by provided items # by index or label name # by index or label name. Divides into [self.tileH][self.tileW] tiles def getstack(self,items=[],shape_as_tiles=False): def getstack(self,items=[],shape_as_tiles=False): a = () a = () if len(items)==0: if len(items)==0: Loading @@ -301,17 +296,30 @@ class imagej_tiff: a += (self.image[:,:,j],) a += (self.image[:,:,j],) # stack along depth # stack along depth b = np.stack(a,axis=2) b = np.stack(a,axis=2) if shape_as_tiles: if shape_as_tiles: b = get_tile_images(b,self.tileW,self.tileH) b = get_tile_images(b,self.tileW,self.tileH) return b return b def trimStack (self, stack, radius = 0): if (radius == 0): radius=self.props['corrRadius'] corr_side = 2*radius+1 return stack[:,:,:,:corr_side,:corr_side] # get np.array of a channel # get np.array of a channel # * do not handle out of bounds # * do not handle out of bounds def channel(self,index): def channel(self,index): return self.image[:,:,index] return self.image[:,:,index] def getCorrsMeta(self,items=[]): stack0 = self.getstack(items,shape_as_tiles=True) stack = np.moveaxis(stack0, 4, 0) # slices - first index radius=self.props['corrRadius'] num_meta=self.props['numMeta'] corr_side = 2*radius+1 corr_tiles = stack[:,:,:,:corr_side,:corr_side] meta = stack[:,:,:,-1,:num_meta] return corr_tiles, meta/self.props['tileMetaScale'] # display images by index or label # display images by index or label def show_images(self,items=[]): def show_images(self,items=[]): Loading @@ -332,7 +340,6 @@ class imagej_tiff: # display single image # display single image def show_image(self,index): def show_image(self,index): # display using matplotlib # display using matplotlib t = self.image[:,:,index] t = self.image[:,:,index] Loading @@ -359,7 +366,6 @@ class imagej_tiff: infos = [] infos = [] for info in self.infos: for info in self.infos: infos.append(ET.fromstring(info)) infos.append(ET.fromstring(info)) self.infos = infos self.infos = infos # specifics # specifics Loading @@ -372,16 +378,46 @@ class imagej_tiff: pd[child.tag] = child.text pd[child.tag] = child.text self.props = pd self.props = pd file_version = float(self.props['VERSION']) # tiles are squares if (file_version < 2.0): # tiles are squares (older version self.tileW = int(self.props['tileWidth']) self.tileW = int(self.props['tileWidth']) self.tileH = int(self.props['tileWidth']) self.tileH = int(self.props['tileWidth']) self.data_min = float(self.props['data_min']) self.data_min = float(self.props['data_min']) self.data_max = float(self.props['data_max']) self.data_max = float(self.props['data_max']) else: floats=['dispOffsetLow','tileMetaScale','disparity_low','dispOffset', 'fatZero','disparity_pwr','VERSION','dispOffsetHigh', 'disparity_high'] ints = ['metaGTConfidence','tileMetaSlice','indexReference','metaLastDiff', 'metaGTDisparity','metaFracValid', 'numScenes','metaTargetDisparity', 'disparity_steps','tileStepX', 'tileStepY', "corrRadius","numMeta"] bools=['randomize_offsets'] for key in pd: val = pd[key] if key in bools: if (val == '1') or (val == 'true') or (val == 'True'): pd[key] = 1 else: pd[key] = 0 pass elif key in ints: pd[key] = int(pd[key]) elif key in floats: pd[key] = float(pd[key]) try: pd['corrRadius'] = pd['corrRadius'] # not yet exists except: pd['corrRadius'] = 7 try: pd['numMeta'] = pd['numMeta'] # not yet exists except: pd['numMeta'] = 6 pass # makes arrays of labels (strings) and unparsed xml infos # makes arrays of labels (strings) and unparsed xml infos def __split_labels(self,n,tag): def __split_labels(self,n,tag): # list # list tag_lens = tag[self.__TIFF_TAG_LABELS_LENGTHS] tag_lens = tag[self.__TIFF_TAG_LABELS_LENGTHS] # string # string Loading @@ -405,21 +441,13 @@ class imagej_tiff: skip -= 1 skip -= 1 tag_labels = tag_labels[l:] tag_labels = tag_labels[l:] #MAIN #MAIN if __name__ == "__main__": if __name__ == "__main__": try: try: fname = sys.argv[1] fname = sys.argv[1] except IndexError: except IndexError: 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 = "/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 = "/home/elphel/lwir16-proc/proc1/models/1626032208_613623/v01/ml32/1626032208_613623-ML-AUX-RND-DOFFS-5.000.tiff" # fname = "1521849031_093189-ML_DATA-08B-O-OFFS1.0.tiff" #fname = "1521849031_093189-DISP_MAP-D0.0-46.tif" #fname = "1526905735_662795-ML_DATA-08B-AIOTD-OFFS2.0.tiff" #fname = "test.tiff" print(bcolors.BOLDWHITE+"time: "+str(time.time())+bcolors.ENDC) print(bcolors.BOLDWHITE+"time: "+str(time.time())+bcolors.ENDC) ijt = imagej_tiff(fname) ijt = imagej_tiff(fname) Loading @@ -432,32 +460,28 @@ if __name__ == "__main__": rough_string = ET.tostring(ijt.infos[0], "utf-8") rough_string = ET.tostring(ijt.infos[0], "utf-8") reparsed = minidom.parseString(rough_string) reparsed = minidom.parseString(rough_string) print(reparsed.toprettyxml(indent="\t")) print(reparsed.toprettyxml(indent="\t")) #print(ijt.props) # needed properties: # needed properties: print("Tiles shape: "+str(ijt.tileW)+"x"+str(ijt.tileH)) print("Tiles shape: "+str(ijt.tileW)+"x"+str(ijt.tileH)) try: print("Data min: "+str(ijt.data_min)) print("Data min: "+str(ijt.data_min)) print("Data max: "+str(ijt.data_max)) print("Data max: "+str(ijt.data_max)) except: print("Data min/max are not provided") print(ijt.image.shape) print(ijt.image.shape) # tiles,tiles_meta = ijt.getCorrsMeta(['0-1','1-2','2-3','3-4']) tiles,tiles_meta = ijt.getCorrsMeta([]) print("Corr stack shape: "+str(tiles.shape)) print("Meta stack shape: "+str(tiles_meta.shape)) # layer order: ['diagm-pair', 'diago-pair', 'hor-pairs', 'vert-pairs', 'other'] exit (0) # now split this into tiles: # # each tile's disparity: #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 # provide layer name values = ijt.getvalues(label='other') values = ijt.getvalues(label='other') print("Stack of values shape: "+str(values.shape)) print("Stack of values shape: "+str(values.shape)) # each tile's disparity: fig = plt.figure() fig = plt.figure() fig.suptitle("Estimated Disparity") fig.suptitle("Estimated Disparity") plt.imshow(values[:,:,0]) plt.imshow(values[:,:,0]) Loading @@ -474,40 +498,6 @@ if __name__ == "__main__": plt.colorbar() plt.colorbar() print(bcolors.BOLDWHITE+"time: "+str(time.time())+bcolors.ENDC) 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']) #ijt.show_images([0,3]) #ijt.show_images(['X-corr','Y-corr']) #ijt.show_images(['R-vign',3]) ijt.show_images() ijt.show_images() plt.show() plt.show() Loading