예제 #1
0
    def run(self, u_info):

        ## Load DB
        db = DB(u_info)
        self.u_info = u_info
        ##
        ## Update split and adjust
        ##

        for iz in range(db.num_tiles_z):

            print('Saving: ', iz, '/', db.num_tiles_z)
            # Check teemporary data
            data_path = u_info.tmp_tile_ids_path + u_info.tile_path_wz.format(0, iz)
            if not os.path.isdir(data_path):
                continue

            # print('Copy from ', data_path)
            for iw in range(db.num_tiles_w):
                source_dir = u_info.tmp_tile_ids_path \
                                 + u_info.tile_path_wz.format(iw, iz)
                destination_dir = u_info.tile_ids_path \
                                      + u_info.tile_path_wz.format(iw, iz)
                shutil.rmtree(destination_dir)
                shutil.move(source_dir, destination_dir)

                ## Remove temp file
                # shutil.rmtree(source_dir)

        ##
        ## Update merges
        ##

        # print(u_info.merge_table)

        for iw in range(db.num_tiles_w):
            for iz, iy, ix in itertools.product(range(db.num_tiles_z), range(db.num_tiles_y_at_w[iw]), range(db.num_tiles_x_at_w[iw])):

                ### Load tile file
                tile_ids_filename = u_info.tile_ids_path + u_info.tile_ids_filename_wzyx.format( iw, iz, iy, ix )
                tile_ids = m.load_hdf5( tile_ids_filename, u_info.tile_var_name )

                ## Color exchange [for merge? check __merge_table.keys() ]
                for mm in u_info.merge_table.keys():
                    mm_id = self.lookup_label(mm, u_info.merge_table)
                    tile_ids[ tile_ids == int(mm) ] = mm_id

                ### Save tile file
                    m.save_hdf5(tile_ids_filename, u_info.tile_var_name, tile_ids)

        u_info.merge_table = {}
        u_info.flag_undo = 0
        u_info.flag_redo = 0

        ## Update
        print('Updating database.')
        db.Update()
        print('Successfully saved.')
예제 #2
0
    def run(self, input_ids, tile_index_z):

        in_shape = input_ids.shape
        pad_shape = self.round(in_shape)
        original_ids = np.zeros(pad_shape, dtype=input_ids.dtype)
        original_ids[:in_shape[0], :in_shape[1]] = input_ids

        (original_ids_num_pixels_y, original_ids_num_pixels_x
         ) = original_ids.shape  ######## ###### 180624Change

        ## Grow regions until there are no boundaries

        current_ids_counts = np.bincount(original_ids.ravel())
        current_ids_counts_ids = np.nonzero(current_ids_counts)[0]
        current_max = np.max(current_ids_counts_ids)
        self.tile_index_z = tile_index_z

        if self.id_max < current_max:
            self.id_max = current_max
            self.id_counts.resize(self.id_max + 1)

        self.id_counts[current_ids_counts_ids] = self.id_counts[
            current_ids_counts_ids] + np.int64(
                current_ids_counts[current_ids_counts_ids])

        current_ids_num_pixels_y = original_ids_num_pixels_y
        current_ids_num_pixels_x = original_ids_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_w = 0
        ids_stride = 1

        while current_ids_num_pixels_y > self.tile_num_pixels_y / 2 or current_ids_num_pixels_x > self.tile_num_pixels_x / 2:

            current_tile_ids_path = self.output_tile_ids_path + self.tile_path_wz.format(
                self.tile_index_w, self.tile_index_z)
            m.mkdir_safe(current_tile_ids_path)

            current_ids = original_ids[::ids_stride, ::ids_stride]

            num_tiles_y = int(
                math.ceil(
                    float(current_ids_num_pixels_y) / self.tile_num_pixels_y))
            num_tiles_x = int(
                math.ceil(
                    float(current_ids_num_pixels_x) / self.tile_num_pixels_x))

            print('Scale: ', ids_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):

                    y = iy * self.tile_num_pixels_y
                    x = ix * self.tile_num_pixels_x

                    current_tile_ids_name = self.output_tile_ids_path + self.tile_ids_filename_wzyx.format(
                        self.tile_index_w, self.tile_index_z, iy, ix)

                    tile_ids = np.zeros(
                        (self.tile_num_pixels_y, self.tile_num_pixels_x),
                        np.uint32)
                    tile_ids_non_padded = current_ids[y:y +
                                                      self.tile_num_pixels_y,
                                                      x:x +
                                                      self.tile_num_pixels_x]
                    tile_ids[0:tile_ids_non_padded.shape[0],
                             0:tile_ids_non_padded.
                             shape[1]] = tile_ids_non_padded[:, :]

                    if os.path.isfile(
                            current_tile_ids_name):  ## Backup for undo
                        shutil.move(current_tile_ids_name,
                                    current_tile_ids_name + '_')
                        self.u_info.ids_files_undo.extend(
                            current_tile_ids_name)
                    m.save_hdf5(current_tile_ids_name,
                                self.u_info.tile_var_name, tile_ids)

                    for unique_tile_id in np.unique(tile_ids):

                        self.id_tile_list.append(
                            (unique_tile_id, self.tile_index_w,
                             self.tile_index_z, iy, ix))

            current_ids_num_pixels_y = current_ids_num_pixels_y / 2
            current_ids_num_pixels_x = current_ids_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
            ids_stride = ids_stride * 2
예제 #3
0
    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.')