Esempio n. 1
0
def view_sample_data_list(X, y, num_sample=5):
    for i in range(num_sample):
        idx = np.random.randint(len(X))
        image = cv2.imread(X[idx], 0)
        points = y[idx]
        image_utils.show_image(image_utils.get_image_with_points(
            image, points))
Esempio n. 2
0
 def test_get_block_image(self):
     chromosome1 = image_utils.read_image(
         data_dir + "/chromosome/1/xx_karyotype_001_0.bmp")
     chromosome2 = image_utils.read_image(
         data_dir + "/chromosome/1/xx_karyotype_001_1.bmp")
     image = image_utils.get_block_image([chromosome1, chromosome2], "1")
     image_utils.show_image(image)
Esempio n. 3
0
def view_sample_data(data, num_sample):
    X = data["X"]
    y_skeleton = data["y_skel"]
    y_clf = data["y_clf"]

    for i in range(num_sample):
        idx = np.random.randint(X.shape[0])
        image = X[idx].copy()
        image = np.stack((image, ) * 3, axis=-1)
        print(y_skeleton[idx])
        image_with_points = image_utils.get_image_with_points(
            image, y_skeleton[idx])

        print("Class: " + str(y_clf[idx] + 1))
        image_utils.show_image(image_with_points, cmap=None)
Esempio n. 4
0
    def test_denormalize_image(self):
        X_train = [test_utils.get_test_image() for _ in range(10)]
        X_val = [test_utils.get_test_image() for _ in range(1)]
        X_test = [test_utils.get_test_image() for _ in range(1)]

        image_utils.show_image(X_test[0], name="Before")

        X_train = np.asarray(X_train)
        X_val = np.asarray(X_val)
        X_test = np.asarray(X_test)

        X = {"train": X_train, "val": X_val, "test": X_test}

        X, norm_params = data_utils.normalize_data(X)
        image = X["test"][0].copy()
        image = data_utils.denormalize_image(image, norm_params)

        image_utils.show_image(image, name="After")
Esempio n. 5
0
def process_karyotyping_images(directory,
                               chromosome_type="xx",
                               debug=False,
                               load_karyotype_info=False,
                               max_p=None,
                               need_confirm=False):
    last_chromosome = chromosome_type[1]
    chromosome_ids = [
        1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20,
        21, 22, "x", "y"
    ]
    save_directory = directory + "/" + chromosome_type + "/chromosome"
    karyotype_directory = directory + "/" + chromosome_type

    if debug:
        print("Save directory: " + save_directory)
        print("Karyotype directory: " + karyotype_directory)

    general_utils.create_directory(save_directory)

    if not load_karyotype_info:
        # Create directories to store output images
        for idx in chromosome_ids:
            general_utils.create_directory(save_directory + "/" + str(idx))

        karyotypes = dict()
        image_files = general_utils.get_all_files(karyotype_directory)

        for image_file in image_files:
            if not image_file.endswith(".bmp"):
                continue
            if debug:
                print(image_file)

            image = cv2.imread(karyotype_directory + "/" + image_file, 0)
            # ret, thresh = threshold.partial_otsu_threshold(image, minval=0, maxval=255, dark_background=False)
            ret, thresh = cv2.threshold(image, 254, 255, cv2.THRESH_BINARY_INV)
            # if debug:
            #     image_utils.show_image(thresh)

            _, contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE,
                                                      cv2.CHAIN_APPROX_SIMPLE)

            num_contour = len(contours)

            # Find area threshold - taking the 46-th largest area
            areas = list()
            for idx in range(num_contour):
                is_outer_contour = hierarchy[0][idx][3] == -1
                if is_outer_contour:
                    area = cv2.contourArea(contours[idx])
                    areas.append(area)
            areas.sort(reverse=True)
            if len(areas) < 46:
                print("Wrong at " + image_file)
                continue
            area_threshold = areas[45] - 0.01

            chosen_contours = list()
            for idx in range(num_contour):
                contour = contours[idx]
                if cv2.contourArea(contour) < area_threshold:
                    continue
                chosen_contours.append(contour)

            if len(chosen_contours) < 46:
                print("Wrong at " + image_file)
                continue

            contour_info = list()
            for contour in chosen_contours:
                x, y, w, h = cv2.boundingRect(contour)
                contour_info.append([x, y, w, h, contour])

            def sorted_by(a, b):
                x_a, y_a, w_a, h_a, _ = a
                x_b, y_b, w_b, h_b, _ = b
                if (y_a + h_a) < y_b:
                    return -1
                if x_a < x_b:
                    return -1
                return 1

            cmp = functools.cmp_to_key(sorted_by)
            contour_info.sort(key=cmp)

            if need_confirm:
                rgb_image = np.stack((image, ) * 3, axis=-1)
                image_utils.show_image(image_utils.get_image_with_contours(
                    rgb_image, chosen_contours, thickness=-1),
                                       cmap=None)
                user_input = input()
                if "yes".startswith(user_input):
                    karyotypes[image_file] = contour_info
                else:
                    print("Skipping: " + image_file)
            else:
                rgb_image = np.stack((image, ) * 3, axis=-1)
                image_utils.show_image(image_utils.get_image_with_contours(
                    rgb_image, chosen_contours, thickness=-1),
                                       cmap=None)
                karyotypes[image_file] = contour_info

        # Save data in case of bugs
        pickle.dump(
            karyotypes,
            open(directory + "/" + chromosome_type + "_karyotype_info.data",
                 'wb'))
    else:
        with open(directory + "/" + chromosome_type + "_karyotype_info.data",
                  'rb') as f:
            karyotypes = pickle.load(f)

    # Find maximum perimeter
    if max_p is None:
        max_p = -1
        for idx in karyotypes.keys():
            contour_info = karyotypes[idx]

            if len(contour_info) != 46:
                print("Skipping:" + idx)
                continue

            con_1 = contour_info[0][4]
            con_2 = contour_info[1][4]
            p_1 = cv2.arcLength(con_1, True)
            p_2 = cv2.arcLength(con_2, True)
            max_p = max(p_1, max_p)
            max_p = max(p_2, max_p)

    if debug:
        print("Max p: " + str(max_p))

    # Resize all images according to max perimeter
    for image_file in karyotypes.keys():
        image = cv2.imread(karyotype_directory + "/" + image_file, 0)
        contour_info = karyotypes[image_file]
        if debug:
            print(image_file)

        if len(contour_info) != 46:
            print("Skipping:" + image_file)
            continue

        # Get max perimeter of two chromosome 1
        con_1 = contour_info[0][4]
        con_2 = contour_info[1][4]
        p_1 = cv2.arcLength(con_1, True)
        p_2 = cv2.arcLength(con_2, True)
        local_max_p = max(p_1, p_2)

        # Get scale according to above local max
        scale = max_p * 1.0 / local_max_p
        print(scale)

        component_size = 512
        counter = 1
        pixel_open = 0

        for idx in range(len(contour_info)):
            x, y, w, h, contour = contour_info[idx]

            # open the bounding box a little bit
            x -= pixel_open
            y -= pixel_open
            w += pixel_open
            h += pixel_open

            # create a white image of size 512
            white_image = 255 - np.zeros(shape=(component_size,
                                                component_size))

            # calculate corresponding top-left point in white image
            new_x = int((component_size - w) / 2)
            new_y = int((component_size - h) / 2)

            # copy bounding box patch from karyotype image into white image
            white_image[new_y:(new_y + h),
                        new_x:(new_x + w)] = image[y:(y + h), x:(x + w)]

            # rescale chromosome image
            white_image = cv2.resize(
                white_image,
                (int(component_size * scale), int(component_size * scale)))
            white_image = white_image.astype('uint8')

            # get the center patch of size 512
            white_image = image_utils.get_center_sub_image(white_image,
                                                           size=component_size)

            # get current chromosome name (1, 2, ..., x, y)
            chromosome_name = str(chromosome_ids[int(idx / 2)])
            # the last chromosome name depends on what karyotype image (xx or xy)
            if idx == 45:
                chromosome_name = last_chromosome

            # save the patch contain only 1 chromosome
            cv2.imwrite(
                save_directory + "/" + chromosome_name + "/" +
                chromosome_type + "_" + image_file + "_" + str(idx),
                white_image)
            counter += 1