Beispiel #1
0
def save_color_norm_patches(dest_path, source_path, image_patch, img_norm,
                            color_norm_method):
    """
    The normalized image patches will be saved in the same folder structure
    as the original image patches.
    :param dest_path: the place to store the normalized image patches.
    :type dest_path:string
    :param source_path: the folder to store the original image patches.
    :type source_path: string
    :param file: the full path of the original image patch.
    :type file: string
    :param img_norm: the normalized image patch.
    :type img_norm: array
    :param color_norm_method: the method used for color normalization
    :type color_norm_method: string
    :return: None
    """
    file_full_path = osp.dirname(image_patch)
    relative_path = osp.relpath(file_full_path, source_path)
    path_to_create = osp.join(dest_path, relative_path)
    try:
        os.makedirs(path_to_create)
    except Exception:
        pass
    # print(image_patch)
    try:
        saveim(
            '%s/%s_%s.png' %
            (path_to_create, osp.splitext(
                osp.basename(image_patch))[0], color_norm_method), img_norm)
    except Exception:
        pass

    return None
Beispiel #2
0
    def extract_normal_patches_from_tumor_slide(self):
        """

        The actual function for tumor patch extraction from tumor slides.

        """
        n = 0
        # b=[]
        while n in range(0, 1000):
            # slide = openslide.open_slide(slide_paths[i])
            r = self.random_crop_tumor_normal()
            if (cv2.countNonZero(r[1]) > self.crop_size[0] *
                    self.crop_size[1] * 0.1) and (cv2.countNonZero(r[2])
                                                  == 0) and (n <= 1000):

                saveim(
                    '%s/%s_%d_%d_N.png' %
                    (self.des_folder_patches,
                     osp.splitext(
                         osp.basename(
                             self.single_slide_for_patch_extraction))[0],
                     r[3][0], r[3][1]), r[0])
                # io.imsave('/home/wli/Downloads/test/validation/nmask/%s_%d_%d_mask.png' % (
                # osp.splitext(osp.basename(slide_paths_total[i]))[0], r[3][0], r[3][1]), r[2])

                # b.append(r[3])
                # zz = testduplicates(b)

                n = n + 1

            else:
                n = n
Beispiel #3
0
    def extract_normal_patches_from_normal_slide(self):
        """
        The actual function for patch extraction from normal slides.

        """
        o = 0
        while o in range(0, 1000):
            nr = self.random_crop_normal()
            if (cv2.countNonZero(nr[1]) > self.crop_size[0] *
                    self.crop_size[1] * 0.1) and (o <= 1000):
                nmask = np.zeros((256, 256))

                saveim(
                    '%s/%s_%d_%d_N.png' %
                    (self.des_folder_patches,
                     osp.splitext(
                         osp.basename(
                             self.single_slide_for_patch_extraction))[0],
                     nr[2][0], nr[2][1]), nr[0])
                # io.imsave('/home/wli/Downloads/test/nmask/%s_%d_%d_mask.png' % (
                # osp.splitext(osp.basename(slide_paths_total[i]))[0], nr[2][0], nr[2][1]), nmask)

                # c.append(r[3])

                # zzz = testduplicates(c)
                o = o + 1

            else:
                o = o
Beispiel #4
0
def patch_aug_flip_rotate_crop_1_to_5(image,
                                      crop_size,
                                      image_name,
                                      folder_to_save=False):
    """
    The function generates 224 patches from 256 patches.
    each image patches will have 5 cropped and rotated image patches

    :param image: the original image
    :type image: array
    :param crop_size: the size of new image
    :type crop_size: list
    :param image_name: the name of original image
    :type image_name: string
    :param folder_to_save: the folder storing the new images

    :return: image list including 5 new images
    :rtype: list

    """
    image_list = []
    image_rotated1 = np.fliplr(image)
    image_rotated2 = np.rot90(image, 1)
    image_rotated3 = np.rot90(image, 2)
    image_rotated4 = np.rot90(image, 3)
    image_rotated5 = image

    # maskset = [imgmask, maskroted1, maskroted2, maskroted3, maskroted4]
    # imageset = [img_norm, imageroted1, imageroted2, imageroted3, imageroted4]

    dy, dx = crop_size
    x = np.random.randint(0, 256 - dx + 1)
    y = np.random.randint(0, 256 - dy + 1)
    # index = [x, y]
    # cropped_img = (image[x:(x+dx),y:(y+dy),:],rgb_binary[x:(x+dx),y:(y+dy)],
    # mask[x:(x+dx), y:(y+dy)])
    cropped_img1 = image_rotated1[x:(x + dx), y:(y + dy), :]
    image_list.append(cropped_img1)
    cropped_img2 = image_rotated2[x:(x + dx), y:(y + dy), :]
    image_list.append(cropped_img2)
    cropped_img3 = image_rotated3[x:(x + dx), y:(y + dy), :]
    image_list.append(cropped_img3)
    cropped_img4 = image_rotated4[x:(x + dx), y:(y + dy), :]
    image_list.append(cropped_img4)
    cropped_img5 = image_rotated5[x:(x + dx), y:(y + dy), :]
    image_list.append(cropped_img5)
    # cropped_binary = rgb_binary[x:(x+dx), y:(y+dy)]
    # cropped_mask = mask[x:(x + dx), y:(y + dy)]

    # print(image_list)
    if folder_to_save:
        for i in range(0, 5):
            saveim('%s/%s_aug.png' % (folder_to_save, image_name),
                   image_list[i])

    return image_list
Beispiel #5
0
def patch_aug_flip_rotate_crop(image,
                               crop_size,
                               image_name,
                               folder_to_save=False):
    """
    the function generates 224 patches from 256 patches

    :param image: the original image patch
    :type image: array
    :param crop_size: the sized of new image
    :type crop_size: list
    :param image_name: the name of the original image
    :type image_name: string
    :param folder_to_save: the folder to save the new images
    :type folder_to_save: string

    :return: cropped image
    :rtype: array

    """
    random_number = np.random.randint(0, 4)

    if random_number == 0:
        image_rotated = np.fliplr(image)
    elif random_number == 1:
        image_rotated = np.rot90(image, 1)
    elif random_number == 2:
        image_rotated = np.rot90(image, 2)
    elif random_number == 3:
        image_rotated = np.rot90(image, 3)
    else:
        image_rotated = image

    # maskset = [imgmask, maskroted1, maskroted2, maskroted3, maskroted4]
    # imageset = [img_norm, imageroted1, imageroted2, imageroted3, imageroted4]

    dy, dx = crop_size
    if image.shape[0] > dx:
        x = np.random.randint(0, 256 - dx + 1)
        y = np.random.randint(0, 256 - dy + 1)
        # index = [x, y]
        # cropped_img = (image[x:(x+dx),y:(y+dy),:],rgb_binary[x:(x+dx),y:(y+dy)],
        # mask[x:(x+dx), y:(y+dy)])
        cropped_img = image_rotated[x:(x + dx), y:(y + dy), :]
        # cropped_binary = rgb_binary[x:(x+dx), y:(y+dy)]
        # cropped_mask = mask[x:(x + dx), y:(y + dy)]
    else:
        cropped_img = image_rotated

    if folder_to_save:

        saveim('%s/%s_aug.png' % (folder_to_save, image_name), cropped_img)

    return cropped_img
Beispiel #6
0
    def extract_tumor_patches_from_tumor_slide(self):
        """
        The actual function for tumor patch extraction from tumor slides.

        """
        m = 0
        # a = []
        while m in range(0, 1000):
            r = self.random_crop_tumor_tumor()
            if (cv2.countNonZero(r[2]) > self.crop_size[0] *
                    self.crop_size[1] * 0.5) and (m <= 1000):

                saveim(
                    '%s/%s_%d_%d_T.png' %
                    (self.des_folder_patches,
                     osp.splitext(
                         osp.basename(
                             self.single_slide_for_patch_extraction))[0],
                     r[3][0], r[3][1]), r[0])

                io.imsave(
                    '%s/%s_%d_%d_T_mask.png' %
                    (self.des_folder_mask,
                     osp.splitext(
                         osp.basename(
                             self.single_slide_for_patch_extraction))[0],
                     r[3][0], r[3][1]), r[2])

                print(r[2])

                # a.append(r[3])
                # z = testduplicates(a)
                m = m + 1

            else:
                m = m
Beispiel #7
0
                #slide = openslide.open_slide(slide_paths_total[i])
                annotations = convert_xml_df(str(Anno_path_xml))
                x_values = list(annotations['X'].get_values())
                y_values = list(annotations['Y'].get_values())
                coord = zip(x_values, y_values)
                coord = list(coord)

                m = 0
                while m in range(0, len(coord)):
                    r = region_crop(slide, truth, crop_size, coord[m])
                    for n in range(0, 4):
                        # selectlly save the image patches only outside of tumor regions
                        if (cv2.countNonZero(r[n + 4]) == 0):

                            saveim(
                                '%s/%s_%d_%d.png' %
                                (new_folder,
                                 osp.splitext(
                                     osp.basename(slide_paths_total[i]))[0],
                                 r[8][n][0], r[8][n][1]), r[n])

                            # io.imsave('/Users/liw17/Documents/new pred/%s_%d_%d_mask.png' % (osp.splitext(
                            #    osp.basename(slide_paths_total[i]))[0], r[8][n][0], r[8][n][1]), r[n+4])

                            # print(r[n])

                        m = m + 5

                    # print(m)