def Execute3D(self, w): ## ## Load image ## filestack = self.ObtainTarget() params = self.ObtainParamsBottomTable(self.obj_args, self.args) output_path = params['Output Folder'] if len(output_path) == 0: print('Output folder unspecified.') return False # numz = len(filestack) # size = cv2.imread(filestack[0], cv2.IMREAD_GRAYSCALE).shape check_attribute = m.imread(filestack[0], flags=cv2.IMREAD_GRAYSCALE) tsize = check_attribute.shape tdtype = check_attribute.dtype input_volume = np.zeros([tsize[0], tsize[1], numz], tdtype) print('Loading images ...') for zi, filename in enumerate(filestack): # input_volume[:, :, zi] = cv2.imread(filename, cv2.IMREAD_GRAYSCALE).astype(tdtype) input_volume[:, :, zi] = m.imread(filename, flags=cv2.IMREAD_GRAYSCALE) ## ## 2D/3D filter application ## for i in range(w.count()): item = w.item(i) text = item.text() instance = item.data(Qt.UserRole) params = self.ObtainParamsFilter(instance.args) type = self.fi.get_type(text) cls = self.fi.get_class(text) if type == '2d': for zi in range(numz): input_image = input_volume[:, :, zi] output_image = cls.Filter(self, input_image, params) input_volume[:, :, zi] = output_image elif type == '3d': tmp = cls.Filter(self, input_volume, params) input_volume = tmp.astype(np.uint16) # Unlock Folder m.UnlockFolder(self.parent.u_info, output_path) # Save segmentation print('Saving images ...') for zi, filename in enumerate(filestack): output_name = os.path.basename(filename) savename = os.path.join(output_path, output_name) root, ext = os.path.splitext(savename) if ext == ".tif" or ext == ".tiff" or ext == ".TIF" or ext == ".TIFF": m.save_tif16(input_volume[:, :, zi], savename) elif ext == ".png" or ext == ".PNG": m.save_png16(input_volume[:, :, zi], savename) print('2D/3D filters were applied!') # Lock Folder m.LockFolder(self.parent.u_info, output_path)
def _Run(self, parent, params, comm_title): ## print('Start postprocesing.') print(params['Output Filetype']) data = np.load(params['Target Sementation File (npz)']) print('File contents :', data.files) segmentation = data['segmentation'] print('Segmentation image size: ', segmentation.shape) filetype = params['Output Filetype'] ## ## if (filetype == '8-bit color PNG') or (filetype == '8-bit color TIFF'): ids = np.max(segmentation) print('Max segmentation ID: ', ids) colormap = np.random.randint(255, size=(ids + 2, 3), dtype=np.uint64) colormap[0, :] = 0 ## m.UnlockFolder(parent.u_info, params['Output Segmentation Folder']) ## for idz in range(segmentation.shape[0]): image2d = segmentation[idz, :, :] print('image2d size: ', image2d.shape) if filetype == '16-bit gray scale TIFF': filename = os.path.join(params['Output Segmentation Folder'], 'z{:0=4}.tif'.format(idz)) m.save_tif16(image2d, filename) elif filetype == '8-bit gray scale TIFF': filename = os.path.join(params['Output Segmentation Folder'], 'z{:0=4}.tif'.format(idz)) m.save_tif8(image2d, filename) elif filetype == '16-bit gray scale PNG': filename = os.path.join(params['Output Segmentation Folder'], 'z{:0=4}.png'.format(idz)) m.save_png16(image2d, filename) elif filetype == '8-bit gray scale PNG': filename = os.path.join(params['Output Segmentation Folder'], 'z{:0=4}.png'.format(idz)) m.save_png8(image2d, filename) elif filetype == '8-bit color PNG': filename = os.path.join(params['Output Segmentation Folder'], 'z{:0=4}.png'.format(idz)) m.save_pngc(image2d, filename, colormap) elif filetype == '8-bit color TIFF': filename = os.path.join(params['Output Segmentation Folder'], 'z{:0=4}.tif'.format(idz)) m.save_tifc(image2d, filename, colormap) else: print('Data was not saved.') ## print(comm_title, 'was finished.') flag = m.LockFolder(parent.u_info, params['Output Segmentation Folder']) return flag
def Execute2D(self, w): ## ## Input files /Output folder ## self.filestack = self.ObtainTarget() params = self.ObtainParamsBottomTable(self.obj_args, self.args) output_path = params['Output Folder'] if len(output_path) == 0: print('Output folder unspecified.') return False # Unlock Folder m.UnlockFolder(self.parent.u_info, output_path) for filename in self.filestack: print(filename) output_name = os.path.basename(filename) # input_image = cv2.imread(filename, cv2.IMREAD_GRAYSCALE) input_image = m.imread(filename, flags=cv2.IMREAD_GRAYSCALE) output_image = self.FilterApplication2D(w, input_image) output_dtype = output_image.dtype savename = os.path.join(output_path, output_name) root, ext = os.path.splitext(savename) if ext == ".tif" or ext == ".tiff" or ext == ".TIF" or ext == ".TIFF": if output_dtype == 'uint16': m.save_tif16(output_image, savename) elif output_dtype == 'uint8': m.save_tif8(output_image, savename) else: print('dtype mismatch: ', output_dtype) elif ext == ".png" or ext == ".PNG": if output_dtype == 'uint16': m.save_png16(output_image, savename) elif output_dtype == 'uint8': m.save_png8(output_image, savename) else: print('dtype mismatch: ', output_dtype) print('2D filters were applied!') # Lock Folder m.LockFolder(self.parent.u_info, output_path)
def run(self, u_info, dir, fname, ftype, startid, numdigit, flag): ## Load DB db = DB(u_info) print("Tile Num: z {0}, y {1}, x {2}".format(db.num_tiles_z, db.num_tiles_y, db.num_tiles_x)) ## Makedir #self.mkdir_safe(dir) ## Save ColorInfo & SegmentationInfo if flag == 'ids': self.save_color_data(u_info, dir) self.save_segmentInfo(u_info, dir) ## Volume storage VOLUME_FORMAT = ["MTIF16G", "MTIF8G", "MTIF8C","NUMPY32", "NUMPY32C","HDF64"] if ftype in VOLUME_FORMAT: volume_images_ids = np.zeros((db.num_voxels_z, db.num_voxels_y, db.num_voxels_x), np.uint32) ## ## Export image/segmentation files ## print("Tile Num: z {0}, y {1}, x {2}".format(db.num_tiles_z, db.num_tiles_y, db.num_tiles_x)) iw = 0 for iz in range(db.num_tiles_z): # 100): # print("iz ", iz) merged_images_ids = np.zeros( ( db.canvas_size_y, db.canvas_size_x ), np.uint32 ) tile_image = [] for iy, ix in itertools.product( range(db.num_tiles_y), range(db.num_tiles_x)): if flag == 'images': filename = u_info.tile_images_path + u_info.tile_images_filename_wzyx.format(iw, iz, iy, ix) print(filename) tile_image = PIL.Image.open(filename) elif flag == 'ids' : filename = u_info.tile_ids_path + u_info.tile_ids_filename_wzyx.format(iw, iz, iy, ix) print(filename) tile_image = m.load_hdf5(filename, u_info.tile_var_name) else: return False y = iy * db.num_voxels_per_tile_y x = ix * db.num_voxels_per_tile_x merged_images_ids[ y : y + db.num_voxels_per_tile_y, x : x + db.num_voxels_per_tile_x ] = tile_image # Crop by original image size merged_images_ids = merged_images_ids[ 0:db.num_voxels_y, 0:db.num_voxels_x ] # Filename setting # u_info, dir, fname, ftype, startid, numdigit current_frefix = dir + os.sep + fname + str(int(iz+startid)).zfill(numdigit) print(current_frefix) # if ftype in VOLUME_FORMAT: volume_images_ids[iz,:,:] = merged_images_ids elif ftype == "PNG16G": m.save_png16(merged_images_ids, current_frefix+".png") elif ftype == "PNG8G": m.save_png8(merged_images_ids, current_frefix+".png") elif ftype == "PNG8C": colordata = m.load_hdf5(u_info.color_map_file, u_info.hdf_color_name) m.save_pngc(merged_images_ids, current_frefix+".png", colordata) elif ftype == "TIF16G": m.save_tif16(merged_images_ids, current_frefix+".tif") elif ftype == "TIF8G": m.save_tif8(merged_images_ids, current_frefix+".tif") elif ftype == "TIF8C": colordata = m.load_hdf5(u_info.color_map_file, u_info.hdf_color_name) m.save_tifc(merged_images_ids, current_frefix+".tif", colordata) else: print("Export filetype error (Internal Error).") ### ### current_frefix = dir + os.sep + fname print('Save file to ', current_frefix) if ftype == "MTIF16G": volume_images_ids = volume_images_ids.astype(np.uint16) tifffile.imsave(current_frefix + ".tif", volume_images_ids) elif ftype == "MTIF8G": volume_images_ids = volume_images_ids.astype(np.uint8) tifffile.imsave(current_frefix + ".tif", volume_images_ids) elif ftype == "MTIF8C": print('Multi-tiff 8 color, save.') colordata = m.load_hdf5(u_info.color_map_file, u_info.hdf_color_name) volume_images_ids = self.gen_col_multi(volume_images_ids, colordata) tifffile.imsave(current_frefix + ".tif", volume_images_ids) elif ftype == "NUMPY32": volume_images_ids = volume_images_ids.astype(np.uint32) np.save(current_frefix + ".npy", volume_images_ids) elif ftype == "NUMPY32C": volume_images_ids = volume_images_ids.astype(np.uint32) np.savez(current_frefix + ".npz", stack=volume_images_ids) elif ftype == "HDF64": volume_images_ids = volume_images_ids.astype(np.int64) m.save_hdf5(current_frefix + ".h5", "stack", volume_images_ids) print('Images/segmentations were Exported.')