def _by_points(self, case_name, n, *, angle_min=0, angle_max=359, noise_error=0.0): 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(case_name) / '0_ellipse.png', img_contour) np.random.seed(239) angles = np.random.randint(angle_min, angle_max, (n, )) xys = np.float32(list(map(ellipse.calculate_point, angles))) xys += np.random.random_sample((len(xys), 2)) * noise_error img_points = img_contour // 3 draw_pixels(img_points, xys, 255) self.dump_debug_img(Path(case_name) / '1_points.png', img_points) estimation = fit_ellipse(xys) estimation.draw(img_points, 255) self.dump_debug_img(Path(case_name) / '2_estimation.png', img_points) self.assertTrue( np.all(np.abs(np.array(ellipse) - np.array(estimation)) < 3.0))
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 draw_pixels_test_grayscale_multi_points(self): w, h = 10, 6 img = np.zeros((h, w), np.uint8) img2 = shapes.draw_pixels(img, [[0, 0], [3, 0]], 1) self.assertIs(img2, img) self.assertEqual(img[0, 0], 1) self.assertEqual(img[0, 3], 1) self.assertEqual(img.sum(), 2) center_yx = [h//2, w//2] expected_img = shapes.draw_pixels(img.copy(), center_yx, 30) for x, y in [[0, -1], [-1, 0], [0, h + 1], [w + 1, 0]]: shapes.draw_pixels(img, [[x, y], center_yx], 30) self.assertTrue(np.all(img == expected_img))
def draw_pixels_test_color_one_point(self): w, h = 10, 6 img = np.zeros((h, w, 3), np.uint8) img2 = shapes.draw_pixels(img, [0, 0], (239, 0, 30)) self.assertIs(img2, img) self.assertTrue(np.all(img[0, 0] == [239, 0, 30])) shapes.draw_pixels(img, [w - 1, 0], (0, 200, 15)) self.assertTrue(np.all(img[0, w - 1] == [0, 200, 15])) for x, y in [[0, -1], [-1, 0], [0, h + 1], [w + 1, 0]]: img_copy = img.copy() shapes.draw_pixels(img_copy, [x, y], (124, 2, 6)) self.assertTrue(np.all(img == img_copy))
def draw_pixels_test_grayscale_one_point(self): w, h = 10, 6 img = np.zeros((h, w), np.uint8) img2 = shapes.draw_pixels(img, [0, 0], 239) self.assertIs(img2, img) self.assertEqual(img[0, 0], 239) shapes.draw_pixels(img, [w - 1, 0], 240) self.assertEqual(img[0, w - 1], 240) for x, y in [[0, -1], [-1, 0], [0, h + 1], [w + 1, 0]]: img_copy = img.copy() shapes.draw_pixels(img_copy, [x, y], 30) self.assertTrue(np.all(img == img_copy))
def draw_pixels_test_grayscale_multi_points(self): w, h = 10, 6 img = np.zeros((h, w), np.uint8) img2 = shapes.draw_pixels(img, [[0, 0], [3, 0]], 1) self.assertIs(img2, img) self.assertEqual(img[0, 0], 1) self.assertEqual(img[0, 3], 1) self.assertEqual(img.sum(), 2) center_yx = [h // 2, w // 2] expected_img = shapes.draw_pixels(img.copy(), center_yx, 30) for x, y in [[0, -1], [-1, 0], [0, h + 1], [w + 1, 0]]: shapes.draw_pixels(img, [[x, y], center_yx], 30) self.assertTrue(np.all(img == expected_img))
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 _by_points(self, case_name, n, *, angle_min=0, angle_max=359, noise_error=0.0): 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(case_name) / '0_ellipse.png', img_contour) np.random.seed(239) angles = np.random.randint(angle_min, angle_max, (n,)) xys = np.float32(list(map(ellipse.calculate_point, angles))) xys += np.random.random_sample((len(xys), 2)) * noise_error img_points = img_contour // 3 draw_pixels(img_points, xys, 255) self.dump_debug_img(Path(case_name) / '1_points.png', img_points) estimation = fit_ellipse(xys) estimation.draw(img_points, 255) self.dump_debug_img(Path(case_name) / '2_estimation.png', img_points) self.assertTrue(np.all(np.abs(np.array(ellipse) - np.array(estimation)) < 3.0))