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, parent, params, comm_title): input_files = m.ObtainImageFiles(params['Image Folder']) if len(input_files) == 0: print('No images in the Image Folder.') return False im = m.imread(input_files[0], cv2.IMREAD_UNCHANGED) root, ext_image = os.path.splitext(os.path.basename(input_files[0])) print('') print('Target file to check color type : ', input_files[0]) print('Image dimensions : ', im.shape) print('Image filetype : ', im.dtype) image_size_x = im.shape[1] image_size_y = im.shape[0] if (image_size_x <= 256 or image_size_y <= 256): print('Image size is too small.') return False # Generate tmpdir tmpdir_standardized = os.path.join( params['Output Segmentation Folder (Empty)'], "standardized" + str(threading.get_ident()).zfill(6)[-6:]) if os.path.exists(tmpdir_standardized): shutil.rmtree(tmpdir_standardized) os.mkdir(tmpdir_standardized) # tmpdir_output = os.path.join( params['Output Segmentation Folder (Empty)'], "output" + str(threading.get_ident()).zfill(6)[-6:]) if os.path.exists(tmpdir_output): shutil.rmtree(tmpdir_output) os.mkdir(tmpdir_output) ## Check image size max_image_size = params['Maximal unit image size'] if max_image_size == '512': std_sizes = np.array([512]) elif max_image_size == '1024': std_sizes = np.array([512, 1024]) elif max_image_size == '2048': std_sizes = np.array([512, 1024, 2048]) else: print('Internal error at Maximal unit image size.') return False max_std_size = np.max(std_sizes) if image_size_x > max_std_size: unit_image_size_x = max_std_size num_tiles_x = np.int(np.ceil(float(image_size_x) / max_std_size)) else: unit_image_size_x = np.min(std_sizes[std_sizes >= image_size_x]) num_tiles_x = 1 if image_size_y > max_std_size: unit_image_size_y = max_std_size num_tiles_y = np.int(np.ceil(float(image_size_y) / max_std_size)) else: unit_image_size_y = np.min(std_sizes[std_sizes >= image_size_y]) num_tiles_y = 1 # converted_size_x = unit_image_size_x * num_tiles_x converted_size_y = unit_image_size_y * num_tiles_y fringe_size_x = converted_size_x - image_size_x fringe_size_y = converted_size_y - image_size_y # # output_files = [] print('Image standardization: ') for input_file in input_files: im_col = m.imread(input_file) # im_col = self._ChangeIntoColor(im_col) filename = path.basename(input_file) print(filename + ' ') for ext in [ '.TIF', '.tif', '.TIFF', '.tiff', '.PNG', '.jpg', '.jpeg', '.JPG', '.JPEG' ]: filename = filename.replace(ext, '.png') output_files.append(filename) # add fringe X im_fringe_x = cv2.flip(im_col, 1) # flipcode > 0, left-right im_fringe_x = im_fringe_x[:, 0:fringe_size_x] converted_image = cv2.hconcat([im_col, im_fringe_x]) # add fringe Y im_fringe_y = cv2.flip(converted_image, 0) # flipcode = 0, top-bottom im_fringe_y = im_fringe_y[0:fringe_size_y, :] converted_image = cv2.vconcat([converted_image, im_fringe_y]) # Save if (num_tiles_x == 1) and (num_tiles_y == 1): converted_filename = os.path.join(tmpdir_standardized, filename) m.imwrite(converted_filename, converted_image) else: for iy in range(num_tiles_y): for ix in range(num_tiles_x): y0 = iy * unit_image_size_y y1 = y0 + unit_image_size_y x0 = ix * unit_image_size_x x1 = x0 + unit_image_size_x current_tile = converted_image[y0:y1, x0:x1] converted_filename = str(ix).zfill(3)[-3:] + '_' + str( iy).zfill(3)[-3:] + '_' + filename converted_filename = os.path.join( tmpdir_standardized, converted_filename) m.imwrite(converted_filename, current_tile) #Complete print('') print('Images were split and changed into RGB 8bit, and stored in ', tmpdir_standardized) print('') tmp = ['--mode' , 'predict' , \ '--save_freq' , '0' , \ '--input_dir' , tmpdir_standardized, \ '--output_dir' , tmpdir_output, \ '--checkpoint' , params['Model Folder'], \ '--image_height', str(unit_image_size_y), \ '--image_width' , str(unit_image_size_x)] comm = parent.u_info.exec_translate[:] comm.extend(tmp) print('') print(' '.join(comm)) print('') print('Start inference.') print('') m.UnlockFolder(parent.u_info, params['Output Segmentation Folder (Empty)'] ) # Only for shared folder/file s.run(comm) print('') print('Segmentation reconstruction: ') for output_file in output_files: ## if (num_tiles_x == 1) and (num_tiles_y == 1): ## Remove fringes filename = os.path.join(tmpdir_output, output_file) inferred_segmentation = m.imread(filename, flags=cv2.IMREAD_GRAYSCALE, dtype='uint8') else: ## Merge split images. inferred_segmentation = np.zeros( (converted_size_y, converted_size_x), dtype='uint8') for iy in range(num_tiles_y): for ix in range(num_tiles_x): y0 = iy * unit_image_size_y y1 = y0 + unit_image_size_y x0 = ix * unit_image_size_x x1 = x0 + unit_image_size_x current_tile_filename = str(ix).zfill( 3)[-3:] + '_' + str(iy).zfill( 3)[-3:] + '_' + output_file current_tile_filename = os.path.join( tmpdir_output, current_tile_filename) current_tile = m.imread(current_tile_filename, flags=cv2.IMREAD_GRAYSCALE, dtype='uint8') inferred_segmentation[y0:y1, x0:x1] = current_tile[:, :] inferred_segmentation = inferred_segmentation[0:image_size_y, 0:image_size_x] print('inferred_segmentation: ', inferred_segmentation.shape, inferred_segmentation.dtype) ## Save filename_base = os.path.splitext(os.path.basename(output_file))[0] filename_base = os.path.join( params['Output Segmentation Folder (Empty)'], filename_base) filetype = params['Output Filetype'] if filetype == '8-bit gray scale PNG': filename = filename_base + '.png' m.save_png8(inferred_segmentation, filename) elif filetype == '8-bit gray scale TIFF (Uncompressed)': filename = filename_base + '.tif' m.save_tif8(inferred_segmentation, filename, compression=1) elif filetype == '8-bit gray scale TIFF (Compressed)': filename = filename_base + '.tif' m.save_tif8(inferred_segmentation, filename) else: print('Internel error: bad filetype.') print(filename) ## # rm tmpdir if os.path.exists(tmpdir_standardized): shutil.rmtree(tmpdir_standardized) if os.path.exists(tmpdir_output): shutil.rmtree(tmpdir_output) parent.parent.ExecuteCloseFileFolder( params['Output Segmentation Folder (Empty)']) parent.parent.OpenFolder(params['Output Segmentation Folder (Empty)']) print('') print('Finish inference.') print('') return True
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.')
def run(self, input_image, tile_index_z): in_shape = input_image.shape pad_shape = self.round(in_shape) original_image = np.zeros(pad_shape, dtype=input_image.dtype) original_image[:in_shape[0], :in_shape[1]] = input_image (original_image_num_pixels_y, original_image_num_pixels_x ) = original_image.shape ###### 180624Change current_image_num_pixels_y = original_image_num_pixels_y current_image_num_pixels_x = original_image_num_pixels_x current_tile_data_space_y = self.tile_num_pixels_y current_tile_data_space_x = self.tile_num_pixels_x self.tile_index_z = tile_index_z self.tile_index_w = 0 images_stride = 1 ### print('Image size (x, y): ', original_image_num_pixels_x, original_image_num_pixels_y) while current_image_num_pixels_y > self.tile_num_pixels_y / 2 or current_image_num_pixels_x > self.tile_num_pixels_x / 2: current_tile_image_path = self.output_tile_image_path + self.tile_path_wz.format( self.tile_index_w, self.tile_index_z) m.mkdir_safe(current_tile_image_path) # current_image = cv2.resize(original_image,( current_image_num_pixels_x, current_image_num_pixels_y )) current_image = original_image[::images_stride, ::images_stride] num_tiles_y = int( math.ceil( float(current_image_num_pixels_y) / self.tile_num_pixels_y)) num_tiles_x = int( math.ceil( float(current_image_num_pixels_x) / self.tile_num_pixels_x)) print('Scale: ', images_stride) print('Number of panels (x, y): ', num_tiles_x, num_tiles_y) for iy in range(num_tiles_y): for ix in range(num_tiles_x): current_tile_image_name = self.output_tile_image_path + self.tile_images_filename_wzyx.format( self.tile_index_w, self.tile_index_z, iy, ix) y = iy * self.tile_num_pixels_y x = ix * self.tile_num_pixels_x ## tile_image = current_image[y:y + self.tile_num_pixels_y, x:x + self.tile_num_pixels_x] ### ## m.save_tif8(tile_image, current_tile_image_name ) ### tmp = current_image[y:y + self.tile_num_pixels_y, x:x + self.tile_num_pixels_x] tile_with_fringe = np.zeros( (self.tile_num_pixels_y, self.tile_num_pixels_x), self.images_dtype) tile_with_fringe[0:tmp.shape[0], 0:tmp.shape[1]] = tmp m.save_tif8(tile_with_fringe, current_tile_image_name) #print(current_tile_image_name) current_image_num_pixels_y = current_image_num_pixels_y / 2 current_image_num_pixels_x = current_image_num_pixels_x / 2 current_tile_data_space_y = current_tile_data_space_y * 2 current_tile_data_space_x = current_tile_data_space_x * 2 self.tile_index_w = self.tile_index_w + 1 images_stride = images_stride * 2