def masks_to_annotation(datasets_dir, save_path):
    masks_dir = os.path.join(datasets_dir, "labels_all")
    nucleis_dir = os.path.join(datasets_dir, "images_all")

    # %% Process one folder and save as one json file allowing multiple annotation types
    simplify_tol = 0  # Tolerance for polygon simplification with shapely (0 to not simplify)

    # outputs_dir = os.path.abspath(os.path.join('..', 'data', 'postProcessing', 'mask2json'))
    if os.path.exists(masks_dir):
        print(f'Analyzing folder:{masks_dir}')
        for file in [f for f in os.listdir(masks_dir)]:
            file_id = os.path.splitext(file)[0]

            # Read png with mask
            print(f'Analyzing file:{file}')

            file_full = os.path.join(masks_dir, file)
            mask_img = io.imread(file_full)
            print("mask_img.shape:", mask_img.shape)
            mask = measure.label(mask_img)
            label = "nuclei"
            print("label:", label)
            sample_path = os.path.join(save_path, file_id)
            if not os.path.exists(sample_path):
                os.makedirs(sample_path)
            io.imsave(os.path.join(sample_path, "mask_labels.png"), mask_img)
            shutil.copyfile(
                os.path.join(nucleis_dir, file.replace(".tif", ".png")),
                os.path.join(sample_path, "nuclei.png"))
            segmentationUtils.masks_to_polygon(mask,
                                               label=label,
                                               simplify_tol=simplify_tol,
                                               save_name=os.path.join(
                                                   sample_path,
                                                   "annotation.json"))
Beispiel #2
0
    def masks_to_annotation(self, outputs_dir, outputs=None):
        # %% Process one folder and save as one json file allowing multiple annotation types
        simplify_tol = 0  # Tolerance for polygon simplification with shapely (0 to not simplify)

        if os.path.exists(outputs_dir):
            print(f'Analyzing folder:{outputs_dir}')
            features = []  # For geojson
            image_size = None
            for out in outputs:
                file_full = os.path.join(outputs_dir, out.get("name") + "_output.png")
                if os.path.exists(file_full):
                    print("get output file path:", file_full)
                    mask_img = io.imread(file_full)
                    print("mask_img.shape:", mask_img.shape)
                    mask = measure.label(mask_img)
                    post_mask = DRFNStoolbox.seedless_segment(mask, 15, p_thresh=0.5)
                    img = Image.fromarray(post_mask.astype('uint8'))
                    img.save(os.path.join(outputs_dir, out.get("name") + '_noSeeds_OBJECTS.png'))
                    if out.get("postProcessing").get("type") == "withseed":
                        seed_mask_file = os.path.join(outputs_dir, out.get("postProcessing").get("seed") + "_output.png")
                        if os.path.exists(seed_mask_file):
                            print("get seed mask file :", seed_mask_file)
                            seed_mask_img = io.imread(seed_mask_file)
                            print("seed_mask_img.shape:", seed_mask_img.shape)
                            seed_mask = measure.label(seed_mask_img)
                            post_mask = DRFNStoolbox.segment_with_seed(seed_mask, post_mask, 15, p_thresh=0.5)
                            img = Image.fromarray(post_mask.astype('uint8'))
                            img.save(os.path.join(outputs_dir, out.get("name") + '_wSeeds_OBJECTS.png'))
                        else:
                            print("warming: seed mask file not exist:", seed_mask_file)

                    # Here summarizing the geojson should occur
                    image_size = mask_img.shape  # This might cause problems if any kind of binning was performed

                    # Get label from file name
                    label = out.get("name").split('_', 1)[0]
                    print("label:", label)
                    # print(mask_img[0:1, :100])

                    # Call function to transform segmentation masks into (geojson) polygons
                    feature, contours = segmentationUtils.masks_to_polygon(post_mask,
                                                                           label=label,
                                                                           simplify_tol=simplify_tol)
                    # save_name=file_full.replace(".png", ".json"))
                    features = features + feature
                else:
                    print("warming: output file not exist:", file_full)
            # feature_collection = FeatureCollection(features, bbox=[0, 0.0, image_size[0], image_size[1]])
            feature_collection = FeatureCollection(features, bbox=[0, 0.0, image_size[0], image_size[1]])

            # Save to json file
            save_name_json = os.path.join(outputs_dir, 'prediction.json')
            with open(save_name_json, 'w') as f:
                dump(feature_collection, f)
                f.close()
            return feature_collection