Ejemplo n.º 1
0
    def unify_chromosomes_parts(self, out_path):
        img1 = common_operations.read_image(self.best_img1_path)
        img2 = common_operations.read_image(self.best_img2_path)
        if self.best_angle_2 > 90:
            img2 = np.flipud(img2)
            cut2 = 180 - self.best_angle_2
        else:
            cut2 = self.best_angle_2
        cut2 //= 8
        if self.img2.shape[0] > img2.shape[0] - cut2:
            cut2 = max(0, self.img2.shape[0] - img2.shape[0])

        if self.best_angle_1 > 90:
            img1 = np.flipud(img1)
            cut1 = 180 - self.best_angle_1
        else:
            cut1 = self.best_angle_1
        cut1 //= 8
        if self.img1.shape[0] > img1.shape[0] - cut1:
            cut1 = max(0, self.img1.shape[0] - img1.shape[0])
        print(img1.shape)
        print(img2.shape)
        h = int(img1.shape[0]) + int(img2.shape[0])
        if h > cut1 + cut2:
            h = h - cut1 - cut2
        offset1, offset2 = self.__get_imgs_offset(img1, img2, cut1, cut2)
        print("         Img1   Img2")
        print("Shape: %s   %s" % (str(img1.shape), str(img2.shape)))
        print("Cut: %d   %d" % (cut1, cut2))
        print("Offset: %d   %d" % (offset1, offset2))
        w = max(img1.shape[1] + offset1, img2.shape[1] + offset2)
        print("h=%d" % h)
        print("w=%d" % w)
        img3 = np.zeros((h, w, 3))
        img3[:, :] = (255, 255, 255)
        start_row_offset_for_2nd_img = img1.shape[0] - cut1
        if cut1 > 0:
            img3[:img1.shape[0] - cut1,
                 offset1:img1.shape[1] + offset1] = img1[:-cut1, :]
        else:
            img3[:img1.shape[0], offset1:img1.shape[1] + offset1] = img1[:, :]
            start_row_offset_for_2nd_img = img1.shape[0]
        if cut2 > 0:
            img3[start_row_offset_for_2nd_img:,
                 offset2:img2.shape[1] + offset2] = img2[cut2:, :]
        else:
            img3[start_row_offset_for_2nd_img:,
                 offset2:img2.shape[1] + offset2] = img2[:, :]
        cv2.imwrite(out_path, img3)
        print("========================================================")
Ejemplo n.º 2
0
 def __init__(self, input_image_path, invert=False):
     self.image_path = input_image_path
     self.image = common_operations.read_image(self.image_path, invert)
     self.segments = list()
     self.colored_image_path = self._get_colored_image_path()
     self.logger = LOGGER.getChild("segmentation")
     self.__color_index = 0
     self.__current_color = [20, 0, 0]
     self.__current_color_index_to_increment = 1
Ejemplo n.º 3
0
    def __get_chromosome_len(self, file):
        """

        :param file: straighten image file
        :return: chromosome length
        """
        if self.__straighten:
            img = common_operations.read_image(file)
            return img.shape[0]
        else:
            return self.__get_chromosome_len_using_curve_len(file)
Ejemplo n.º 4
0
 def is_curved(self, image_to_check_path):
     self.image_path = image_to_check_path
     image = common_operations.read_image(self.image_path)
     compute_projection_vector_obj = compute_projection_vector.ComputeProjectionVector(
     )
     h_vector = compute_projection_vector_obj.get_horizontal_projection_vector(
         self.image_path)
     ch_area = sum(h_vector)
     total_area = image.shape[0] * image.shape[1]
     proc = ch_area / total_area
     if proc < 0.6:
         return True
     return False
Ejemplo n.º 5
0
 def __init__(self,
              orig_img_path,
              colored_img_path,
              segments_list,
              inverted=False):
     self.logger = LOGGER.getChild("segmentation")
     self.orig_image_path = orig_img_path
     self.colored_image_path = colored_img_path
     self.segments_list: List[ChromosomeFrame] = segments_list
     self.orig_image = common_operations.read_image(self.orig_image_path,
                                                    inverted)
     self.colored_image = common_operations.read_image(
         self.colored_image_path, inverted)
     self.output_dir = os.path.join(
         os.path.dirname(self.orig_image_path),
         ".".join(os.path.basename(self.orig_image_path).split(".")[:-1]) +
         "_split")
     self.individual_dir = os.path.join(self.output_dir, "individual")
     if not os.path.exists(self.output_dir):
         os.makedirs(self.output_dir)
     if not os.path.exists(self.individual_dir):
         os.makedirs(self.individual_dir)
Ejemplo n.º 6
0
 def __get_chromosome_area(self, file):
     """
     :param file: straighten image file
     :return: chromosome area
     """
     colored_individual_ch_file_path = self.__get_individual_colored_path_from_individual_straighten_path(
         file)
     img = common_operations.read_image(colored_individual_ch_file_path)
     area = 0
     for i in img:
         for j in i:
             if any(j != 0):
                 area += 1
     return area
 def get_horizontal_projection_vector(self, image_path):
     self.img_path = image_path
     image = common_operations.read_image(self.img_path)
     gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
     ret, img1 = cv2.threshold(gray, 245, 255, cv2.THRESH_BINARY_INV)
     h_projection_vector = list()
     for i in range(img1.shape[0]):
         value = 0
         for j in range(img1.shape[1]):
             if img1[i][j] == 0:
                 continue
             value += 1
         h_projection_vector.append(value)
     return h_projection_vector
Ejemplo n.º 8
0
    def is_valid_curve(curve_path):
        img = common_operations.read_image(curve_path)

        ret, threshold_img = cv2.threshold(img, 245, 255, cv2.THRESH_BINARY)
        k = 0
        x_points, y_points = list(), list()
        for i in range(threshold_img.shape[0]):
            for j in range(threshold_img.shape[1]):
                if all(threshold_img[i][j] == 255):
                    x_points.append(i)
                    y_points.append(j)
                k += 1
        if len(set(x_points)) <= img.shape[0] / VALID_CURVE_POINT_PERCENT \
                or len(set(y_points)) <= img.shape[1] / VALID_CURVE_POINT_PERCENT:
            return False
        return True
Ejemplo n.º 9
0
    def __compute_polynomial_function(self):
        if self.__img is None:
            self.__img = common_operations.read_image(self.__img_path)

        ret, self.orig_threshold_img = cv2.threshold(self.__img, 245, 255,
                                                     cv2.THRESH_BINARY)
        ret, self.threshold_img = cv2.threshold(self.__img, 245, 255,
                                                cv2.THRESH_BINARY)
        k = 0
        for i in range(self.threshold_img.shape[0]):
            for j in range(self.threshold_img.shape[1]):
                if all(self.threshold_img[i][j] == 255):
                    self.x_points.append(i)
                    self.y_points.append(j)
                k += 1
        if len(set(self.x_points)) == 1 or len(set(self.y_points)) == 1:
            self.__polynomial_function_grade = 1

        self.polynomial_function = np.poly1d(
            np.polyfit(self.x_points, self.y_points,
                       self.__polynomial_function_grade))
Ejemplo n.º 10
0
    def straight_curved_chromosome(self, image_path, cut_point,
                                   ready_dir_path):
        self.image_path = image_path
        self.ready_dir_path = ready_dir_path
        self.img = common_operations.read_image(self.image_path)
        self.img1 = self.img[:cut_point, :]
        self.img2 = self.img[cut_point:, :]
        img_dir_path = os.path.join(
            os.path.dirname(self.image_path),
            os.path.splitext(os.path.basename(self.image_path))[0])
        img1_dir_path = os.path.join(img_dir_path, "img1")
        img2_dir_path = os.path.join(img_dir_path, 'img2')
        if not os.path.exists(img_dir_path):
            os.makedirs(img_dir_path)
        if not os.path.exists(img1_dir_path):
            os.makedirs(img1_dir_path)
        if not os.path.exists(img2_dir_path):
            os.makedirs(img2_dir_path)
        self.img1_path = os.path.join(
            img1_dir_path,
            os.path.splitext(os.path.basename(self.image_path))[0] + "_1" +
            os.path.splitext(os.path.basename(self.image_path))[1])
        self.img2_path = os.path.join(
            img2_dir_path,
            os.path.splitext(os.path.basename(self.image_path))[0] + "_2" +
            os.path.splitext(os.path.basename(self.image_path))[1])

        cv2.imwrite(self.img1_path, self.img1)
        cv2.imwrite(self.img2_path, self.img2)

        self.best_img1_path, self.best_angle_1 = self.straight_half_chromosome(
            self.img1_path)
        self.best_img2_path, self.best_angle_2 = self.straight_half_chromosome(
            self.img2_path)
        self.unify_chromosomes_parts(
            os.path.join(self.ready_dir_path,
                         os.path.basename(self.image_path)))
Ejemplo n.º 11
0
 def __trim_and_save_image(image_path, x1, x2, y1, y2):
     image = common_operations.read_image(image_path)
     new_image = image[y1:y2, x1:x2]
     cv2.imwrite(image_path, new_image)