def image_detach(color_img: np.ndarray, bin_num: int) -> np.ndarray:
    """
    컬러 이미지 `color_img`를 색상에 따른 각 인스턴스 객체로 분리합니다.

    Parameters
    ----------
    color_img : np.ndarray
        컬러 이미지
    bin_num : int
        분리할 최대 인스턴스 객체의 개수

    Returns
    -------
    np.ndarray
        `color_img`의 width, height에 `bin_num` channel인 `np.ndarray`

    Examples
    --------
    >>> import cv2
    >>> from utils.image_transform import image_detach
    >>> img = cv2.imread("../simple_image_generator/samples/label.png")
    >>> image_detach(img, 30)
    """
    color_list: List[Tuple[int, int, int]] = list(
        map(lambda v: v[0], get_all_colors(color_img, True)))
    id_color_list: List[Tuple[int,
                              Tuple[int, int,
                                    int]]] = color_map_generate(color_list)
    return image_detach_with_id_color_list(color_img, id_color_list, bin_num)
    def test_image_detach_with_id_color_list(self):
        """
        `image_detach_with_id_color_list()` 메서드를 테스트합니다.
        """
        # Prerequisite
        image: np.ndarray = self.__get_sample_image()
        image_colors: List[Tuple[int, int, int]] = list(
            map(lambda v: v[0], image_info.get_all_colors(image, True))
        )
        image_color_map: List[
            Tuple[int, Tuple[int, int, int]]
        ] = image_color_transform.color_map_generate(image_colors, True)

        # Processing
        bin_num: int = 30
        mask_value: float = 1.0
        detached_image: np.ndarray = (
            image_color_transform.image_detach_with_id_color_list(
                color_img=image,
                id_color_list=image_color_map[:bin_num],
                bin_num=bin_num,
                mask_value=mask_value,
            )
        )

        # Result
        test_result_non_zero_counts: int = 32400
        test_result_unique_values: Tuple[float, float] = [0.0, mask_value]

        # Check
        self.assertEqual(np.count_nonzero(detached_image), test_result_non_zero_counts)
        self.assertEqual(np.unique(detached_image).tolist(), test_result_unique_values)
    def test_color_map_generate_without_black(self):
        """
        `color_map_generate()` 메서드를 테스트합니다.

        검은 색을 추가하지 않는 옵션을 테스트합니다.
        """
        # Prerequisite
        image: np.ndarray = self.__get_lenna_image()
        image_colors: List[Tuple[int, int, int]] = list(
            map(lambda v: v[0], image_info.get_all_colors(image, True))
        )

        # Processing
        image_color_map: List[
            Tuple[int, Tuple[int, int, int]]
        ] = image_color_transform.color_map_generate(image_colors, False)

        # Result
        test_result_not_lenna_first_color_map: Tuple[Tuple[int, int, int]] = (
            0,
            (0, 0, 0),
        )

        # Check
        self.assertNotEqual(
            image_color_map[0][1], test_result_not_lenna_first_color_map
        )
Ejemplo n.º 4
0
 def test_get_levels_from_grayscale_image(self):
     test_file_name: str = "bone.png"
     test_image_fullpath: str = os.path.join(
         self.test_path, self.test_resource_folder_name, test_file_name
     )
     gray_image = cv2.imread(test_image_fullpath, cv2.IMREAD_GRAYSCALE)
     colors_on_image = image_info.get_all_colors(gray_image, True)
     self.assertEqual(colors_on_image[0][0], (0,))
Ejemplo n.º 5
0
 def test_get_all_colors__black_color_first(self):
     test_file_name: str = "bone.png"
     test_image_fullpath: str = os.path.join(
         self.test_path, self.test_resource_folder_name, test_file_name
     )
     image = cv2.imread(test_image_fullpath)
     colors_on_image: List[
         Tuple[Tuple[int, int, int], int]
     ] = image_info.get_all_colors(image, True)
     self.assertEqual(colors_on_image[0][0], (0, 0, 0))
Ejemplo n.º 6
0
 def test_get_all_colors(self):
     test_file_name: str = "lenna.png"
     lenna_colors: int = 148279
     test_image_fullpath: str = os.path.join(
         self.test_path, self.test_resource_folder_name, test_file_name
     )
     image = cv2.imread(test_image_fullpath)
     colors_on_image: List[
         Tuple[Tuple[int, int, int], int]
     ] = image_info.get_all_colors(image, True)
     self.assertEqual(len(colors_on_image), lenna_colors)
    def test_image_detach_with_id_color_probability_list(self):
        """
        `image_detach_with_id_color_probability_list()` 메서드를 테스트합니다.
        """
        # Prerequisite
        image: np.ndarray = self.__get_sample_image()
        image_colors: List[Tuple[int, int, int]] = list(
            map(lambda v: v[0], image_info.get_all_colors(image, True))
        )
        image_color_map: List[
            Tuple[int, Tuple[int, int, int]]
        ] = image_color_transform.color_map_generate(image_colors, True)

        # Processing
        bin_num: int = 30
        detached_image: np.ndarray = (
            image_color_transform.image_detach_with_id_color_probability_list(
                color_img=image,
                id_color_list=image_color_map[:bin_num],
                bin_num=bin_num,
                resize_by_power_of_two=2,
            )
        )

        # Result
        test_result_non_zero_counts: int = 2189
        test_result_unique_values: List[Tuple[float, int]] = [
            (0.0, 58561),
            (0.0625, 8),
            (0.25, 152),
            (0.4375, 8),
            (0.5, 76),
            (1.0, 1945),
        ]

        # Check
        self.assertEqual(np.count_nonzero(detached_image), test_result_non_zero_counts)
        self.assertEqual(
            np.unique(detached_image).tolist(),
            list(map(lambda v: v[0], test_result_unique_values)),
        )
        for value in test_result_unique_values:
            self.assertEqual(
                np.count_nonzero(np.equal(detached_image, value[0])), value[1]
            )
    def test_color_map_generate_for_add_black(self):
        """
        `color_map_generate()` 메서드를 테스트합니다.

        검은 색이 없는 경우, 검은 색을 맨 앞으로 추가하는 옵션을 테스트합니다.
        """
        # Prerequisite
        image: np.ndarray = self.__get_lenna_image()
        image_colors: List[Tuple[int, int, int]] = list(
            map(lambda v: v[0], image_info.get_all_colors(image, True))
        )

        # Processing
        image_color_map: List[
            Tuple[int, Tuple[int, int, int]]
        ] = image_color_transform.color_map_generate(image_colors, True)

        # Result
        test_result_lenna_color_num: int = 148280
        test_result_lenna_first_color_map: Tuple[Tuple[int, int, int]] = (0, (0, 0, 0))

        # Check
        self.assertEqual(len(image_color_map), test_result_lenna_color_num)
        self.assertEqual(image_color_map[0], test_result_lenna_first_color_map)