def main(argv): """Register the slices in a stack.""" source_dir = 'SOURCE_DIR/' target_dir = 'TARGET_DIR/' transf_dir = 'TARGET_DIR/trans/' reference_name = 'REFNAME' use_shrinking_constraint = 0 p = Register_Virtual_Stack_MT.Param() p.sift.maxOctaveSize = 1024 p.minInlierRatio = 0.05 Register_Virtual_Stack_MT.exec(source_dir, target_dir, transf_dir, reference_name, p, use_shrinking_constraint)
def Composite_Aligner(channels, dirs, parameters): """ Aligns composite images, saves to directory. """ # Reference image name (must be within source directory) reference_name = "Timepoint000.tif" # Shrinkage option (False = 0) if parameters["shrinkage"] == "True": use_shrinking_constraint = 1 print "shrink" else: use_shrinking_constraint = 0 print "noshrink" # Parameters method, RVSS p = Register_Virtual_Stack_MT.Param() # SIFT parameters: # python cannot coerce string "floats" to int directly, # hence int(float("1.0")) (RVSS wants ints..). p.sift.maxOctaveSize = int(float(parameters["max_oct"])) p.sift.fdSize = int(float(parameters["fd_size"])) p.sift.initialSigma = int(float(parameters["sigma"])) p.maxEpsilon = int(float(parameters["max_eps"])) p.sift.steps = int(float(parameters["steps"])) p.minInlierRatio = int(float(parameters["min_inlier"])) # 1 = RIGID, 3 = AFFINE p.featuresModelIndex = int(float(parameters["feat_model"])) p.registrationModelIndex = int(float(parameters["reg_model"])) # Opens a dialog to set transformation options, comment out to run in default mode #IJ.beep() #p.showDialog() # Executes alignment. print ("Registering stack...") reference_name = "Timepoint000.tif" Register_Virtual_Stack_MT.exec(dirs["Composites"] + os.sep, dirs["Composites_Aligned"] + os.sep, dirs["Transformations"] + os.sep, reference_name, p, use_shrinking_constraint) print ("Registration completed.") # Close alignment window. imp = WindowManager.getCurrentImage() imp.close()
def rigidAlignment(projectPath, params, name = '', boxFactor = 1): # rigid alignment outside trakem2 with register virtual stack plugin because bug in trakem2 projectFolder = os.path.dirname(projectPath) projectName = os.path.splitext(os.path.basename(projectPath))[0] project, loader, layerset, nLayers = openTrakemProject(projectPath) exportForRigidAlignmentFolder = mkdir_p(os.path.join(projectFolder, 'exportForRigidAlignment')) resultRigidAlignmentFolder = mkdir_p(os.path.join(projectFolder, 'resultRigidAlignment')) bb = layerset.get2DBounds() roi = Rectangle(int(bb.width/2 * (1 - boxFactor)), int(bb.height/2 * (1 - boxFactor)), int(bb.width*boxFactor), int(bb.height*boxFactor)) boxPath = os.path.join(resultRigidAlignmentFolder, 'alignmentBox.txt') writeRectangle(roi, boxPath) exportFlat(project, exportForRigidAlignmentFolder, 1, baseName = 'exportForRigidAlignment', roi = roi) referenceName = naturalSort(os.listdir(exportForRigidAlignmentFolder))[0] use_shrinking_constraint = 0 IJ.log('Rigid alignment with register virtual stack') Register_Virtual_Stack_MT.exec(exportForRigidAlignmentFolder, resultRigidAlignmentFolder, resultRigidAlignmentFolder, referenceName, params, use_shrinking_constraint) time.sleep(2) IJ.log('Warning: rigidAlignment closing all existing windows') WindowManager.closeAllWindows() # problematic because it also closes the log window # IJ.getImage().close() for l, layer in enumerate(layerset.getLayers()): imagePath = os.path.join(exportForRigidAlignmentFolder, 'exportForRigidAlignment_' + str(l).zfill(4) + '.tif') transformPath = os.path.join(resultRigidAlignmentFolder, 'exportForRigidAlignment_' + str(l).zfill(4) + '.xml') aff = getAffFromRVSTransformPath(transformPath) for patch in layer.getDisplayables(Patch): patch.setLocation(patch.getX()-roi.x, patch.getY()-roi.y) # compensate for the extracted bounding box # patch.setLocation(roi.x, roi.y) # compensate for the extracted bounding box currentAff = patch.getAffineTransform() currentAff.preConcatenate(aff) patch.setAffineTransform(currentAff) resizeDisplay(layerset) project.save() closeProject(project) IJ.log('All LM layers have been aligned in: ' + projectPath)
def runVirtualAlignment(self): IJ.log("Function runVirtualAlignment on " + self.inputFolder) refImage = None for (DAPIFile, fluoFiles) in self.imageFiles: copyfile(DAPIFile, self.inputDAPIFolder + os.path.basename(DAPIFile)) for fluoFile in fluoFiles: copyfile(fluoFile, self.inputFluoFolder + os.path.basename(fluoFile)) if refImage is None: refImage = os.path.basename(self.inputDAPIFolder + os.path.basename(DAPIFile)) p = Register_Virtual_Stack_MT.Param() use_shrinking_constraint = self.useShrinking IJ.log("Registering DAPI") Register_Virtual_Stack_MT.exec(self.inputDAPIFolder, self.outputDAPIFolder, self.outputDAPIFolder, refImage, p, use_shrinking_constraint) IJ.log("Copying DAPI transformation matrices to Fluo folder") for (DAPIFile, fluoFiles) in self.imageFiles: for fluoFile in fluoFiles: copyfile( self.outputDAPIFolder + os.path.basename(DAPIFile).replace(".tif", ".xml"), self.outputFluoFolder + os.path.basename(fluoFile).replace(".tif", ".xml")) use_interpolate = self.useInterpolate IJ.log("Registering Fluo based on DAPI transformations") Transform_Virtual_Stack_MT.exec(self.inputFluoFolder, self.outputFluoFolder, self.outputFluoFolder, use_interpolate) self.displayStack()
from register_virtual_stack import Register_Virtual_Stack_MT namePlugin = 'alignRigid_LM' MagCFolder = fc.startPlugin(namePlugin) ControlWindow.setGUIEnabled(False) # get mosaic size MagCParameters = fc.readMagCParameters(MagCFolder) executeAlignment = MagCParameters[namePlugin]['executeAlignment'] boxFactor = MagCParameters[namePlugin]['boxFactor'] # e.g. 0.5, use only the center part of the layer to compute alignment: 0.5 divides the x and y dimensions by 2 projectPath = fc.findFilesFromTags(MagCFolder,['LMProject'])[0] # alignment parameters regParams = Register_Virtual_Stack_MT.Param() regParams.minInlierRatio = 0 regParams.registrationModelIndex = 1 regParams.featuresModelIndex = 1 regParams.sift.fdBins = 8 regParams.sift.fdSize = 4 regParams.sift.initialSigma = 1.6 regParams.sift.maxOctaveSize = 1024 regParams.sift.minOctaveSize = 64 regParams.sift.steps = 6 regParams.interpolate = True regParams.maxEpsilon = 25 regParams.minInlierRatio = 0 regParams.rod = 0.92
for c in range(1,(xdim+1)): cs= str(c) # source directory source_dir = ISanalysisfolder + "%04d" % i + "/aligned/conv_561adj/c_" + cs + "/" # output directory # transforms directory transf_dir = ISanalysisfolder + "%04d" % i + "/aligned/conv_561adj/c_" + cs + "/" # reference image file = (glob.glob(source_dir + "*" + ".tif")) print >>f1, os.path.basename(file[0]) reference_name = os.path.basename(file[0]) # shrinkage option (false) use_shrinking_constraint = 0 p = Register_Virtual_Stack_MT.Param() p.featuresModelIndex = 0 p.registrationModelIndex = 0 p.interpolate = 1 p.maxEpsilon = 25 # The "maximum image size": p.sift.maxOctaveSize = 400 p.sift.fdBins = 8 p.sift.fdSize = 8 p.sift.initialSigma = 1.0 p.sift.steps = 6 p.sift.minOctaveSize = 64 # The "inlier ratio": p.minInlierRatio = 0.03 iterat = 0
## script to run Register_Virtual_Stack as CLI based on http://imagej.net/Register_Virtual_Stack_Slices#Scripting_.2F_PlugIn import sys # to get command line args # import argparse # fiji's jython does not have argparse: https://ilovesymposia.com/2014/02/26/fiji-jython/ from register_virtual_stack import Register_Virtual_Stack_MT argv = sys.argv # shrinkage option (false) use_shrinking_constraint = 0 ## http://javadoc.imagej.net/Fiji/register_virtual_stack/Register_Virtual_Stack_MT.html p = Register_Virtual_Stack_MT.Param() # p.sift.maxOctaveSize = 1024 # The "maximum image size" # p.minInlierRatio = 0.05 # The "inlier ratio" p.featuresModelIndex = Register_Virtual_Stack_MT.RIGID # 0=TRANSLATION, 1=RIGID, 2=SIMILARITY, 3=AFFINE p.registrationModelIndex = Register_Virtual_Stack_MT.RIGID # 0=TRANSLATION, 1=RIGID, 2=SIMILARITY, 3=AFFINE, 4=ELASTIC, 5=MOVING_LEAST_SQUARES Register_Virtual_Stack_MT.save_transforms= True Register_Virtual_Stack_MT.exec(argv[1], argv[2], argv[3], argv[4], p, use_shrinking_constraint)
format='tiff') featuremodelindex = ["Translation", "Rigid", "Similarity", "Affine"] regmodelindex = [ "Translation", "Rigid", "Similarity", "Affine", "Elastic", "Moving Least Squares" ] fmi = featuremodelindex.index(fmodelstring) rmi = regmodelindex.index(rmodelstring) # shrinkage option (false) use_shrinking_constraint = False p = Register_Virtual_Stack_MT.Param() # inlier ratio p.minInlierRatio = 0.05 # implemented transformation models for choice 0=TRANSLATION, 1=RIGID, 2=SIMILARITY, 3=AFFINE #p.featuresModelIndex = 3 p.featuresModelIndex = fmi # maximal allowed alignment error in pixels p.maxEpsilon = 10 # Implemented transformation models for choice 0=TRANSLATION, 1=RIGID, 2=SIMILARITY, 3=AFFINE, 4=ELASTIC, 5=MOVING_LEAST_SQUARES #p.registrationModelIndex = 3 p.registrationModelIndex = rmi # Closest/next neighbor distance ratio p.rod = 0.9 ############# bunwarpJ Parameters #########
namePlugin = 'alignRigid_EM' MagCFolder = fc.startPlugin(namePlugin) ControlWindow.setGUIEnabled(False) MagC_EM_Folder = os.path.join(MagCFolder, 'MagC_EM', '') MagCParameters = fc.readMagCParameters(MagCFolder) regModel = MagCParameters[namePlugin]['regModel'] inputFolder = fc.findFoldersFromTags(MagCFolder, ['export_stitchedEMForAlignment'])[0] imagePaths = filter( lambda x: os.path.splitext(x)[1] == '.tif', fc.naturalSort( [os.path.join(inputFolder, x) for x in os.listdir(inputFolder)])) regParams = Register_Virtual_Stack_MT.Param() regParams.minInlierRatio = 0 regParams.registrationModelIndex = regModel # 1-Rigid, 2-Similarity, 3-Affine regParams.featuresModelIndex = regModel exportForRigidAlignmentFolder = inputFolder resultRigidAlignmentFolder = fc.mkdir_p( os.path.join(MagC_EM_Folder, 'resultRigidAlignment')) ################################################ # rigid alignment outside trakem2 with register virtual stack plugin because bug in trakem2 transformsPath = os.path.join( MagC_EM_Folder, 'rigidAlignmentTransforms_' + namePlugin + '.txt') referenceName = fc.naturalSort(os.listdir(exportForRigidAlignmentFolder))[0] use_shrinking_constraint = 0
from register_virtual_stack import Register_Virtual_Stack_MT # source directory source_dir = "C:\\Users\\stoeter\\Documents\\Temp\\sequence_GreinerGlassPlate_GSP_500nm_beads_H12_T0001F001_raw_xx_plane6\\" # output directory target_dir = "C:\\Users\\stoeter\\Documents\\Temp\\result\\" # transforms directory transf_dir = "C:\\Users\\stoeter\\Documents\\Temp\\transform\\" # reference image reference_name = "GreinerGlassPlate_GSP_500nm_beads_H12_T0001F001_raw_01_plane6.tif" # shrinkage option (false) use_shrinking_constraint = 0 p = Register_Virtual_Stack_MT.Param() # The "maximum image size": p.sift.maxOctaveSize = 2800 # The "inlier ratio": p.minInlierRatio = 0.05 # Implemented transformation models for choice # 0=TRANSLATION, 1=RIGID, 2=SIMILARITY, 3=AFFINE, 4=ELASTIC, 5=MOVING_LEAST_SQUARES p.registrationModelIndex = 3 # Implemented transformation models for choice # 0=TRANSLATION, 1=RIGID, 2=SIMILARITY, 3=AFFINE p.featuresModelIndex = 3 Register_Virtual_Stack_MT.exec(source_dir, target_dir, transf_dir, reference_name, p, use_shrinking_constraint)