for i in range(num_sections): layerset.getLayer(i, 1, True) layerset.setDimensions(im_width, im_height, LayerSet.NORTHWEST) # ... and update the LayerTree: project.getLayerTree().updateList(layerset) # ... and the display slider Display.updateLayerScroller(layerset) filenames = sorted(os.listdir(storm_merged_folder)) filenames1 = sorted(os.listdir(conv_561_folder)) filenames2 = sorted(os.listdir(conv_merged_folder)) filenames3 = sorted(os.listdir(conv_align_folder)) #3 Load in images for i, layer in enumerate(layerset.getLayers()): #i = i +150 #use this to start in the middle of a dataset filepath = os.path.join(storm_merged_folder, filenames[i]) patch = Patch.createPatch(project, filepath) # patch.updateMipMaps() layer.add(patch, False) # filepath = os.path.join(conv_merged_folder, filenames2[i]) patch = Patch.createPatch(project, filepath) # patch.updateMipMaps() layer.add(patch, False) filepath = os.path.join(conv_561_folder, filenames1[i]) patch = Patch.createPatch(project, filepath) # patch.updateMipMaps() layer.add(patch, False) filepath = os.path.join(conv_align_folder, filenames3[i]) patch = Patch.createPatch(project, filepath)
IJ.log('Inserting all patches') for i in range(0, len(transforms), 8): alignedPatchPath = transforms[i] l = int(transforms[i + 1]) alignedPatchName = os.path.basename(alignedPatchPath) toAlignPatchPath = fc.cleanLinuxPath( os.path.join(os.path.dirname(alignedPatchPath), alignedPatchName.replace(channels[-1], channel))) toAlignPatchPath = toAlignPatchPath[: -1] # remove a mysterious trailing character ... IJ.log('In channel ' + str(channel) + ', inserting this image: ' + str(toAlignPatchPath)) aff = AffineTransform( [float(transforms[a]) for a in range(i + 2, i + 8)]) patch = Patch.createPatch(project, toAlignPatchPath) layer = layerset.getLayers().get(l) layer.add(patch) patch.setAffineTransform(aff) patch.updateBucket() time.sleep(1) IJ.log('Readjusting display') fc.resizeDisplay(layerset) IJ.log('Blending all layers') Blending.blendLayerWise(layerset.getLayers(), True, None) IJ.log('Exporting') for scaleFactor in scaleFactors: theBaseName = 'exported_downscaled_' + str(int( 1 / float(scaleFactor))) + '_' + channel
registeredFolder = os.path.join(layerFolder, 'registered') affTransformPath = os.path.join(registeredFolder, 'affineSerialized') if os.path.isfile(affTransformPath): affTransform = loaderZ.deserialize(affTransformPath) for channel in channels: affineCroppedFolder = os.path.join(LMFolder, 'affineCropped_' + channel) LMMosaicsPath = fc.cleanLinuxPath( os.path.join( LMFolder, 'exported_downscaled_1_' + channel, 'exported_downscaled_1_' + channel + '_' + str(l).zfill(4) + '.tif')) patch = Patch.createPatch(pZ, LMMosaicsPath) layerZ.add(patch) IJ.log('Setting the affineTransform ' + str(affTransform)) patch.setAffineTransform(affTransform) patch.updateBucket() bb = Rectangle(0, 0, widthEM, heightEM) affineCroppedIm = loaderZ.getFlatImage(layerZ, bb, 1, 0x7fffffff, ImagePlus.GRAY8, Patch, layerZ.getAll(Patch), True, Color.black, None) affineCroppedImPath = os.path.join( affineCroppedFolder, 'affineCropped_' + channel + '_' + str(l).zfill(4) + '.tif') IJ.save(affineCroppedIm, affineCroppedImPath) affineCroppedIm.close()
def computeRegistration(): while atomicI.get() < nSections: l = atomicI.getAndIncrement() if l < nSections: IJ.log('Computing EM/LM registration for layer ' + str(l).zfill(4)) layerFolder = fc.mkdir_p( os.path.join(registrationFolder, 'layer_' + str(l).zfill(4))) toRegisterFolder = fc.mkdir_p( os.path.join(layerFolder, 'toRegister')) registeredFolder = fc.mkdir_p( os.path.join(layerFolder, 'registered')) # Applying appropriate filters to make lowresEM and LM look similar for layer l imLM = IJ.openImage(imPaths['LM'][l]) imLM = fc.localContrast(imLM) imLMPath = os.path.join(toRegisterFolder, 'imLM_' + str(l).zfill(4) + '.tif') IJ.save(imLM, imLMPath) imEM = IJ.openImage(imPaths['EM'][l]) imEM = fc.localContrast(imEM) imEM = fc.blur(imEM, 2) imEMPath = os.path.join(toRegisterFolder, 'imEM_' + str(l).zfill(4) + '.tif') IJ.save(imEM, imEMPath) # Compute first a simple affine registration on the non-cropped images IJ.log( 'Computing affine and moving least squares alignment for layer ' + str(l).zfill(4)) firstStepRegistered = False # registration at first step with 1step/octave (less features) pLowRes = getSIFTMatchingParameters(nOctaves[0], 1.6, 16, 4000, 8, 4) featuresLM = getFeatures(imLMPath, pLowRes) featuresEM = getFeatures(imEMPath, pLowRes) matchingResults = getMatchingResults(featuresLM, featuresEM) if matchingResults is None: IJ.log( 'No registration matching at low resolution matching step 1 in layer ' + str(l).zfill(4)) else: model, inliers = matchingResults distance = PointMatch.meanDistance( inliers ) # mean displacement of the remaining matching features IJ.log('---Layer ' + str(l).zfill(4) + ' distance ' + str(distance) + ' px with ' + str(len(inliers)) + ' inliers') if distance > matchingThreshold[0]: IJ.log( 'Matching accuracy is lower than the threshold at the low resolution step 1 - ' + str(l).zfill(4) + ' - distance - ' + str(distance)) else: affTransform = model.createAffine() s1, s2 = getScalingFactors(affTransform) IJ.log('Layer ' + str(l).zfill(4) + ' scaling factors - step 1 - ' + str(s1) + ' - ' + str(s2) + '--' + str(s1 * s2) + ' affDeterminant ' + str(affTransform.getDeterminant()) + ' nInliers ' + str(len(inliers))) if (abs(s1 - 1) < 0.2) and ( abs(s2 - 1) < 0.2 ): # scaling in both directions should be close to 1 IJ.log('First step ok - layer ' + str(l).zfill(4)) firstStepRegistered = True loaderZ.serialize( affTransform, os.path.join(registeredFolder, 'affineSerialized')) if not firstStepRegistered: IJ.log( 'First step registration in layer ' + str(l).zfill(4) + ' with few features has failed. Trying with more features.' ) # registration at first step with 3steps/octave (more features) pLowRes = getSIFTMatchingParameters(3, 1.6, 64, 4000, 8, 4) #pLowRes = getSIFTMatchingParameters(nOctaves[0], 1.6, 16, 4000, 8, 4) # for BIB featuresLM = getFeatures(imLMPath, pLowRes) featuresEM = getFeatures(imEMPath, pLowRes) matchingResults = getMatchingResults(featuresLM, featuresEM) if matchingResults is None: IJ.log( 'No registration matching at low resolution matching step 1bis in layer ' + str(l).zfill(4)) else: model, inliers = matchingResults distance = PointMatch.meanDistance( inliers ) # mean displacement of the remaining matching features IJ.log('---Layer ' + str(l).zfill(4) + ' distance ' + str(distance) + ' px with ' + str(len(inliers)) + ' inliers') if distance > matchingThreshold[0]: IJ.log( 'Matching accuracy is lower than the threshold at the high resolution step 1bis - ' + str(l).zfill(4) + ' - distance - ' + str(distance)) else: affTransform = model.createAffine() s1, s2 = getScalingFactors(affTransform) IJ.log('Layer ' + str(l).zfill(4) + ' scaling factors - step 1bis - ' + str(s1) + ' - ' + str(s2) + '--' + str(s1 * s2) + ' affDeterminant ' + str(affTransform.getDeterminant()) + ' nInliers ' + str(len(inliers))) if (abs(s1 - 1) < 0.2) and ( abs(s2 - 1) < 0.2 ): # scaling in both directions should be close to 1 IJ.log('First step 1bis ok - layer ' + str(l).zfill(4)) firstStepRegistered = True loaderZ.serialize( affTransform, os.path.join(registeredFolder, 'affineSerialized')) if not firstStepRegistered: IJ.log('The two first step trials in layer ' + str(l).zfill(4) + ' have failed') else: # Affine transform and crop the LM, and compute a high res MLS matching with lock: # only one trakem working at a time # apply affTransform patch = Patch.createPatch(pZ, imLMPath) layerZ.add(patch) patch.setAffineTransform(affTransform) patch.updateBucket() # crop and export bb = Rectangle(0, 0, widthEM, heightEM) affineCroppedIm = loaderZ.getFlatImage( layerZ, bb, 1, 0x7fffffff, ImagePlus.GRAY8, Patch, layerZ.getAll(Patch), True, Color.black, None) affineCroppedImPath = os.path.join( toRegisterFolder, 'affineCroppedLM_' + str(l).zfill(4) + '.tif') IJ.save(affineCroppedIm, affineCroppedImPath) affineCroppedIm.close() layerZ.remove(patch) layerZ.recreateBuckets() pHighRes = getSIFTMatchingParameters(nOctaves[1], 1.6, 64, 4096, 8, 4) featuresLM = getFeatures(affineCroppedImPath, pHighRes) featuresEM = getFeatures(imEMPath, pHighRes) # get the MLS matchingResults = getMatchingResults(featuresLM, featuresEM) if matchingResults is None: IJ.log( 'It cannot be, there should be a good match given that an affine was computed. Layer ' + str(l).zfill(4)) else: model, inliers = matchingResults affTransform = model.createAffine() s1, s2 = getScalingFactors(affTransform) IJ.log('Second step determinant - layer ' + str(l).zfill(4) + ' - determinant - ' + str(affTransform.getDeterminant()) + ' nInliers ' + str(len(inliers)) + 'Scaling factors - step 2 - ' + str(s1) + ' - ' + str(s2)) if (abs(s1 - 1) < 0.2) and (abs(s2 - 1) < 0.2) and len( inliers ) > 50: # scaling in both directions should be close to 1 distance = PointMatch.meanDistance( inliers ) # mean displacement of the remaining matching features if distance > matchingThreshold[1]: IJ.log( 'Weird: matching accuracy is lower than the threshold at the high resolution step 2 - ' + str(l).zfill(4) + ' - distance - ' + str(distance)) else: mlst = MovingLeastSquaresTransform() mlst.setModel(AffineModel2D) mlst.setAlpha(1) mlst.setMatches(inliers) xmlMlst = mlst.toXML('\t') with open( os.path.join(registeredFolder, 'MLST.xml'), 'w') as f: f.write(xmlMlst) loaderZ.serialize( mlst, os.path.join(registeredFolder, 'mlstSerialized')) registrationStats.append( [l, distance, len(inliers)])
p, loader, layerset, nLayers = fc.getProjectUtils( fc.initTrakem(LMFolder, nLayers)) p.saveAs(projectPath, True) layerset.setDimensions(0, 0, width0, height0) for l, layer in enumerate(layerset.getLayers()): layerFolder = os.path.join(registrationFolder, 'layer_' + str(l).zfill(4)) registeredFolder = os.path.join(layerFolder, 'registered') MLSTPath = os.path.join(registeredFolder, 'MLST.xml') if os.path.isfile(MLSTPath): MLSTransform = CoordinateTransformXML().parse(MLSTPath) affineCroppedImPath = os.path.join( affineCroppedFolder, 'affineCropped_' + channel + '_' + str(l).zfill(4) + '.tif') patch = Patch.createPatch(p, affineCroppedImPath) layer.add(patch) patch.setCoordinateTransform( MLSTransform ) # does the order matter ? apparently yes, but I have to be sure that it is not an offset problem IJ.log('Setting the mlsTransform in layer ' + str(l) + ' ' + str(MLSTransform)) patch.updateBucket() if idChannel < len(channels) - 2: # if it is a fluochannel MLSTransformedFolder = fc.mkdir_p( os.path.join(LMFolder, 'MLS_Transformed_' + str(channel), '')) imp = loader.getFlatImage(layer, roiExport, 1, 0x7fffffff, ImagePlus.GRAY8, Patch, layer.getAll(Patch), True,