def testOffset(self): image = np.zeros([300, 200], dtype=np.int) image = draw_square_by_corner(image, 100, (150, 50), color=255) slices = mask_to_objects_2d(image, offset=(255, 320)) self.assertEqual(len(slices), 1) self.assertEqual(slices[0].label, 255) self.assertTrue(slices[0].polygon.equals(box(305, 470, 405, 570)), msg="Polygon is equal")
def testOtherPixels(self): mask = np.array([[0, 0, 0, 0, 0, 0], [0, 1, 1, 0, 1, 1], [0, 0, 1, 0, 1, 0], [0, 0, 0, 1, 0, 0], [0, 0, 0, 0, 0, 0]]) p1 = Polygon([(1, 1), (2, 2), (2, 1), (1, 1)]) p2 = Polygon([(4, 1), (4, 2), (5, 1), (4, 1)]) slices = mask_to_objects_2d(mask) self.assertEqual(len(slices), 2) self.assertTrue(slices[0].polygon.equals(p1)) self.assertTrue(slices[1].polygon.equals(p2))
def testExportOneSquare(self): image = np.zeros([300, 200], dtype=np.int) image = draw_square_by_corner(image, 100, (150, 50), color=255) slices = mask_to_objects_2d(image) self.assertEqual(len(slices), 1) self.assertEqual(slices[0].label, 255) self.assertTrue(slices[0].polygon.equals(box(50, 150, 150, 250)), msg="Polygon is equal")
def testAdjacentWithoutSeperation(self): image = np.zeros([300, 200], dtype=np.int) image = draw_square_by_corner(image, 50, (150, 50), color=255) image = draw_square_by_corner(image, 50, (150, 101), color=127) slices = mask_to_objects_2d(image) # sort by bounding box top left corner slices = sorted(slices, key=lambda s: s.polygon.bounds[:2]) self.assertEqual(len(slices), 2) self.assertEqual(slices[0].label, 255) self.assertEqual(slices[1].label, 127)
def testSmallObject(self): image = np.zeros([100, 100], dtype=np.int) image = draw_poly(image, Polygon([(15, 77), (15, 78), (16, 78), (15, 77)]), color=127) image = draw_poly(image, box(1, 1, 2, 2), color=255) slices = mask_to_objects_2d(image) # sort by bounding box top left corner slices = sorted(slices, key=lambda s: s.polygon.bounds[:2]) self.assertEqual(len(slices), 2) self.assertEqual(slices[0].label, 255) self.assertEqual(slices[1].label, 127)
def testStandalonePixels(self): mask = np.array([[0, 0, 0, 0, 0], [0, 1, 1, 0, 0], [0, 0, 1, 0, 0], [0, 0, 0, 1, 0], [0, 0, 0, 0, 0]]) rotated = [(mask, Polygon([(1, 1), (2, 2), (2, 1), (1, 1)])), (mask[::-1, :], Polygon([(2, 2), (1, 3), (2, 3), (2, 2)])), (mask[:, ::-1], Polygon([(2, 1), (2, 2), (3, 1), (2, 1)])), (mask[::-1, ::-1], Polygon([(2, 2), (2, 3), (3, 3), (2, 2)]))] for m, p in rotated: slices = mask_to_objects_2d(m) self.assertEqual(len(slices), 1) self.assertTrue(slices[0].polygon.equals(p))
def testSeveralObjects(self): image = np.zeros([300, 200], dtype=np.int) image = draw_square_by_corner(image, 50, (150, 50), color=255) image = draw_square_by_corner(image, 50, (205, 105), color=127) slices = mask_to_objects_2d(image) # sort by bounding box top left corner slices = sorted(slices, key=lambda s: s.polygon.bounds[:2]) self.assertEqual(len(slices), 2) self.assertEqual(slices[0].label, 255) self.assertTrue(slices[0].polygon.equals(box(50, 150, 100, 200)), msg="Polygon is equal") self.assertEqual(slices[1].label, 127) self.assertTrue(slices[1].polygon.equals(box(105, 205, 155, 255)), msg="Polygon is equal")
def testTwoPoints(self): mask = np.array([[0, 0, 0, 0], [0, 1, 1, 0], [0, 0, 0, 0]]) polygon = LineString([(1, 1), (2, 1)]) slices = mask_to_objects_2d(mask) self.assertEqual(len(slices), 1) self.assertTrue(slices[0].polygon.equals(polygon))