Ejemplo n.º 1
0
def generateOverlay(project, patch, shape):
    oWidth  = patch.getOWidth()
    oHeight = patch.getOHeight()

    overlayp = ByteProcessor(oWidth, oHeight)
    # TODO: Use ShortProcessor instead of ByteProcessor
    imp      = ImagePlus("Patch %s" % patch, overlayp)
    stepX    = oWidth/shape[0]
    stepY    = oHeight/shape[1]
    color    = 1

    for x in xrange(shape[0]):
        offsetX = x * stepX
        for y in xrange(shape[1]):
            offsetY = y * stepY
            imp.setRoi(offsetX, offsetY, stepX, stepY)
            imp.getProcessor().setValue(color)
            imp.getProcessor().fill()
            color += 1
    imp.setRoi(None)
    
    overlayPatch = Patch(project, "%s_overlay" % patch, 0.0, 0.0, imp)
    
    overlayPatch.setAffineTransform(patch.getAffineTransform())
    overlayPatch.setCoordinateTransform(patch.getCoordinateTransform())
    return overlayPatch
Ejemplo n.º 2
0
def extract_stack_under_arealist():
    # Check that a Display is open
    display = Display.getFront()
    if display is None:
        IJ.log("Open a TrakEM2 Display first!")
        return
    # Check that an AreaList is selected and active:
    ali = display.getActive()
    if ali is None or not isinstance(ali, AreaList):
        IJ.log("Please select an AreaList first!")
        return

    # Get the range of layers to which ali paints:
    ls = display.getLayerSet()
    ifirst = ls.indexOf(ali.getFirstLayer())
    ilast = ls.indexOf(ali.getLastLayer())
    layers = display.getLayerSet().getLayers().subList(ifirst, ilast + 1)

    # Create a stack with the dimensions of ali
    bounds = ali.getBoundingBox()
    stack = ImageStack(bounds.width, bounds.height)

    # Using 16-bit. To change to 8-bit, use GRAY8 and ByteProcessor in the two lines below:
    type = ImagePlus.GRAY16
    ref_ip = ShortProcessor(bounds.width, bounds.height)

    for layer in layers:
        area = ali.getArea(layer)
        z = layer.getZ()
        ip = ref_ip.createProcessor(bounds.width, bounds.height)
        if area is None:
            stack.addSlice(str(z), bp)
            continue

        # Create a ROI from the area of ali at layer:
        aff = ali.getAffineTransformCopy()
        aff.translate(-bounds.x, -bounds.y)
        roi = ShapeRoi(area.createTransformedArea(aff))

        # Create a cropped snapshot of the images at layer under ali:
        flat = Patch.makeFlatImage(type, layer, bounds, 1.0,
                                   layer.getDisplayables(Patch), Color.black)
        b = roi.getBounds()
        flat.setRoi(roi)
        ip.insert(flat.crop(), b.x, b.y)

        # Clear the outside of ROI (ShapeRoi is a non-rectangular ROI type)
        bimp = ImagePlus("", ip)
        bimp.setRoi(roi)
        ip.setValue(0)
        ip.setBackgroundValue(0)
        IJ.run(bimp, "Clear Outside", "")

        # Accumulate slices
        stack.addSlice(str(z), ip)

    imp = ImagePlus("AreaList stack", stack)
    imp.setCalibration(ls.getCalibrationCopy())
    imp.show()
Ejemplo n.º 3
0
def extract_stack_under_arealist():
	# Check that a Display is open
	display = Display.getFront()
	if display is None:
		IJ.log("Open a TrakEM2 Display first!")
		return
	# Check that an AreaList is selected and active:
	ali = display.getActive()
	if ali is None or not isinstance(ali, AreaList):
		IJ.log("Please select an AreaList first!")
		return

	# Get the range of layers to which ali paints:
	ls = display.getLayerSet()
	ifirst = ls.indexOf(ali.getFirstLayer())
	ilast = ls.indexOf(ali.getLastLayer())
	layers = display.getLayerSet().getLayers().subList(ifirst, ilast +1)

	# Create a stack with the dimensions of ali
	bounds = ali.getBoundingBox()
	stack = ImageStack(bounds.width, bounds.height)

	# Using 16-bit. To change to 8-bit, use GRAY8 and ByteProcessor in the two lines below:
	type = ImagePlus.GRAY16
	ref_ip = ShortProcessor(bounds.width, bounds.height)

	for layer in layers:
		area = ali.getArea(layer)
		z = layer.getZ()
		ip = ref_ip.createProcessor(bounds.width, bounds.height)
		if area is None:
			stack.addSlice(str(z), bp)
			continue

		# Create a ROI from the area of ali at layer:
		aff = ali.getAffineTransformCopy()
		aff.translate(-bounds.x, -bounds.y)
		roi = ShapeRoi(area.createTransformedArea(aff))

		# Create a cropped snapshot of the images at layer under ali:
		flat = Patch.makeFlatImage(type, layer, bounds, 1.0, layer.getDisplayables(Patch), Color.black)
		b = roi.getBounds()
		flat.setRoi(roi)
		ip.insert(flat.crop(), b.x, b.y)

		# Clear the outside of ROI (ShapeRoi is a non-rectangular ROI type)
		bimp = ImagePlus("", ip)
		bimp.setRoi(roi)
		ip.setValue(0)
		ip.setBackgroundValue(0)
		IJ.run(bimp, "Clear Outside", "")

		# Accumulate slices
		stack.addSlice(str(z), ip)

	imp = ImagePlus("AreaList stack", stack)
	imp.setCalibration(ls.getCalibrationCopy())
	imp.show()
Ejemplo n.º 4
0
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)
Ejemplo n.º 5
0
    ## Import tiles
    # Get all layers
    f = open(os.path.join(data_dir, "tile_location.txt"), "r")

    z_list = []
    for l in f.readlines():

        elem = l[:-1].split(" ")
        fname = elem[0]
        xloc = int(elem[1])
        yloc = int(elem[2])
        zloc = int(elem[3])
        z_list.append(zloc)

        imp = IJ.openImage(os.path.join(img_dir, fname))
        patch = Patch(project, imp.title, xloc, yloc, imp)
        patch.project.loader.addedPatchFrom(os.path.join(img_dir, fname),
                                            patch)

        layer = layerset.getLayer(zloc, 1, True)
        layer.add(patch)

    f.close()

    front = Display.getFront()
    bounds = Rectangle(x=0, y=0, width=20000, height=20000)
    front.resizeCanvas(bounds)

    z_list.sort()
    if z_list[0] != 0:
        layer = layerset.getLayer(0, 1, False)
Ejemplo n.º 6
0
    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
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)])
    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()
Ejemplo n.º 9
0
    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,
Ejemplo n.º 10
0
preprocessor_script_path = "/tmp/trakem2-n5.bsh"
with open(preprocessor_script_path, 'w') as f:
    f.write(script)

# Create as many layers as indices in the Z dimension
layers = []
for z in xrange(dimensions[2]):
    layer = layerset.getLayer(z, 1.0, True)  # create if not there
    layerset.addSilently(layer)
    layers.append(layer)
    # Add a single Patch instance per Layer, whose image is a 2D crop of the N5 volume
    if 0 == layer.getDisplayables().size():
        index = layerset.getLayerIndex(layer.getId())
        patch = Patch(project, str(z), dimensions[0], dimensions[1],
                      dimensions[0], dimensions[1], img_type, 1.0, Color.black,
                      True, 0, 255, AffineTransform(), "")
        layer.add(patch, False)  # don't update displays
        project.getLoader().setPreprocessorScriptPathSilently(
            patch, preprocessor_script_path)

layerset.recreateBuckets(layers, True)
Display.updateLayerScroller(layerset)

# Export for CATMAID from raw images (strategy=0)
"""
saver = Saver("jpg")
saver.setQuality(0.75)
ExportMultilevelTiles.makePrescaledTiles(layers, Patch, Rectangle(0, 0, dimensions[0], dimensions[1]),
                                         -1, img_type, tgt_dir, 0, saver, tile_side, 1,
                                         True, True,