Esempio n. 1
0
def plot_regions_on_all_images():

    k_list = []
    for j in range(nb_images):
        all_mask = segmentation(image_lab_list[j],
                                2,
                                2,
                                2,
                                brown_lab=np.array([16.36, 110.89, 53.06]),
                                blue_lab=np.array([18.10, 50.58, -70.44]),
                                white_lab=np.array([53.15, -18.15, 1.20]))

        results_p_dir_bis = os.path.join(results_dir, "image", f'{j}')
        os.makedirs(results_p_dir_bis, exist_ok=True)

        regions_positive = outline_regions(
            image_original_list[j][10:image_original_list[j].shape[0] - 10,
                                   10:image_original_list[j].shape[1] - 10],
            all_mask[0, :, :])
        io.imsave(fname=os.path.join(results_p_dir_bis,
                                     f'regions positive.jpg'),
                  arr=regions_positive)
        regions_negative = outline_regions(
            image_original_list[j][10:image_original_list[j].shape[0] - 10,
                                   10:image_original_list[j].shape[1] - 10],
            all_mask[1, :, :])
        io.imsave(fname=os.path.join(results_p_dir_bis,
                                     f'regions negative.jpg'),
                  arr=regions_negative)
        regions_background = outline_regions(
            image_original_list[j][10:image_original_list[j].shape[0] - 10,
                                   10:image_original_list[j].shape[1] - 10],
            all_mask[2, :, :])
        io.imsave(fname=os.path.join(results_p_dir_bis,
                                     f'regions background.jpg'),
                  arr=regions_background)

        k = validation(all_mask[0, :, :], all_mask[1, :, :], all_mask[2, :, :],
                       ref_paths[j][0], ref_paths[j][1])
        print('k = ', k)

        k_list.append(k)
        k_history[j].append(k)

    print('k mean = ', np.mean(k_list))
    k_history[-1].append(np.mean(k_list))

    return 1 / np.mean(k_list)
Esempio n. 2
0
 def label_patches(self, patches, max_patches=None):
     if max_patches is not None:
         patches = itertools.islice(patches, max_patches)
     for patch in patches:
         assert isinstance(patch, Patch)
         os.makedirs(root_dir(FOLDER_TEMP_DATA), exist_ok=True)
         path_patch = root_dir(FOLDER_TEMP_DATA, 'patch.png')
         path_outln = root_dir(FOLDER_TEMP_DATA, 'outln.png')
         images = {
             path_patch: patch.cropped_image(),
             path_outln: outline_regions(image=patch.cropped_image(),
                                         region_labels=patch.cropped_mask())
         }
         [imsave(fname=rel_path, arr=resize(img, output_shape=(600, 600))) for rel_path, img in images.items()]
         labels = LABELS_Ki67 + ['Not well defined']
         while patch.expert_label not in labels:
             # noinspection PyTypeChecker
             gui = buttonbox("Ki67 - Manual labelling", choices=labels,
                             images=list(images.keys()), run=False)
             gui.ui.set_pos("+0+0")
             patch.expert_label = gui.run()
         print(patch.expert_label)
         self.labelled_patches.append(patch)
Esempio n. 3
0
                      arr=image)

            logging.info('CLAHE')
            image = apply_on_normalized_luminance(
                lambda img: equalize_adapthist(img, clip_limit=0.02),
                image_rgb=image)
            io.imsave(fname=os.path.join(results_p_dir, f'01 03 - CLAHE.jpg'),
                      arr=image)

            logging.info('K-means clustering')
            image_flat = rgb2lab(image).reshape((-1, 3))
            clustering = KMeans(n_clusters=CLUSTERING_NUM_CENTROIDS, random_state=0).fit(image_flat)
            io.imsave(fname=os.path.join(results_p_dir, f'02 K-means - labels.jpg'),
                      arr=colormap(clustering.labels_.reshape(image.shape[0:2])))
            io.imsave(fname=os.path.join(results_p_dir, f'02 K-means - regions.jpg'),
                      arr=outline_regions(image=original_image, region_labels=clustering.labels_.reshape(image.shape[0:2])))
            io.imsave(fname=os.path.join(results_p_dir, f'02 K-means - average_color.jpg'),
                      arr=average_color(image=original_image, region_labels=clustering.labels_.reshape(image.shape[0:2])))

            logging.info('Class separation')
            # Positive mask: cluster with maximum on channel a
            idx_positive_cluster = np.argmax(clustering.cluster_centers_[:, 1])
            positive_mask = np.equal(clustering.labels_, idx_positive_cluster).reshape(image.shape[0:2])
            # Negative mask: cluster with minimum on channel b
            idx_negative_cluster = np.argmin(clustering.cluster_centers_[:, 2])
            negative_mask = np.equal(clustering.labels_, idx_negative_cluster).reshape(image.shape[0:2])
            # Visualize
            io.imsave(fname=os.path.join(results_p_dir, f'03 Positives.jpg'),
                      arr=outline_regions(image=original_image, region_labels=positive_mask))
            io.imsave(fname=os.path.join(results_p_dir, f'03 Negatives.jpg'),
                      arr=outline_regions(image=original_image, region_labels=negative_mask))
Esempio n. 4
0
            logging.info('Visualizing classification')
            classification_colored = visualize_classification(classification)
            io.imsave(fname=os.path.join(results_p_dir, '04 04 Classification colored.jpg'),
                      arr=classification_colored)

            logging.info('Creating some patches')
            c1 = PatchClassifier()
            patches = list(c1.patches(image=image, region_labels=segmentation_idcs))

            logging.info('Labelling patches')
            c1.label_patches(patches=patches, max_patches=MAX_PATCHES_PER_IMAGE)

            logging.info('Saving patches')
            c1.save_labelled_patches(training_label=execution_id)

            logging.info('Retrieving patches')
            c2 = PatchClassifier()
            c2.load_labelled_patches(training_label=execution_id)

            logging.info('Visualizing retrieved patches')
            for idx_patch, patch in enumerate(c2.labelled_patches):
                io.imsave(fname=os.path.join(results_p_dir, f'05 {idx_patch:02d} Patch image classification.jpg'),
                          arr=colormap(crop(image=segmentation_idcs, bounding_box=patch.bounding_box)))
                io.imsave(fname=os.path.join(results_p_dir, f'05 {idx_patch:02d} Patch image outlined.jpg'),
                          arr=outline_regions(patch.cropped_image(), patch.cropped_mask()))
                io.imsave(fname=os.path.join(results_p_dir, f'05 {idx_patch:02d} Patch image.jpg'),
                          arr=patch.cropped_image())
                io.imsave(fname=os.path.join(results_p_dir, f'05 {idx_patch:02d} Patch mask.jpg'),
                          arr=colormap(patch.cropped_mask()))
Esempio n. 5
0
            logging.info(
                'Positive separation (k-means clustering on `a channel`)')
            channel_a = image_lab[:, :, 1]
            clustering = KMeans(n_clusters=2,
                                random_state=0).fit(channel_a.reshape(-1, 1))
            idx_positive_cluster = np.argmax(clustering.cluster_centers_)
            positive_mask = np.equal(clustering.labels_,
                                     idx_positive_cluster).reshape(
                                         channel_a.shape[0:2])
            io.imsave(fname=os.path.join(results_p_dir,
                                         f'03 K-means - Positives - Mask.jpg'),
                      arr=colormap(positive_mask))
            io.imsave(fname=os.path.join(
                results_p_dir, f'03 K-means - Positives - regions.jpg'),
                      arr=outline_regions(image=image,
                                          region_labels=positive_mask))
            io.imsave(fname=os.path.join(
                results_p_dir, f'03 K-means - Positives - average color.jpg'),
                      arr=average_color(image=image,
                                        region_labels=positive_mask))

            logging.info(
                'Negative separation (k-means clustering on `b channel`)')
            channel_b = image_lab[:, :, 2]
            nonpositive_mask = np.logical_not(positive_mask)
            clustering = KMeans(n_clusters=2, random_state=0).fit(
                channel_b[nonpositive_mask].reshape(-1, 1))
            idx_negative_cluster = np.argmin(clustering.cluster_centers_)
            negative_mask_wrt_nonpositive = np.equal(clustering.labels_,
                                                     idx_negative_cluster)
            negative_mask = np.full(shape=positive_mask.shape,
Esempio n. 6
0
            image_lab = rgb2lab(image)

            logging.info('Resizing')
            resize_factor = 8
            image_lab = resize(image_lab, (int(image.shape[0] / resize_factor),
                                           (image.shape[1] / resize_factor)),
                               anti_aliasing=True)

            all_mask = segmentation(image_lab,
                                    brown_lab=np.array([16.36, 110.89, 53.06]),
                                    blue_lab=np.array([18.10, 50.58, -70.44]),
                                    white_lab=np.array([53.15, -18.15, 1.20]),
                                    sigma=0.454)

            regions_positive = outline_regions(
                resized_original[5:resized_original.shape[0] - 5,
                                 5:resized_original.shape[1] - 5],
                all_mask[0, :, :])
            io.imsave(fname=os.path.join(results_p_dir,
                                         f'regions positive.jpg'),
                      arr=regions_positive)
            regions_negative = outline_regions(
                resized_original[5:resized_original.shape[0] - 5,
                                 5:resized_original.shape[1] - 5],
                all_mask[1, :, :])
            io.imsave(fname=os.path.join(results_p_dir,
                                         f'regions negative.jpg'),
                      arr=regions_negative)
            regions_background = outline_regions(
                resized_original[5:resized_original.shape[0] - 5,
                                 5:resized_original.shape[1] - 5],
                all_mask[2, :, :])
Esempio n. 7
0
            n = randint(0, 4)  # to try the new value on one of the five images
            masks = segmentation(image_lab_list[n],
                                 2,
                                 2,
                                 2,
                                 brown_lab=dictionary_arguments['brown_lab'],
                                 blue_lab=dictionary_arguments['blue_lab'],
                                 white_lab=dictionary_arguments['white_lab'])

            results_p_dir_bis = os.path.join(results_dir, "image", param_name,
                                             f'{result.x}')
            os.makedirs(results_p_dir_bis, exist_ok=True)

            regions_positive = outline_regions(
                image_original_list[n][10:image_original_list[n].shape[0] - 10,
                                       10:image_original_list[n].shape[1] -
                                       10], masks[0, :, :])
            io.imsave(fname=os.path.join(results_p_dir_bis,
                                         f'regions positive.jpg'),
                      arr=regions_positive)
            regions_negative = outline_regions(
                image_original_list[n][10:image_original_list[n].shape[0] - 10,
                                       10:image_original_list[n].shape[1] -
                                       10], masks[1, :, :])
            io.imsave(fname=os.path.join(results_p_dir_bis,
                                         f'regions negative.jpg'),
                      arr=regions_negative)
            regions_background = outline_regions(
                image_original_list[n][10:image_original_list[n].shape[0] - 10,
                                       10:image_original_list[n].shape[1] -
                                       10], masks[2, :, :])