예제 #1
0
파일: screen.py 프로젝트: lntk/karyotyping
    def load_images(self, image_dir):
        image_files = general_utils.get_all_files(image_dir)
        for image_file in image_files:
            parts = image_file[:-4].split("_")
            if parts[1] == "draw":
                continue

            idx = int(parts[0]) - 1
            self.blocks[idx].reload_image(source=image_dir + "/" + image_file)
예제 #2
0
def get_test_image():
    chromosome_dir = data_dir + "/chromosome"
    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"
    ]
    idx = np.random.randint(len(chromosome_ids))
    chromosome_dir = chromosome_dir + "/" + str(chromosome_ids[idx])
    image_files = general_utils.get_all_files(chromosome_dir)
    image_file = image_files[np.random.randint(len(image_files))]
    test_image = cv2.imread(chromosome_dir + "/" + image_file, 0)
    return test_image
예제 #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 directory_to_images_files_and_labels(directory, verbose=False):
    if verbose:
        print("Processing " + directory)

    relative_label_dirs = [x for x in next(os.walk(directory))[1]
                           ]  # get label names, such as "1", "2", ....

    label_to_id = {
        key: value
        for (value, key) in enumerate(relative_label_dirs)
    }  # mapping label to id (0, 1, ...)
    id_to_label = {
        key: value
        for (key, value) in enumerate(relative_label_dirs)
    }

    if verbose:
        print(label_to_id)

    image_ids, label_ids = list(), list()

    for label in relative_label_dirs:
        label_id = int(label_to_id[label])
        label_dir = directory + "/" + label

        if verbose:
            print("Processing " + label_dir)

        image_files = general_utils.get_all_files(label_dir)

        for image_file in image_files:
            image_file = label_dir + "/" + image_file
            image_ids.append(image_file)
            label_ids.append(label_id)

        if verbose:
            print("#Images so far: ", len(image_ids))

    if verbose:
        print("10 first image ids:", image_ids[:10])
        print("10 first label ids:", label_ids[:10])
        print("10 first label:", [id_to_label[idx] for idx in label_ids[:10]])

    return image_ids, label_ids, id_to_label, label_to_id
예제 #5
0
파일: screen.py 프로젝트: lntk/karyotyping
 def load_images(self, image_dir):
     image_files = general_utils.get_all_files(image_dir)
     for image_file in image_files:
         idx = int(image_file[:-4]) - 1
         self.blocks[idx].reload_image(source=image_dir + "/" + image_file)
예제 #6
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