示例#1
0
def test_rotMatToAffine(seed):

    pi = np.pi
    pi2 = pi / 2

    for i in range(100):

        rots = [
            -pi + 2 * pi * np.random.random(),
            -pi2 + 2 * pi2 * np.random.random(),
            -pi + 2 * pi * np.random.random()
        ]

        if np.random.random() < 0.5: origin = None
        else: origin = np.random.random(3)

        rmat = affine.axisAnglesToRotMat(*rots)
        mataff = affine.rotMatToAffine(rmat, origin)
        rotaff = affine.rotMatToAffine(rots, origin)

        exp = np.eye(4)
        exp[:3, :3] = rmat
        exp[:3, 3] = origin

        assert np.all(np.isclose(mataff, rotaff))
示例#2
0
def rotate(infile, rx, ry, rz):
    basename = fslimage.removeExt(op.basename(infile))
    outfile = '{}_rotated_{}_{}_{}.nii.gz'.format(basename, rx, ry, rz)
    img = fslimage.Image(infile)

    rx = rx * np.pi / 180
    ry = ry * np.pi / 180
    rz = rz * np.pi / 180

    rot = affine.axisAnglesToRotMat(rx, ry, rz)
    rot = affine.rotMatToAffine(rot)
    img.voxToWorldMat = affine.concat(rot, img.voxToWorldMat)

    img.save(outfile)

    return outfile
def swapdim(infile):
    basename = fslimage.removeExt(op.basename(infile))
    outfile = '{}_swapdim.nii.gz'.format(basename)
    img = fslimage.Image(infile)

    data = img.data
    xform = img.voxToWorldMat

    data = data.transpose((2, 0, 1))
    rot = affine.rotMatToAffine(
        affine.concat(affine.axisAnglesToRotMat(np.pi / 2, 0, 0),
                      affine.axisAnglesToRotMat(0, 0, 3 * np.pi / 2)))
    xform = affine.concat(xform, affine.scaleOffsetXform((1, -1, -1),
                                                         (0, 0, 0)), rot)

    fslimage.Image(data, xform=xform, header=img.header).save(outfile)

    return outfile
示例#4
0
    def __genViewMatrix(self, w, h):
        """Generate and return a transformation matrix to be used as the
        model-view matrix. This includes applying the current :attr:`zoom`,
        :attr:`rotation` and :attr:`offset` settings, and configuring
        the camera. This method is called by :meth:`__setViewport`.

        :arg w: Canvas width in pixels
        :arg h: Canvas height in pixels
        """

        opts = self.opts
        b = self.__displayCtx.bounds
        centre = [
            b.xlo + 0.5 * b.xlen, b.ylo + 0.5 * b.ylen, b.zlo + 0.5 * b.zlen
        ]

        # The MV matrix comprises (in this order):
        #
        #    - A rotation (the rotation property)
        #
        #    - Camera configuration. With no rotation, the
        #      camera will be looking towards the positive
        #      Y axis (i.e. +y is forwards), and oriented
        #      towards the positive Z axis (i.e. +z is up)
        #
        #    - A translation (the offset property)
        #    - A scaling (the zoom property)

        # Scaling and rotation matrices. Rotation
        # is always around the centre of the
        # displaycontext bounds (the bounding
        # box which contains all loaded overlays).
        scale = opts.zoom / 100.0
        scale = affine.scaleOffsetXform([scale] * 3, 0)
        rotate = affine.rotMatToAffine(opts.rotation, centre)

        # The offset property is defined in x/y
        # pixels, normalised to [-1, 1]. We need
        # to convert them into viewport space,
        # where the horizontal axis maps to
        # (-xhalf, xhalf), and the vertical axis
        # maps to (-yhalf, yhalf). See
        # gl.routines.ortho.
        offset = np.array(opts.offset[:] + [0])
        xlen, ylen = glroutines.adjust(b.xlen, b.ylen, w, h)
        offset[0] = xlen * offset[0] / 2
        offset[1] = ylen * offset[1] / 2
        offset = affine.scaleOffsetXform(1, offset)

        # And finally the camera.
        eye = list(centre)
        eye[1] += 1
        up = [0, 0, 1]
        camera = glroutines.lookAt(eye, centre, up)

        # Order is very important!
        xform = affine.concat(offset, scale, camera, rotate)
        np.array(xform, dtype=np.float32)

        self.__viewOffset = offset
        self.__viewScale = scale
        self.__viewRotate = rotate
        self.__viewCamera = camera
        self.__viewMat = xform