Example #1
0
                #ann = sio.loadmat(os.path.join(ann_dir, '{}.mat'.format(basename)))
                #ann_inst = ann['inst_map']
                #ann_type = ann['type_map']

                # merge classes for CoNSeP (in paper we only utilise 3 nuclei classes and background)
                # If own dataset is used, then the below may need to be modified
                # TODO: move to preproc CoNSeP dataset
                # ann_type[(ann_type == 3) | (ann_type == 4)] = 3
                # ann_type[(ann_type == 5) | (ann_type == 6) | (ann_type == 7)] = 4
                
                # print (f"nr_types = {cfg.nr_types}, max in annotation = {np.max(ann_type)}")
                assert np.max(ann_type) <= cfg.nr_types, \
                                ("Only {} types of nuclei are defined for training"\
                                "but there are {} types found in the input image.").format(cfg.nr_types, np.max(ann_type))

                ann = np.dstack([ann_inst, ann_type])
                ann = ann.astype('int32')
            else:
                # assumes that ann is HxW
                # ann_inst = sio.loadmat(os.path.join(ann_dir, '{}.mat'.format(basename)))
                ann = np.load(os.path.join(ann_dir, '{}.npy'.format(basename)))
                ann_inst = (ann_inst.item().get('inst_map')).astype('int32')
                # ann_inst = ann_inst.astype('int32')
                ann = np.expand_dims(ann_inst, -1)
                
            img = np.concatenate([img, ann], axis=-1)
            sub_patches = xtractor.extract(img, cfg.extract_type)
            for idx, patch in enumerate(sub_patches):
                np.save("{0}/{1}_{2:03d}.npy".format(out_dir, basename, idx), patch)
            print (f"{out_dir}/{basename} saved.")
        if not os.path.exists(save_dir): os.mkdir(save_dir)
        colormap = [class_colors[x] for x in inst_type]
        overlaid_output = visualize_instances(instance_map, patchcorr,
                                              colormap)
        cv2.imwrite("{}/{}.png".format(save_dir, basename), overlaid_output)
        #type_map[np.logical_and(instance_map > 0, type_map == 0)] = -1
        unannot = np.logical_and(instance_map != 0, type_map == 0)
        patchjoin[unannot] = 255
        instance_map[unannot] = 0
        ann = np.dstack([instance_map, type_map])
        patchjoin = np.array(patchjoin, np.int32)
        print(ann.shape)
        print(patchjoin.shape)
        patchjoin = np.concatenate([patchjoin, ann], axis=-1)
        sub_patches = xtractor.extract(
            patchjoin, extract_type
        )  # This extracts mirrored subpatch of 540x540 from 1k patch
        for idx, patch in enumerate(sub_patches):
            np.save("{}/{}_{}.npy".format(save_dir, basename, idx), patch)

    f = open(fdone, 'w')
    t = len(points)
    f.write("Total Number of Dots annotated are: {}\n".format(t))
    if t != 0:
        f.write(
            "Number of Dots that fall outside of boxes are: {}\n".format(t -
                                                                         b))
        f.write(
            "Percentage of the dots falling into the boxes: {}%\n\n".format(
                (b / t) * 100))
        if segment: