Exemplo n.º 1
0
    def test_shift_real(self):
        """ Test on decomposed image with known shift """
        numTiles = [2, 3, 4]
        overlap = [0.5, 0.4, 0.3, 0.2]
        acq = ["horizontalLines", "horizontalZigzag", "verticalLines"]

        for img, num, o, a in itertools.product(IMGS, numTiles, overlap, acq):
            _, img_name = os.path.split(img)
            conv = find_fittest_converter(img)
            data = conv.read_data(img)[0]
            data = ensure2DImage(data)

            # Create artificial tiled image
            [tiles, real_pos] = decompose_image(data, o, num, a)
            px_size = tiles[0].metadata[model.MD_PIXEL_SIZE]
            registrar = GlobalShiftRegistrar()

            # Register tiles
            for tile in tiles:
                registrar.addTile(tile)
            # Compare positions to real positions, allow 5 px offset
            registered_pos = registrar.getPositions()[0]
            diff = numpy.absolute(numpy.subtract(registered_pos, real_pos))
            allowed_px_offset = numpy.repeat(numpy.multiply(px_size, 5), len(diff))
            numpy.testing.assert_array_less(diff.flatten(), allowed_px_offset.flatten(),
                        "Position %s pxs off for image '%s', " % (max(diff.flatten()) / px_size[0], img_name) +
                        "%s x %s tiles, %s ovlp, %s method." % (num, num, o, a))
Exemplo n.º 2
0
    def test_real_perfect_overlap(self):
        """
        Test on decomposed image
        """

        numTiles = [2, 3, 4]
        overlap = [0.4]

        for img in IMGS:
            conv = find_fittest_converter(img)
            data = conv.read_data(img)[0]
            img = ensure2DImage(data)

            for n in numTiles:
                for o in overlap:
                    [tiles, _] = decompose_image(img, o, n, "horizontalZigzag",
                                                 False)

                    weaver = CollageWeaverReverse()
                    for t in tiles:
                        weaver.addTile(t)

                    sz = len(weaver.getFullImage())
                    w = weaver.getFullImage()

                    numpy.testing.assert_allclose(w, img[:sz, :sz], rtol=1)
Exemplo n.º 3
0
    def test_shift_real(self):
        """ Test on decomposed image with known shift """
        numTiles = [2, 3, 4]
        overlap = [0.5, 0.4, 0.3, 0.2]
        acq = ["horizontalLines", "horizontalZigzag", "verticalLines"]

        for img, num, o, a in itertools.product(IMGS, numTiles, overlap, acq):
            _, img_name = os.path.split(img)
            conv = find_fittest_converter(img)
            data = conv.read_data(img)[0]
            data = ensure2DImage(data)

            # Create artificial tiled image
            [tiles, real_pos] = decompose_image(data, o, num, a)
            px_size = tiles[0].metadata[model.MD_PIXEL_SIZE]
            registrar = GlobalShiftRegistrar()

            # Register tiles
            for tile in tiles:
                registrar.addTile(tile)
            # Compare positions to real positions, allow 5 px offset
            registered_pos = registrar.getPositions()[0]
            diff = numpy.absolute(numpy.subtract(registered_pos, real_pos))
            allowed_px_offset = numpy.repeat(numpy.multiply(px_size, 5),
                                             len(diff))
            numpy.testing.assert_array_less(
                diff.flatten(), allowed_px_offset.flatten(),
                "Position %s pxs off for image '%s', " %
                (max(diff.flatten()) / px_size[0], img_name) +
                "%s x %s tiles, %s ovlp, %s method." % (num, num, o, a))
Exemplo n.º 4
0
    def test_shift_real(self):
        """ Test on decomposed image with known shift """
        numTiles = [2, 3]
        overlap = [0.2, 0.3, 0.4]
        acq = ["horizontalLines", "verticalLines", "horizontalZigzag"]

        for img in IMGS:
            conv = find_fittest_converter(img)
            data = conv.read_data(img)[0]
            data = ensure2DImage(data)
            for num in numTiles:
                for o in overlap:
                    for a in acq:
                        [tiles, pos] = decompose_image(data, o, num, a, False)
                        registrar = IdentityRegistrar()
                        for i in range(len(pos)):
                            registrar.addTile(tiles[i])
                            calculatedPositions = registrar.getPositions()[0]
                            diff1 = abs(calculatedPositions[i][0] - pos[i][0])
                            diff2 = abs(calculatedPositions[i][1] - pos[i][1])
                            # allow difference of 10% of overlap
                            px_size = tiles[i].metadata[model.MD_PIXEL_SIZE]
                            # allow error of 1% of tileSize
                            margin1 = 0.01 * tiles[i].shape[0] * px_size[0]
                            margin2 = 0.01 * tiles[i].shape[1] * px_size[1]

                            self.assertLessEqual(diff1, margin1,
                                                 "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                                                 " %f != %f" % (calculatedPositions[i][0], pos[i][0]))
                            self.assertLessEqual(diff2, margin2,
                                                 "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                                                 " %f != %f" % (calculatedPositions[i][1], pos[i][1]))
Exemplo n.º 5
0
    def test_no_seam(self):
        """
        Test on decomposed image
        """

        numTiles = [2, 3, 4]
        overlap = [0.4]
        for img in IMGS:
            conv = find_fittest_converter(img)
            data = conv.read_data(img)[0]
            img = ensure2DImage(data)
            for n in numTiles:
                for o in overlap:
                    [tiles, _] = decompose_image(img, o, n, "horizontalZigzag",
                                                 False)

                    weaver = CollageWeaver(adjust_brightness=False)
                    for t in tiles:
                        weaver.addTile(t)

                    sz = len(weaver.getFullImage())
                    w = weaver.getFullImage()

                    numpy.testing.assert_array_almost_equal(w,
                                                            img[:sz, :sz],
                                                            decimal=1)
Exemplo n.º 6
0
    def test_real_perfect_overlap(self):
        """
        Test on decomposed image
        """

        numTiles = [2, 3, 4]
        overlap = [0.4]

        for img in IMGS:
            conv = find_fittest_converter(img)
            data = conv.read_data(img)[0]
            img = ensure2DImage(data)

            for n in numTiles:
                for o in overlap:
                    [tiles, _] = decompose_image(
                        img, o, n, "horizontalZigzag", False)

                    weaver = CollageWeaverReverse()
                    for t in tiles:
                        weaver.addTile(t)

                    sz = len(weaver.getFullImage())
                    w = weaver.getFullImage()

                    numpy.testing.assert_allclose(w, img[:sz, :sz], rtol=1)
Exemplo n.º 7
0
    def test_shift_real(self):
        """ Test on decomposed image with known shift """
        numTiles = [2, 3]
        overlap = [0.2, 0.3, 0.4]
        acq = ["horizontalLines", "verticalLines", "horizontalZigzag"]

        for img in IMGS:
            conv = find_fittest_converter(img)
            data = conv.read_data(img)[0]
            data = ensure2DImage(data)
            for num in numTiles:
                for o in overlap:
                    for a in acq:
                        [tiles, pos] = decompose_image(data, o, num, a, False)
                        registrar = IdentityRegistrar()
                        for i in range(len(pos)):
                            registrar.addTile(tiles[i])
                            calculatedPositions = registrar.getPositions()[0]
                            diff1 = abs(calculatedPositions[i][0] - pos[i][0])
                            diff2 = abs(calculatedPositions[i][1] - pos[i][1])
                            # allow difference of 10% of overlap
                            px_size = tiles[i].metadata[model.MD_PIXEL_SIZE]
                            # allow error of 1% of tileSize
                            margin1 = 0.01 * tiles[i].shape[0] * px_size[0]
                            margin2 = 0.01 * tiles[i].shape[1] * px_size[1]

                            self.assertLessEqual(
                                diff1, margin1,
                                "Failed for %s tiles, %s overlap and %s method,"
                                % (num, o, a) + " %f != %f" %
                                (calculatedPositions[i][0], pos[i][0]))
                            self.assertLessEqual(
                                diff2, margin2,
                                "Failed for %s tiles, %s overlap and %s method,"
                                % (num, o, a) + " %f != %f" %
                                (calculatedPositions[i][1], pos[i][1]))
Exemplo n.º 8
0
    def test_dependent_tiles(self):
        """ Tests functionality for dependent tiles """

        # Test on 3 layers of the same image create by decompose_image
        for img in IMGS:
            conv = find_fittest_converter(img)
            data = conv.read_data(img)[0]
            img = ensure2DImage(data)
            num = 4
            o = 0.3  # fails for 0.2
            a = "horizontalZigzag"
            [tiles, pos] = decompose_image(img, o, num, a)
            registrar = GlobalShiftRegistrar()
            for i in range(len(pos)):
                registrar.addTile(tiles[i], (tiles[i], tiles[i]))
            tile_pos, dep_tile_pos = registrar.getPositions()
            for i in range(len(pos)):
                diff1 = abs(tile_pos[i][0] - pos[i][0])
                diff2 = abs(tile_pos[i][1] - pos[i][1])
                # allow difference of 10% of overlap
                px_size = tiles[i].metadata[model.MD_PIXEL_SIZE]

                # Check if position is not completely wrong. The margins are given
                # by the extreme value calculation in the registrar and provide
                # a very generous upper limit for the error that should never be exceeded
                # because of the fallback method.
                # Unfortunately, many tests don't pass stricter limits yet.
                margin1 = px_size[0] * 5
                margin2 = px_size[1] * 5

                self.assertLessEqual(
                    diff1, margin1,
                    "Failed for %s tiles, %s overlap and %s method," %
                    (num, o, a) + " %f != %f" % (tile_pos[i][0], pos[i][0]))
                self.assertLessEqual(
                    diff2, margin2,
                    "Failed for %s tiles, %s overlap and %s method," %
                    (num, o, a) + " %f != %f" % (tile_pos[i][1], pos[i][1]))

            for p, tile in zip(pos, dep_tile_pos):
                for dep_tile in tile:
                    diff1 = abs(dep_tile[0] - p[0])
                    self.assertLessEqual(
                        diff1, margin1,
                        "Failed for %s tiles, %s overlap and %s method," %
                        (num, o, a) + " %f != %f" % (dep_tile[0], p[0]))

                    diff2 = abs(dep_tile[1] - p[1])
                    self.assertLessEqual(
                        diff2, margin2,
                        "Failed for %s tiles, %s overlap and %s method," %
                        (num, o, a) + " %f != %f" % (dep_tile[1], p[1]))

            # Test with shifted dependent tiles
            [tiles, pos] = decompose_image(img, o, num, a)

            registrar = GlobalShiftRegistrar()

            # Add different shift for every dependent tile
            dep_tiles = copy.deepcopy(tiles)
            # x-shift for each dependent tile in px
            rnd1 = [random.randrange(-1000, 1000) for _ in range(len(pos))]
            # y-shift for each dependent tile in px
            rnd2 = [random.randrange(-1000, 1000) for _ in range(len(pos))]
            # Change metadata of dependent tiles
            for i in range(len(dep_tiles)):
                p = (dep_tiles[i].metadata[model.MD_POS][0] +
                     rnd1[i] * px_size[0],
                     dep_tiles[i].metadata[model.MD_POS][1] +
                     rnd2[i] * px_size[1])
                dep_tiles[i].metadata[model.MD_POS] = p

            for i in range(len(pos)):
                # Register tiles
                # 2 layers of dependent tiles with the same pos
                registrar.addTile(tiles[i], (dep_tiles[i], dep_tiles[i]))
            tile_pos, dep_tile_pos = registrar.getPositions()
            for i in range(len(pos)):
                # Test main tile
                diff1 = abs(tile_pos[i][0] - pos[i][0])
                diff2 = abs(tile_pos[i][1] - pos[i][1])
                margin1 = px_size[0] * 5
                margin2 = px_size[1] * 5
                self.assertLessEqual(
                    diff1, margin1,
                    "Failed for %s tiles, %s overlap and %s method," %
                    (num, o, a) + " %f != %f" % (tile_pos[i][0], pos[i][0]))
                self.assertLessEqual(
                    diff2, margin2,
                    "Failed for %s tiles, %s overlap and %s method," %
                    (num, o, a) + " %f != %f" % (tile_pos[i][0], pos[i][0]))

            for p, tile, r1, r2 in zip(tile_pos, dep_tile_pos, rnd1, rnd2):
                for dep_tile in tile:
                    self.assertAlmostEqual(dep_tile[0], p[0] + r1 * px_size[0])
                    self.assertAlmostEqual(dep_tile[1], p[1] + r2 * px_size[1])
Exemplo n.º 9
0
    def test_dependent_tiles(self):
        """ Tests functionality for dependent tiles """

        # Test on 3 layers of the same image create by decompose_image
        for img in IMGS:
            conv = find_fittest_converter(img)
            data = conv.read_data(img)[0]
            img = ensure2DImage(data)
            num = 2
            o = 0.2
            a = "horizontalZigzag"
            [tiles, pos] = decompose_image(img, o, num, a, False)
            registrar = IdentityRegistrar()
            for i in range(len(pos)):
                registrar.addTile(tiles[i], (tiles[i], tiles[i]))
                tile_pos, dep_tile_pos = registrar.getPositions()

                diff1 = abs(tile_pos[i][0] - pos[i][0])
                diff2 = abs(tile_pos[i][1] - pos[i][1])
                # allow difference of 10% of overlap
                px_size = tiles[i].metadata[model.MD_PIXEL_SIZE]
                margin = 0.01 * tiles[i].shape[0] * px_size[0]
                self.assertLessEqual(
                    diff1, margin,
                    "Failed for %s tiles, %s overlap and %s method," %
                    (num, o, a) + " %f != %f" % (tile_pos[i][0], pos[i][0]))
                self.assertLessEqual(
                    diff2, margin,
                    "Failed for %s tiles, %s overlap and %s method," %
                    (num, o, a) + " %f != %f" % (tile_pos[i][1], pos[i][1]))

                for j in range(len(dep_tile_pos[0])):
                    diff1 = abs(dep_tile_pos[i][j][0] - pos[i][0])
                    self.assertLessEqual(
                        diff1, margin,
                        "Failed for %s tiles, %s overlap and %s method," %
                        (num, o, a) + " %f != %f" %
                        (dep_tile_pos[i][j][0], pos[i][0]))

                    diff2 = abs(dep_tile_pos[i][j][1] - pos[i][1])
                    self.assertLessEqual(
                        diff2, margin,
                        "Failed for %s tiles, %s overlap and %s method," %
                        (num, o, a) + " %f != %f" %
                        (dep_tile_pos[i][j][1], pos[i][1]))

            # Test with shifted dependent tiles
            [tiles, pos] = decompose_image(img, o, num, a, False)
            registrar = IdentityRegistrar()

            # Add shift
            dep_tiles = copy.deepcopy(tiles)
            rnd1 = [random.randrange(-20, 20) for _ in range(len(pos))]
            rnd2 = [random.randrange(-20, 20) for _ in range(len(pos))]
            for i in range(len(dep_tiles)):
                p = (dep_tiles[i].metadata[model.MD_POS][0] + rnd1[i],
                     dep_tiles[i].metadata[model.MD_POS][1] + rnd2[i])
                dep_tiles[i].metadata[model.MD_POS] = p

            for i in range(len(pos)):
                registrar.addTile(tiles[i], (dep_tiles[i], dep_tiles[i]))
                tile_pos, dep_tile_pos = registrar.getPositions()

                diff1 = abs(tile_pos[i][0] - pos[i][0])
                # allow difference of 10% of overlap
                margin = 0.3 * o * tiles[i].shape[0]
                self.assertLessEqual(
                    diff1, margin,
                    "Failed for %s tiles, %s overlap and %s method," %
                    (num, o, a) + " %f != %f" % (tile_pos[i][0], pos[i][0]))

                for j in range(2):
                    diff1 = abs(dep_tile_pos[i][j][0] - pos[i][0])
                    self.assertLessEqual(
                        diff1, margin,
                        "Failed for %s tiles, %s overlap and %s method," %
                        (num, o, a) + " %f != %f" %
                        (dep_tile_pos[i][j][0], pos[i][0] + rnd1[i]))

                    diff2 = abs(dep_tile_pos[i][j][1] - pos[i][1])
                    self.assertLessEqual(
                        diff2, margin,
                        "Failed for %s tiles, %s overlap and %s method," %
                        (num, o, a) + " %f != %f" %
                        (dep_tile_pos[i][j][1], pos[i][1] + rnd2[i]))
Exemplo n.º 10
0
    def test_dependent_tiles(self):
        """ Tests functionality for dependent tiles """

        # Test on 3 layers of the same image create by decompose_image
        for img in IMGS:
            conv = find_fittest_converter(img)
            data = conv.read_data(img)[0]
            img = ensure2DImage(data)
            num = 4
            o = 0.3  # fails for 0.2
            a = "horizontalZigzag"
            [tiles, pos] = decompose_image(img, o, num, a)
            registrar = GlobalShiftRegistrar()
            for i in range(len(pos)):
                registrar.addTile(tiles[i], (tiles[i], tiles[i]))
            tile_pos, dep_tile_pos = registrar.getPositions()
            for i in range(len(pos)):
                diff1 = abs(tile_pos[i][0] - pos[i][0])
                diff2 = abs(tile_pos[i][1] - pos[i][1])
                # allow difference of 10% of overlap
                px_size = tiles[i].metadata[model.MD_PIXEL_SIZE]

                # Check if position is not completely wrong. The margins are given
                # by the extreme value calculation in the registrar and provide
                # a very generous upper limit for the error that should never be exceeded
                # because of the fallback method.
                # Unfortunately, many tests don't pass stricter limits yet.
                margin1 = px_size[0] * 5
                margin2 = px_size[1] * 5

                self.assertLessEqual(diff1, margin1,
                                     "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                                     " %f != %f" % (tile_pos[i][0], pos[i][0]))
                self.assertLessEqual(diff2, margin2,
                                     "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                                     " %f != %f" % (tile_pos[i][1], pos[i][1]))

            for p, tile in zip(pos, dep_tile_pos):
                for dep_tile in tile:
                    diff1 = abs(dep_tile[0] - p[0])
                    self.assertLessEqual(diff1, margin1,
                         "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                         " %f != %f" % (dep_tile[0], p[0]))

                    diff2 = abs(dep_tile[1] - p[1])
                    self.assertLessEqual(diff2, margin2,
                         "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                         " %f != %f" % (dep_tile[1], p[1]))

            # Test with shifted dependent tiles
            [tiles, pos] = decompose_image(img, o, num, a)

            registrar = GlobalShiftRegistrar()

            # Add different shift for every dependent tile
            dep_tiles = copy.deepcopy(tiles)
            # x-shift for each dependent tile in px
            rnd1 = [random.randrange(-1000, 1000) for _ in range(len(pos))]
            # y-shift for each dependent tile in px
            rnd2 = [random.randrange(-1000, 1000) for _ in range(len(pos))]
            # Change metadata of dependent tiles
            for i in range(len(dep_tiles)):
                p = (dep_tiles[i].metadata[model.MD_POS][0] + rnd1[i] * px_size[0],
                     dep_tiles[i].metadata[model.MD_POS][1] + rnd2[i] * px_size[1])
                dep_tiles[i].metadata[model.MD_POS] = p

            for i in range(len(pos)):
                # Register tiles
                # 2 layers of dependent tiles with the same pos
                registrar.addTile(tiles[i], (dep_tiles[i], dep_tiles[i]))
            tile_pos, dep_tile_pos = registrar.getPositions()
            for i in range(len(pos)):
                # Test main tile
                diff1 = abs(tile_pos[i][0] - pos[i][0])
                diff2 = abs(tile_pos[i][1] - pos[i][1])
                margin1 = px_size[0] * 5
                margin2 = px_size[1] * 5
                self.assertLessEqual(diff1, margin1,
                                     "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                                     " %f != %f" % (tile_pos[i][0], pos[i][0]))
                self.assertLessEqual(diff2, margin2,
                                     "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                                     " %f != %f" % (tile_pos[i][0], pos[i][0]))

            for p, tile, r1, r2 in zip(tile_pos, dep_tile_pos, rnd1, rnd2):
                for dep_tile in tile:
                    self.assertAlmostEqual(dep_tile[0], p[0] + r1 * px_size[0])
                    self.assertAlmostEqual(dep_tile[1], p[1] + r2 * px_size[1])
Exemplo n.º 11
0
    def test_dependent_tiles(self):
        """ Tests functionality for dependent tiles """

        # Test on 3 layers of the same image create by decompose_image
        for img in IMGS:
            conv = find_fittest_converter(img)
            data = conv.read_data(img)[0]
            img = ensure2DImage(data)
            num = 2
            o = 0.2
            a = "horizontalZigzag"
            [tiles, pos] = decompose_image(img, o, num, a, False)
            registrar = IdentityRegistrar()
            for i in range(len(pos)):
                registrar.addTile(tiles[i], (tiles[i], tiles[i]))
                tile_pos, dep_tile_pos = registrar.getPositions()

                diff1 = abs(tile_pos[i][0] - pos[i][0])
                diff2 = abs(tile_pos[i][1] - pos[i][1])
                # allow difference of 10% of overlap
                px_size = tiles[i].metadata[model.MD_PIXEL_SIZE]
                margin = 0.01 * tiles[i].shape[0] * px_size[0]
                self.assertLessEqual(diff1, margin,
                                     "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                                     " %f != %f" % (tile_pos[i][0], pos[i][0]))
                self.assertLessEqual(diff2, margin,
                                     "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                                     " %f != %f" % (tile_pos[i][1], pos[i][1]))

                for j in range(len(dep_tile_pos[0])):
                    diff1 = abs(dep_tile_pos[i][j][0] - pos[i][0])
                    self.assertLessEqual(diff1, margin,
                                         "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                                         " %f != %f" % (dep_tile_pos[i][j][0], pos[i][0]))

                    diff2 = abs(dep_tile_pos[i][j][1] - pos[i][1])
                    self.assertLessEqual(diff2, margin,
                                         "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                                         " %f != %f" % (dep_tile_pos[i][j][1], pos[i][1]))

            # Test with shifted dependent tiles
            [tiles, pos] = decompose_image(img, o, num, a, False)
            registrar = IdentityRegistrar()

            # Add shift
            dep_tiles = copy.deepcopy(tiles)
            rnd1 = [random.randrange(-20, 20) for _ in range(len(pos))]
            rnd2 = [random.randrange(-20, 20) for _ in range(len(pos))]
            for i in range(len(dep_tiles)):
                p = (dep_tiles[i].metadata[model.MD_POS][0] + rnd1[i],
                     dep_tiles[i].metadata[model.MD_POS][1] + rnd2[i])
                dep_tiles[i].metadata[model.MD_POS] = p

            for i in range(len(pos)):
                registrar.addTile(tiles[i], (dep_tiles[i], dep_tiles[i]))
                tile_pos, dep_tile_pos = registrar.getPositions()

                diff1 = abs(tile_pos[i][0] - pos[i][0])
                # allow difference of 10% of overlap
                margin = 0.3 * o * tiles[i].shape[0]
                self.assertLessEqual(diff1, margin,
                                     "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                                     " %f != %f" % (tile_pos[i][0], pos[i][0]))

                for j in range(2):
                    diff1 = abs(dep_tile_pos[i][j][0] - pos[i][0])
                    self.assertLessEqual(diff1, margin,
                                         "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                                         " %f != %f" % (dep_tile_pos[i][j][0], pos[i][0] + rnd1[i]))

                    diff2 = abs(dep_tile_pos[i][j][1] - pos[i][1])
                    self.assertLessEqual(diff2, margin,
                                         "Failed for %s tiles, %s overlap and %s method," % (num, o, a) +
                                         " %f != %f" % (dep_tile_pos[i][j][1], pos[i][1] + rnd2[i]))