示例#1
0
    def test_rotate_and_translate(self):
        rotation_angle = 30
        horizontal_shift = 50
        vertical_shift = 40
        points = np.asarray([[100., 200.]])
        test_image = self.get_test_image()

        shape = test_image.shape

        rotated_points = image_utils.rotate_points(
            points, rotation_angle=-rotation_angle)
        rotated_test_image = image_utils.rotate_image(
            test_image, rotation_angle=rotation_angle)
        translated_points = image_utils.translate_points(
            rotated_points,
            horizontal_shift=horizontal_shift,
            vertical_shift=vertical_shift)
        translated_test_image = image_utils.translate_image(
            rotated_test_image,
            horizontal_shift=horizontal_shift,
            vertical_shift=vertical_shift)

        scaled_points = image_utils.downscale_points(points, shape=shape)
        scaled_translated_points = image_utils.downscale_points(
            translated_points, shape=shape)

        test_image_points = image_utils.get_image_with_points(
            test_image, scaled_points)
        translated_test_image_points = image_utils.get_image_with_points(
            translated_test_image, scaled_translated_points)
        image_utils.show_multiple_images(
            [test_image_points, translated_test_image_points])
示例#2
0
    def straighten_chromosomes(chromosomes, save_dir=None, popup=None):
        num_chromosome = len(chromosomes)
        """ Handle popup """
        popup_counter = 1
        if popup is not None:
            popup.set_text("0/" + str(num_chromosome) +
                           " chromosomes processed.")

        straightened_chromosomes = list()
        for idx in range(num_chromosome):
            chromosome = chromosomes[idx].copy()
            gray = cv2.cvtColor(chromosome, cv2.COLOR_RGB2GRAY)
            _, binary = cv2.threshold(gray, 254, 255, cv2.THRESH_BINARY_INV)

            # get 2 end points of the best line
            x1, y1, x2, y2 = image_utils.find_best_line_hough_transform(binary)

            # make the first point higher than the second point
            if y1 > y2:
                x1, x2 = math_utils.swap(x1, x2)
                y1, y2 = math_utils.swap(y1, y2)

            angle = math_utils.get_angle_between_two_points([x1, y1], [x2, y2])

            straightened_chromosome = image_utils.rotate_image(
                chromosome, angle + 90)

            if save_dir is not None:
                general_utils.create_directory(save_dir)

                chromosome_with_line = cv2.line(chromosome.copy(), (x1, y1),
                                                (x2, y2), (0, 255, 0), 2)
                straightened_line = image_utils.rotate_points(
                    [[x1, y1], [x2, y2]], angle + 90, shape=chromosome.shape)
                rotated_x1, rotated_y1 = straightened_line[0]
                rotated_x2, rotated_y2 = straightened_line[1]
                straightened_chromosome_with_line = cv2.line(
                    straightened_chromosome.copy(), (rotated_x1, rotated_y1),
                    (rotated_x2, rotated_y2), (0, 255, 0), 2)
                """ Save images """
                cv2.imwrite(save_dir + "/" + str(idx) + ".bmp",
                            chromosome_with_line)
                cv2.imwrite(save_dir + "/" + str(idx) + "_straightened.bmp",
                            straightened_chromosome_with_line)

            straightened_chromosomes.append(straightened_chromosome)
            """ Handle popup """
            if popup is not None:
                popup.set_text(
                    str(popup_counter) + "/" + str(num_chromosome) +
                    " chromosomes processed.")
                popup_counter += 1

        return straightened_chromosomes
示例#3
0
    def test_generate_chromosome_cluster(self):
        image_dir = data_dir + "/chromosome/1"
        image_files = general_utils.get_all_files(image_dir)
        num_chromosome = 15
        contours = list()

        chromosomes = list()
        chosen_files = list()

        # error_case = ['/home/lntk/Desktop/Karyotype/data/chromosome/1/xx_karyotype_225_0.bmp',
        #               '/home/lntk/Desktop/Karyotype/data/chromosome/1/xy_karyotype_213_1.bmp',
        #               '/home/lntk/Desktop/Karyotype/data/chromosome/1/xy_karyotype_190_1.bmp',
        #               '/home/lntk/Desktop/Karyotype/data/chromosome/1/xx_karyotype_064_0.bmp',
        #               '/home/lntk/Desktop/Karyotype/data/chromosome/1/xx_karyotype_017_0.bmp',
        #               '/home/lntk/Desktop/Karyotype/data/chromosome/1/xy_karyotype_055_0.bmp',
        #               '/home/lntk/Desktop/Karyotype/data/chromosome/1/xy_karyotype_208_0.bmp',
        #               '/home/lntk/Desktop/Karyotype/data/chromosome/1/xy_karyotype_266_1.bmp',
        #               '/home/lntk/Desktop/Karyotype/data/chromosome/1/xy_karyotype_132_0.bmp',
        #               '/home/lntk/Desktop/Karyotype/data/chromosome/1/xy_karyotype_233_1.bmp']

        for _ in range(num_chromosome):
            """ Randomly get chromosome image from directory """
            idx = np.random.randint(len(image_files))
            chromosome = image_utils.read_image(image_dir + "/" +
                                                image_files[idx])
            chosen_files.append(image_dir + "/" + image_files[idx])
            # for image_file in error_case:
            #     chromosome = image_utils.read_image(image_file)
            """ Resize """
            chromosome = cv2.resize(chromosome, (64, 64))
            """ Rotate """
            angle = np.random.randint(90)
            rotated_chromosome = image_utils.rotate_image(chromosome, angle)
            """ Extract contour """
            contour = image_utils.get_chromosome_contour(rotated_chromosome)
            contours.append(contour)

            chromosomes.append(rotated_chromosome)

        print(chosen_files)

        contours, boxes, initial_boxes = chromosome_utils.generate_chromosome_cluster(
            contours)

        for contour in contours:
            print(contour.shape)

        # silhouette_image = chromosome_utils.get_chromosome_silhouette(contours, boxes)
        cluster_image = chromosome_utils.get_chromosome_cluster_image(
            boxes, initial_boxes, chromosomes)
        image_utils.show_multiple_images([cluster_image])
示例#4
0
def generate_image(image,
                   points=None,
                   rotation_angle=60,
                   horizontal_shift=10,
                   vertical_shift=10):
    r = np.random.randint(rotation_angle) + 1
    h = np.random.randint(horizontal_shift) + 1
    v = np.random.randint(vertical_shift) + 1
    new_image = image_utils.translate_image(image, h, v)
    new_image = image_utils.rotate_image(new_image, r)

    if points is None:
        return new_image
    else:
        new_points = learning_utils.points_augmentation(
            points, -r, h, v, image.shape)
        return new_image, new_points
示例#5
0
def data_augmentation(X,
                      y_skeleton,
                      y_clf,
                      rotation_angle,
                      horizontal_shift,
                      vertical_shift,
                      iteration=10):
    new_X = list()
    new_y_skeleton = list()
    new_y_clf = list()
    num_data = X.shape[0]
    data_shape = (X.shape[1], X.shape[2])

    # Add current data
    for idx in range(num_data):
        new_X.append(X[idx].copy())
        new_y_skeleton.append(y_skeleton[idx])
        new_y_clf.append(y_clf[idx])

    for i in range(iteration):
        for idx in range(num_data):
            image = X[idx].copy()
            class_id = y_clf[idx]
            points = y_skeleton[idx]

            # process image of X
            r = np.random.randint(rotation_angle) + 1
            h = np.random.randint(horizontal_shift) + 1
            v = np.random.randint(vertical_shift) + 1

            new_image = image_utils.translate_image(image, h, v)
            new_image = image_utils.rotate_image(new_image, r)
            new_X.append(new_image)

            # process points of y_skeleton
            new_points = points_augmentation(points, -r, h, v, data_shape)
            new_y_skeleton.append(new_points)

            # process class of y_clf
            new_y_clf.append(class_id)

    new_X = np.asarray(new_X)
    new_y_skeleton = np.asarray(new_y_skeleton)
    new_y_clf = np.asarray(new_y_clf)
    return new_X, new_y_skeleton, new_y_clf
示例#6
0
    def test_rotate_points(self):
        rotation_angle = 30
        points = np.asarray([[100., 200.]])
        test_image = self.get_test_image()

        shape = test_image.shape

        rotated_points = image_utils.rotate_points(
            points, rotation_angle=rotation_angle)
        rotated_test_image = image_utils.rotate_image(
            test_image, rotation_angle=rotation_angle)

        scaled_points = image_utils.downscale_points(points, shape=shape)
        scaled_rotated_points = image_utils.downscale_points(rotated_points,
                                                             shape=shape)

        test_image_points = image_utils.get_image_with_points(
            test_image, scaled_points)
        rotated_test_image_points = image_utils.get_image_with_points(
            rotated_test_image, scaled_rotated_points)
        image_utils.show_multiple_images(
            [test_image_points, rotated_test_image_points])