Esempio n. 1
0
    def test_get_img_transformation_matrix(self):
        # simplest matrix
        md = {
            model.MD_PIXEL_SIZE: (1.0, 1.0),
            model.MD_ROTATION: 0.0,
            model.MD_SHEAR: 0.0,
        }
        mat = get_img_transformation_matrix(md)
        numpy.testing.assert_almost_equal(mat, [[1., 0.], [0., -1.]])

        micro = 1.0e-06
        # 90 degrees of rotation
        md = {
            model.MD_PIXEL_SIZE: (micro, micro),
            model.MD_ROTATION: math.pi / 2,
        }
        mat = get_img_transformation_matrix(md)
        numpy.testing.assert_almost_equal(mat, [[0.0, micro], [micro, 0.0]])

        # 180 degrees of rotation
        md = {
            model.MD_PIXEL_SIZE: (micro, micro),
            model.MD_ROTATION: math.pi,
        }
        mat = get_img_transformation_matrix(md)
        numpy.testing.assert_almost_equal(mat, [[-micro, 0.0], [0.0, micro]])

        # scale and rotation 45 degrees
        md = {
            model.MD_PIXEL_SIZE: (micro, micro),
            model.MD_ROTATION: math.pi / 4,
        }
        mat = get_img_transformation_matrix(md)
        sin = math.sin(math.pi / 4) * micro
        numpy.testing.assert_almost_equal(mat, [[sin, sin], [sin, -sin]])

        # scale and rotation 45 degrees
        md = {
            model.MD_PIXEL_SIZE: (micro, micro),
            model.MD_SHEAR: 0.5,
        }
        mat = get_img_transformation_matrix(md)
        numpy.testing.assert_almost_equal(
            mat, [[micro, 0.0], [-0.5 * micro, -micro]])

        # everything
        md = {
            model.MD_PIXEL_SIZE: (micro, micro),
            model.MD_ROTATION: math.pi / 4,
            model.MD_SHEAR: 0.1,
        }
        mat = get_img_transformation_matrix(md)
        numpy.testing.assert_almost_equal(
            mat, [[7.77817459e-07, sin], [6.36396103e-07, -sin]])

        # nothing
        md = {}
        with self.assertRaises(ValueError):  # MD_PIXEL_SIZE must be present
            mat = get_img_transformation_matrix(md)
Esempio n. 2
0
    def test_get_img_transformation_matrix(self):
        # simplest matrix
        md = {
            model.MD_PIXEL_SIZE: (1.0, 1.0),
            model.MD_ROTATION: 0.0,
            model.MD_SHEAR: 0.0,
        }
        mat = get_img_transformation_matrix(md)
        numpy.testing.assert_almost_equal(mat, [[1., 0.], [0., -1.]])

        micro = 1.0e-06
        # 90 degrees of rotation
        md = {
            model.MD_PIXEL_SIZE: (micro, micro),
            model.MD_ROTATION: math.pi / 2,
        }
        mat = get_img_transformation_matrix(md)
        numpy.testing.assert_almost_equal(mat, [[0.0, micro], [micro, 0.0]])

        # 180 degrees of rotation
        md = {
            model.MD_PIXEL_SIZE: (micro, micro),
            model.MD_ROTATION: math.pi,
        }
        mat = get_img_transformation_matrix(md)
        numpy.testing.assert_almost_equal(mat, [[-micro, 0.0], [0.0, micro]])

        # scale and rotation 45 degrees
        md = {
            model.MD_PIXEL_SIZE: (micro, micro),
            model.MD_ROTATION: math.pi / 4,
        }
        mat = get_img_transformation_matrix(md)
        sin = math.sin(math.pi / 4) * micro
        numpy.testing.assert_almost_equal(mat, [[sin, sin], [sin, -sin]])

        # scale and rotation 45 degrees
        md = {
            model.MD_PIXEL_SIZE: (micro, micro),
            model.MD_SHEAR: 0.5,
        }
        mat = get_img_transformation_matrix(md)
        numpy.testing.assert_almost_equal(mat, [[micro, 0.0], [-0.5 * micro, -micro]])

        # everything
        md = {
            model.MD_PIXEL_SIZE: (micro, micro),
            model.MD_ROTATION: math.pi / 4,
            model.MD_SHEAR: 0.1,
        }
        mat = get_img_transformation_matrix(md)
        numpy.testing.assert_almost_equal(mat, [[7.77817459e-07, sin], [6.36396103e-07, -sin]])

        # nothing
        md = {}
        with self.assertRaises(ValueError): # MD_PIXEL_SIZE must be present
            mat = get_img_transformation_matrix(md)
Esempio n. 3
0
def getCenterOfTiles(tiles, result_shape):
    """ Calculates the center of the result image
    It is based on the formula for calculating the position of a pixel in world coordinates:
    CT = CI + TMAT * DC
    where:
      CT: center of the tile in pixel coordinates
      CI: center of the image in world coordinates
      DC: delta of the centers in pixel coordinates
      TMAT: transformation matrix
    From the formula above, comes the following formula:
    CI = CT - TMAT * DC,
    which is used below

    tiles (tuple of tuple of DataArray): Tiles
    result_shape (height, width): Size in pixels of the result image from the tiles
    return (x, y): Physical coordinates of the center of the image
    """

    first_tile = tiles[0][0]
    ft_md = first_tile.metadata

    dims = ft_md.get(model.MD_DIMS, "CTZYX"[-first_tile.ndim::])
    ft_shape = [
        first_tile.shape[dims.index('X')], first_tile.shape[dims.index('Y')]
    ]
    # center of the tile in pixel coordinates
    center_tile_pixel = [d / 2 for d in ft_shape]
    # center of the image in pixel coordinates
    center_image_pixel = [d / 2 for d in result_shape[::-1]]
    # distance between the center of the tile and the center of the image, in pixel coordinates
    dist_centers_tile_pixels = [
        ct - ci for ct, ci in zip(center_tile_pixel, center_image_pixel)
    ]
    # converts the centers distance, so this variable can be multiplied by the transformation matrix
    dist_centers_tile_pixels = numpy.array(
        dist_centers_tile_pixels).transpose()
    # transformation matrix
    tmat = get_img_transformation_matrix(first_tile.metadata)
    # distance of the centers converted to world coordinates
    dist_centers_w = tmat @ dist_centers_tile_pixels
    # convert the variable from a numpy.matrix to a numpy.array
    dist_centers_w = numpy.ravel(dist_centers_w)
    # center of the tile in world coordinates
    center_tile_w = first_tile.metadata[model.MD_POS]
    # center of the image in world coordinates
    image_pos = center_tile_w - dist_centers_w
    return tuple(image_pos)
Esempio n. 4
0
def getCenterOfTiles(tiles, result_shape):
    """ Calculates the center of the result image
    It is based on the formula for calculating the position of a pixel in world coordinates:
    CT = CI + TMAT * DC
    where:
      CT: center of the tile in pixel coordinates
      CI: center of the image in world coordinates
      DC: delta of the centers in pixel coordinates
      TMAT: transformation matrix
    From the formula above, comes the following formula:
    CI = CT - TMAT * DC,
    which is used below

    tiles (tuple of tuple of DataArray): Tiles
    result_shape (height, width): Size in pixels of the result image from the tiles
    return (x, y): Physical coordinates of the center of the image
    """

    first_tile = tiles[0][0]
    ft_md = first_tile.metadata

    dims = ft_md.get(model.MD_DIMS, "CTZYX"[-first_tile.ndim::])
    ft_shape = [first_tile.shape[dims.index('X')], first_tile.shape[dims.index('Y')]]
    # center of the tile in pixel coordinates
    center_tile_pixel = [d / 2 for d in ft_shape]
    # center of the image in pixel coordinates
    center_image_pixel = [d / 2 for d in result_shape[::-1]]
    # distance between the center of the tile and the center of the image, in pixel coordinates
    dist_centers_tile_pixels = [ct - ci for ct, ci in zip(center_tile_pixel, center_image_pixel)]
    # converts the centers distance, so this variable can be multiplied by the transformation matrix
    dist_centers_tile_pixels = numpy.matrix(dist_centers_tile_pixels).getT()
    # transformation matrix
    tmat = get_img_transformation_matrix(first_tile.metadata)
    # distance of the centers converted to world coordinates
    dist_centers_w = tmat * dist_centers_tile_pixels
    # convert the variable from a numpy.matrix to a numpy.array
    dist_centers_w = numpy.ravel(dist_centers_w)
    # center of the tile in world coordinates
    center_tile_w = first_tile.metadata[model.MD_POS]
    # center of the image in world coordinates
    image_pos = center_tile_w - dist_centers_w
    return tuple(image_pos)