def __getCurrentXform(self): """Returns the current transformation matrix defined by the scale, offset, and rotation widgets. """ scales, offsets, rotations = self.__getCurrentXformComponents() rotations = [r * np.pi / 180 for r in rotations] # We need to figure out the centre # of the image in world coordinates # to define the origin of rotation. shape = self.__overlay.shape lo, hi = transform.axisBounds(shape, self.__overlay.voxToWorldMat) origin = [l + (h - l) / 2.0 for h, l in zip(hi, lo)] return transform.compose(scales, offsets, rotations, origin)
def __drawLegend(self): """Draws a legend in the bottom left corner of the screen, showing anatomical orientation. """ copts = self.opts b = self.__displayCtx.bounds w, h = self.GetSize() xlen, ylen = glroutines.adjust(b.xlen, b.ylen, w, h) # A line for each axis vertices = np.zeros((6, 3), dtype=np.float32) vertices[0, :] = [-1, 0, 0] vertices[1, :] = [1, 0, 0] vertices[2, :] = [0, -1, 0] vertices[3, :] = [0, 1, 0] vertices[4, :] = [0, 0, -1] vertices[5, :] = [0, 0, 1] # Each axis line is scaled to # 60 pixels, and the legend is # offset from the bottom-left # corner by twice this amount. scale = [xlen * 30.0 / w] * 3 offset = [ -0.5 * xlen + 2.0 * scale[0], -0.5 * ylen + 2.0 * scale[1], 0 ] # Apply the current camera # angle and rotation settings # to the legend vertices. Offset # anatomical labels off each # axis line by a small amount. rotation = transform.decompose(self.__viewMat)[2] xform = transform.compose(scale, offset, rotation) labelPoses = transform.transform(vertices * 1.2, xform) vertices = transform.transform(vertices, xform) # Draw the legend lines gl.glDisable(gl.GL_DEPTH_TEST) gl.glColor3f(*copts.cursorColour[:3]) gl.glLineWidth(2) gl.glBegin(gl.GL_LINES) gl.glVertex3f(*vertices[0]) gl.glVertex3f(*vertices[1]) gl.glVertex3f(*vertices[2]) gl.glVertex3f(*vertices[3]) gl.glVertex3f(*vertices[4]) gl.glVertex3f(*vertices[5]) gl.glEnd() # Figure out the anatomical # labels for each axis. overlay = self.__displayCtx.getSelectedOverlay() dopts = self.__displayCtx.getOpts(overlay) labels = dopts.getLabels()[0] # getLabels returns (xlo, ylo, zlo, xhi, yhi, zhi) - # - rearrange them to (xlo, xhi, ylo, yhi, zlo, zhi) labels = [ labels[0], labels[3], labels[1], labels[4], labels[2], labels[5] ] canvas = np.array([w, h]) view = np.array([xlen, ylen]) # Draw each label for i in range(6): # Calculate pixel x/y # location for this label xx, xy = canvas * (labelPoses[i, :2] + 0.5 * view) / view # Calculate the size of the label # in pixels, so we can centre the # label tw, th = glroutines.text2D(labels[i], (xx, xy), 10, (w, h), calcSize=True) # Draw the text xx -= 0.5 * tw xy -= 0.5 * th gl.glColor3f(*copts.legendColour[:3]) glroutines.text2D(labels[i], (xx, xy), 10, (w, h))
def _test_Image_init_xform(imgtype): with tempdir() as td: sform = transform.compose(np.random.random(3), np.random.random(3), np.random.random(3)) qform = transform.compose(np.random.random(3), np.random.random(3), np.random.random(3)) sform_code = 3 qform_code = 4 # Create a base nifti image img = make_image('file.nii') img.set_sform(sform, code=sform_code) img.set_qform(qform, code=qform_code) nib.save(img, 'file.nii') img = nib.load('file.nii') # an image created off a # header should have # identical sform/qform fimg = fslimage.Image(img.get_data(), header=img.header) fsform, fsform_code = fimg.header.get_sform(True) fqform, fqform_code = fimg.header.get_qform(True) xform = fimg.voxToWorldMat assert np.all(np.isclose(fsform, sform)) assert np.all(np.isclose(fqform, qform)) assert np.all(np.isclose(xform, sform)) assert fsform_code == sform_code assert fqform_code == qform_code # an image created off # an xform only should # get its sform set # set to that xform, # qform to None, and # and codes set to (s2, q0) fimg = fslimage.Image(img.get_data(), xform=sform) fsform, fsform_code = fimg.header.get_sform(True) fqform, fqform_code = fimg.header.get_qform(True) xform = fimg.voxToWorldMat assert np.all(np.isclose(fsform, sform)) assert np.all(np.isclose(xform, sform)) assert fqform is None assert fsform_code == 2 assert fqform_code == 0 # an image created with a # header and an xform should # have its s/q forms set # to the xform. and its # s/q form codes the same # as what is in the header rxform = transform.compose(np.random.random(3), np.random.random(3), np.random.random(3)) fimg = fslimage.Image(img.get_data(), header=img.header, xform=rxform) fsform, fsform_code = fimg.header.get_sform(True) fqform, fqform_code = fimg.header.get_qform(True) xform = fimg.voxToWorldMat assert np.all(np.isclose(fsform, rxform)) assert np.all(np.isclose(fqform, rxform)) assert np.all(np.isclose(xform, rxform)) assert fsform_code == sform_code assert fqform_code == qform_code