class ContoursExtractionTest(TestBase):

    def setUp(self):
        super().setUp()
        self.processor = EdgeDetectionProcessor(debug_enabled=True)

    def _process(self, case_name, img):
        self.dump_debug_img(Path(case_name) / '01_input.png', img)

        edges = self.processor.process(img)

        self.dump_debug_img(Path(case_name) / '02_gaussed_grayscale.png', np.uint8(self.processor._debug_last_values['gaussed_grayscale']))
        self.dump_debug_img(Path(case_name) / '03_intencity_dx.png', np.uint8(127 + self.processor._debug_last_values['intensity_dx']))
        self.dump_debug_img(Path(case_name) / '04_intencity_dy.png', np.uint8(127 + self.processor._debug_last_values['intensity_dy']))
        self.dump_debug_img(Path(case_name) / '05_intencity_dnorm.png', np.uint8(self.processor._debug_last_values['intensity_dnorm']))
        self.dump_debug_img(Path(case_name) / '06_is_extremum.png', np.uint8(self.processor._debug_last_values['is_extremum']) * 255)
        self.dump_debug_img(Path(case_name) / '07_weak_edges.png', np.uint8(self.processor._debug_last_values['weak_edges']) * 255)
        self.dump_debug_img(Path(case_name) / '08_strong_edges.png', np.uint8(self.processor._debug_last_values['strong_edges']) * 255)

        self.dump_debug_img(Path(case_name) / '09_edges.png', edges * 255)
        contours = extract_contours(edges)

        pointsNumber = 0

        h, w = img.shape[:2]
        extracted_edges = np.zeros((h, w, 3), np.uint8)
        np.random.seed(239)
        for contour in contours:
            colour = np.random.randint(60, 255, (3,))
            draw_pixels(extracted_edges, contour, colour)
            pointsNumber += len(contour)
            self.assertTrue(np.all(edges == draw_pixels(edges.copy(), contour, 1)))  # assertion, that all pixels from contours are from edges
        self.dump_debug_img(Path(case_name) / '10_extracted_edges.png', extracted_edges)

        self.assertEqual(pointsNumber, len(edges.nonzero()[0]))

        return contours

    def lena_test(self):
        img = np.array(support.load_image(resources_dir_path / 'data' / 'lena.png'))
        edges = self._process('lena', img)

    def ellipse_test(self):
        w, h = 300, 150
        ellipse = shapes.Ellipse(w // 2, h // 2, w // 3, h // 3, 20)

        img_contour = np.zeros((h, w), np.uint8)
        ellipse.draw(img_contour, 255)
        self.dump_debug_img(Path('ellipse') / '00_ellipse.png', img_contour)

        img = np.zeros((h, w), np.uint8)
        ellipse.draw(img, 255, fill=True)

        edges = self._process('ellipse', img)
示例#2
0
class EdgeDetectionProcessorTest(TestBase):

    def setUp(self):
        super().setUp()
        self.processor = EdgeDetectionProcessor(debug_enabled=True)

    def _process(self, case_name, img):
        self.dump_debug_img(Path(case_name) / '1_input.png', img)

        edges = self.processor.process(img)

        self.dump_debug_img(Path(case_name) / '2_gaussed_grayscale.png', np.uint8(self.processor._debug_last_values['gaussed_grayscale']))
        self.dump_debug_img(Path(case_name) / '3_intencity_dx.png', np.uint8(127 + self.processor._debug_last_values['intensity_dx']))
        self.dump_debug_img(Path(case_name) / '4_intencity_dy.png', np.uint8(127 + self.processor._debug_last_values['intensity_dy']))
        self.dump_debug_img(Path(case_name) / '5_intencity_dnorm.png', np.uint8(self.processor._debug_last_values['intensity_dnorm']))
        self.dump_debug_img(Path(case_name) / '6_is_extremum.png', np.uint8(self.processor._debug_last_values['is_extremum']) * 255)
        self.dump_debug_img(Path(case_name) / '7_weak_edges.png', np.uint8(self.processor._debug_last_values['weak_edges']) * 255)
        self.dump_debug_img(Path(case_name) / '8_strong_edges.png', np.uint8(self.processor._debug_last_values['strong_edges']) * 255)

        self.dump_debug_img(Path(case_name) / '9_edges.png', edges * 255)
        return edges

    def lena_test(self):
        img = np.array(support.load_image(resources_dir_path / 'data' / 'lena.png'))
        edges = self._process('lena', img)

    def ellipse_test(self):
        w, h = 300, 150
        ellipse = shapes.Ellipse(w // 2, h // 2, w // 3, h // 3, 20)

        img_contour = np.zeros((h, w), np.uint8)
        ellipse.draw(img_contour, 255)
        self.dump_debug_img(Path('ellipse') / '0_ellipse.png', img_contour)

        img = np.zeros((h, w), np.uint8)
        ellipse.draw(img, 255, fill=True)

        edges = self._process('ellipse', img)

        edges_points = np.nonzero(edges)
        distances = []
        for y, x in zip(edges_points[0], edges_points[1]):
            distances.append(y)

            is_ok = False
            for dy in [-1, 0, 1]:
                for dx in [-1, 0, 1]:
                    if y > 0 and y < h - 1 and x > 0 and x < w - 1 and img_contour[y + dy, x + dx]:
                        is_ok = True

            self.assertTrue(is_ok)

        self.assertGreater(len(edges_points[0]), len(img_contour.nonzero()[0]) * 0.70)
        self.assertLess(len(edges_points[0]), len(img_contour.nonzero()[0]) * 1.3)
示例#3
0
 def setUp(self):
     super().setUp()
     self.processor = EdgeDetectionProcessor(debug_enabled=True)
示例#4
0
class ContoursExtractionTest(TestBase):
    def setUp(self):
        super().setUp()
        self.processor = EdgeDetectionProcessor(debug_enabled=True)

    def _process(self, case_name, img):
        self.dump_debug_img(Path(case_name) / '01_input.png', img)

        edges = self.processor.process(img)

        self.dump_debug_img(
            Path(case_name) / '02_gaussed_grayscale.png',
            np.uint8(self.processor._debug_last_values['gaussed_grayscale']))
        self.dump_debug_img(
            Path(case_name) / '03_intencity_dx.png',
            np.uint8(127 + self.processor._debug_last_values['intensity_dx']))
        self.dump_debug_img(
            Path(case_name) / '04_intencity_dy.png',
            np.uint8(127 + self.processor._debug_last_values['intensity_dy']))
        self.dump_debug_img(
            Path(case_name) / '05_intencity_dnorm.png',
            np.uint8(self.processor._debug_last_values['intensity_dnorm']))
        self.dump_debug_img(
            Path(case_name) / '06_is_extremum.png',
            np.uint8(self.processor._debug_last_values['is_extremum']) * 255)
        self.dump_debug_img(
            Path(case_name) / '07_weak_edges.png',
            np.uint8(self.processor._debug_last_values['weak_edges']) * 255)
        self.dump_debug_img(
            Path(case_name) / '08_strong_edges.png',
            np.uint8(self.processor._debug_last_values['strong_edges']) * 255)

        self.dump_debug_img(Path(case_name) / '09_edges.png', edges * 255)
        contours = extract_contours(edges)

        pointsNumber = 0

        h, w = img.shape[:2]
        extracted_edges = np.zeros((h, w, 3), np.uint8)
        np.random.seed(239)
        for contour in contours:
            colour = np.random.randint(60, 255, (3, ))
            draw_pixels(extracted_edges, contour, colour)
            pointsNumber += len(contour)
            self.assertTrue(
                np.all(edges == draw_pixels(edges.copy(), contour, 1))
            )  # assertion, that all pixels from contours are from edges
        self.dump_debug_img(
            Path(case_name) / '10_extracted_edges.png', extracted_edges)

        self.assertEqual(pointsNumber, len(edges.nonzero()[0]))

        return contours

    def lena_test(self):
        img = np.array(
            support.load_image(resources_dir_path / 'data' / 'lena.png'))
        edges = self._process('lena', img)

    def ellipse_test(self):
        w, h = 300, 150
        ellipse = shapes.Ellipse(w // 2, h // 2, w // 3, h // 3, 20)

        img_contour = np.zeros((h, w), np.uint8)
        ellipse.draw(img_contour, 255)
        self.dump_debug_img(Path('ellipse') / '00_ellipse.png', img_contour)

        img = np.zeros((h, w), np.uint8)
        ellipse.draw(img, 255, fill=True)

        edges = self._process('ellipse', img)