os.path.join(MagCFolder, 'MagC_EM', 'EMProject.xml')) # the high res EM project afterProjectPath = fc.cleanLinuxPath( projectPath.replace('EMProject.', 'ToBeElasticAlignedEMProject.')) IJ.log('afterProjectPath ' + str(afterProjectPath)) project, loader, layerset, nLayers = fc.openTrakemProject(projectPath) for l, layer in enumerate(layerset.getLayers()): transformPath = os.path.join( resultRigidAlignmentFolder, 'stitchedDownsampledEM_' + str(l).zfill(4) + '.xml') aff = fc.getAffFromRVSTransformPath(transformPath) # aff.scale(float(downsamplingFactor), float(downsamplingFactor)) # cannot be used because it is not a simple scaling aff = AffineTransform(aff.getScaleX(), aff.getShearY(), aff.getShearX(), aff.getScaleY(), aff.getTranslateX() * float(downsamplingFactor), aff.getTranslateY() * float(downsamplingFactor)) for patch in layer.getDisplayables(Patch): currentAff = patch.getAffineTransform() currentAff.preConcatenate(aff) patch.setAffineTransform(currentAff) IJ.log( 'The real EM project is now rigidly aligned. Saving and closing the project.' ) fc.resizeDisplay(layerset) project.save() project.saveAs(afterProjectPath, True) fc.closeProject(project)
# For 2D, java offers an AffineTransform class that addresses this issue # quite trivially, with the method rotate that takes an origin of rotation # as argument. # BEWARE that positive angles rotate to the right (rather than to the left) # in the AffineTransform, so we use radians(45) instead of radians(-45). # And BEWARE that AffineTransform.getMatrix fills a double[] array in # an order that you wouldn't expect (see the javadoc), so instead # we call each value of the matrix, one by one, to fill our matrix. aff = AffineTransform() # initialized as the identity transform cx = imp.getWidth() / 2.0 cy = imp.getHeight() / 2.0 aff.rotate(radians(45), cx, cy) rotate45easy = AffineTransform2D() rotate45easy.set(aff.getScaleX(), aff.getShearX(), aff.getTranslateX(), aff.getShearY(), aff.getScaleY(), aff.getTranslateY()) print rotate45easy viewTransformed(imp, rotate45easy, title=imp.getTitle() + " rotate45easy") # An alternative that works also for 3D transformations exploits # a nice property of transformation matrices: that they can be combined. # That is, instead of applying a transform to an image, an then applying # a second transform to the resulting image, instead the transformation # matrices can be multiplied, and a single transform applied to the image. # This is desirable not only for performance reasons, but primarily # because it avoids the accumulation of interpolation errors. # In addition, ImgLib2 realtransform classes offer a "rotate" method # which simplies operations further: no more explicit sin and cos!
################################################ IJ.log('Aligning the highEM with the new rigid transforms') downsamplingFactor = MagCParameters['downsample_EM']['downsamplingFactor'] IJ.log('The LM/EM pixel scale factor is ' + str(downsamplingFactor)) projectPath = fc.cleanLinuxPath(os.path.join(MagCFolder, 'MagC_EM', 'EMProject.xml')) # the high res EM project afterProjectPath = fc.cleanLinuxPath(projectPath.replace('EMProject.', 'ToBeElasticAlignedEMProject.')) IJ.log('afterProjectPath ' + str(afterProjectPath)) project, loader, layerset, nLayers = fc.openTrakemProject(projectPath) for l, layer in enumerate(layerset.getLayers()): transformPath = os.path.join(resultRigidAlignmentFolder, 'stitchedDownsampledEM_' + str(l).zfill(4) + '.xml') aff = fc.getAffFromRVSTransformPath(transformPath) # aff.scale(float(downsamplingFactor), float(downsamplingFactor)) # cannot be used because it is not a simple scaling aff = AffineTransform(aff.getScaleX(), aff.getShearY(), aff.getShearX(), aff.getScaleY(), aff.getTranslateX() * float(downsamplingFactor), aff.getTranslateY() * float(downsamplingFactor)) for patch in layer.getDisplayables(Patch): currentAff = patch.getAffineTransform() currentAff.preConcatenate(aff) patch.setAffineTransform(currentAff) IJ.log('The real EM project is now rigidly aligned. Saving and closing the project.') fc.resizeDisplay(layerset) project.save() project.saveAs(afterProjectPath, True) fc.closeProject(project) time.sleep(2) fc.terminatePlugin(namePlugin, MagCFolder)