def testRepresentativePoint(self):
        image = np.zeros([300, 200], dtype=np.uint8)
        image = draw_square_by_corner(image, 50, (150, 50), color=255)
        slices = mask_to_objects_2d(image)

        y, x = representative_point(slices[0].polygon, image, label=255)
        self.assertEqual(image[y, x], 255)
 def testTwoPoints(self):
     mask = np.array([[0, 0, 0, 0], [0, 1, 1, 0], [0, 0, 0, 0]],
                     dtype=np.uint8)
     polygon = Polygon([(1, 1), (1, 2), (3, 2), (3, 1), (1, 1)])
     slices = mask_to_objects_2d(mask)
     self.assertEqual(len(slices), 1)
     self.assertTrue(slices[0].polygon.equals(polygon))
    def testOffset(self):
        image = np.zeros([300, 200], dtype=np.uint8)
        image = draw_square_by_corner(image, 100, (150, 50), color=255)

        slices = mask_to_objects_2d(image, offset=(320, 255))

        self.assertEqual(len(slices), 1)
        self.assertEqual(slices[0].label, 255)
        self.assertTrue(slices[0].polygon.equals(box(305, 470, 406, 571)),
                        msg="Polygon is equal")
    def testExportOneSquare(self):
        image = np.zeros([300, 200], dtype=np.uint8)
        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, 151, 251)),
                        msg="Polygon is equal")
def extract_tiled_annotations(in_tiles, out_path, nj, label_merging=False):
    """
    in_images: iterable
        List of BiaflowsTile
    out_path: str
        Path of output tiles
    nj: BiaflowsJob
        A BIAflows job object
    label_merging: bool
        True for merging only polygons having the same label. False for merging based on geometry only
    """
    # regroup tiles by original images
    grouped_tiles = defaultdict(list)
    for in_tile in in_tiles:
        grouped_tiles[in_tile.in_image.original_filename].append(in_tile)

    default_tile_builder = DefaultTileBuilder()
    annotations = AnnotationCollection()
    for tiles in grouped_tiles.values():
        # recreate the topology
        in_image = tiles[0].in_image
        topology = BiaflowsSldcImage(in_image, is_2d=True).tile_topology(
            default_tile_builder,
            max_width=nj.flags["tile_width"],
            max_height=nj.flags["tile_height"],
            overlap=nj.flags["tile_overlap"])

        # extract polygons for each tile
        ids, polygons, labels = list(), list(), list()
        label = -1
        for tile in tiles:
            out_tile_path = os.path.join(out_path, tile.filename)
            slices = mask_to_objects_2d(imread(out_tile_path),
                                        offset=tile.tile.abs_offset[::-1])
            ids.append(tile.tile.identifier)
            polygons.append([s.polygon for s in slices])
            labels.append([s.label for s in slices])
            # save label for use after merging
            if len(slices) > 0:
                label = slices[0]

        # merge
        merged = SemanticMerger(tolerance=1).merge(
            ids, polygons, topology, labels=labels if label_merging else None)
        if label_merging:
            merged = merged[0]
        annotations.extend([
            create_annotation_from_slice(
                _slice=AnnotationSlice(p, label),
                id_image=in_image.object.id,
                image_height=in_image.object.height,
                id_project=nj.project.id,
            ) for p in merged
        ])
    return annotations
    def testAdjacentWithoutSeperation(self):
        image = np.zeros([300, 200], dtype=np.uint8)
        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)
示例#7
0
def skeleton_mask_to_objects_2d(mask, background=0, offset=None):
    """Process a 2d skeleton mask

    Parameters
    ----------
    mask: ndqrrqy
    background: int
    offset: tuple

    """
    dilated = morphology.dilation(mask, selem=morphology.selem.disk(2))
    return mask_to_objects_2d(dilated, background=background, offset=offset)
 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]],
         dtype=np.uint8)
     p1 = Polygon([(1, 1), (1, 2), (2, 2), (2, 3), (3, 3), (3, 1), (1, 1)])
     p2 = Polygon([(4, 1), (4, 3), (5, 3), (5, 2), (6, 2), (6, 1), (4, 1)])
     p3 = Polygon([(3, 3), (3, 4), (4, 4), (4, 3), (3, 3)])
     slices = mask_to_objects_2d(mask)
     self.assertEqual(len(slices), 3)
     self.assertTrue(slices[0].polygon.equals(p1))
     self.assertTrue(slices[1].polygon.equals(p2))
     self.assertTrue(slices[2].polygon.equals(p3))
    def testSmallObject(self):
        image = np.zeros([100, 100], dtype=np.uint8)
        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 testSeveralObjects(self):
        image = np.zeros([300, 200], dtype=np.uint8)
        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, 101, 201)),
                        msg="Polygon is equal")
        self.assertEqual(slices[1].label, 127)
        self.assertTrue(slices[1].polygon.equals(box(105, 205, 156, 256)),
                        msg="Polygon is equal")