Пример #1
0
def crop_along_one_axis(ops, data, intervals, axis_type):
    """Crop along a single axis using Views.

    Parameters
    ----------
    intervals : List with two values specifying the start and the end of the interval.
    axis_type : Along which axis to crop. Can be ["X", "Y", "Z", "TIME", "CHANNEL"]
    """

    axis = get_axis(axis_type)
    interval_start = [
        data.min(d) if d != data.dimensionIndex(axis) else intervals[0]
        for d in range(0, data.numDimensions())
    ]
    interval_end = [
        data.max(d) if d != data.dimensionIndex(axis) else intervals[1]
        for d in range(0, data.numDimensions())
    ]

    interval = interval_start + interval_end
    interval = Intervals.createMinMax(*interval)

    output = ops.run("transform.crop", data, interval, True)

    return output
Пример #2
0
def crop_along_one_axis(ops, data, intervals, axis_type):
    """Crop along a single axis using Views.
 
    Parameters
    ----------
    intervals : List with two values specifying the start and the end of the interval.
    axis_type : Along which axis to crop. Can be ["X", "Y", "Z", "TIME", "CHANNEL"]
    """
 
    axis = get_axis(axis_type)
    interval_start = [data.min(d) if d != data.dimensionIndex(axis) else intervals[0] for d in range(0, data.numDimensions())]
    interval_end = [data.max(d) if d != data.dimensionIndex(axis) else intervals[1] for d in range(0, data.numDimensions())]
 
    interval = interval_start + interval_end
    interval = Intervals.createMinMax(*interval)
 
    output = ops.run("transform.crop", data, interval, True)
 
    return output
Пример #3
0
def crop(ops, data, intervals):
    """Crop along a one or more axis.
 
    Parameters
    ----------
    intervals : Dict specifying which axis to crop and with what intervals.
                Example :
                intervals = {'X' : [0, 50],
                             'Y' : [0, 50]}
    """
 
    intervals_start = [data.min(d) for d in range(0, data.numDimensions())]
    intervals_end = [data.max(d) for d in range(0, data.numDimensions())]
 
    for axis_type, interval in intervals.items():
        index = data.dimensionIndex(get_axis(axis_type))
        intervals_start[index] = interval[0]
        intervals_end[index] = interval[1]
 
    intervals = Intervals.createMinMax(*intervals_start + intervals_end)
 
    output = ops.run("transform.crop", data, intervals, True)
    
    return output
Пример #4
0
def crop(ops, data, intervals):
    """Crop along a one or more axis.
 
    Parameters
    ----------
    intervals : Dict specifying which axis to crop and with what intervals.
                Example :
                intervals = {'X' : [0, 50],
                             'Y' : [0, 50]}
    """

    intervals_start = [data.min(d) for d in range(0, data.numDimensions())]
    intervals_end = [data.max(d) for d in range(0, data.numDimensions())]

    for axis_type, interval in intervals.items():
        index = data.dimensionIndex(get_axis(axis_type))
        intervals_start[index] = interval[0]
        intervals_end[index] = interval[1]

    intervals = Intervals.createMinMax(*intervals_start + intervals_end)

    output = ops.run("transform.crop", data, intervals, True)

    return output
from net.imglib2.util import Intervals
from net.imagej.axis import Axes
from net.imglib2.type.numeric.real import FloatType

from java.lang.Math import floor

# crop PSF to desired size, this makes decon run faster with little effect on final quality
psfX = psf.dimension(data.dimensionIndex(Axes.X))
psfY = psf.dimension(data.dimensionIndex(Axes.Y))
psfZ = psf.dimension(2)

psf_ = ops.transform().crop(
    psf.getImgPlus(),
    Intervals.createMinMax(psfX / 2 - psfXSize / 2, psfY / 2 - psfYSize / 2, 0,
                           psfX / 2 + psfXSize / 2 - 1,
                           psfY / 2 + psfYSize / 2 - 1, psfZ - 1))

psf_ = ops.convert().float32(psf_)

maxPSF = ops.stats().max(psf_).getRealFloat()
psfBackground = psfBackgroundPercent * maxPSF

# subtract background from psf
for t in psf_:
    val = t.getRealFloat() - psfBackground
    if val < 0:
        val = 0
    t.setReal(val)

# normalize psf
# first take a look at the size and type of each dimension
for d in range(data.numDimensions()):
    print "axis d: type: " + str(data.axis(d).type()) + " length: " + str(
        data.dimension(d))

img = data.getImgPlus()

xLen = data.dimension(data.dimensionIndex(Axes.X))
yLen = data.dimension(data.dimensionIndex(Axes.Y))
zLen = data.dimension(data.dimensionIndex(Axes.Z))
cLen = data.dimension(data.dimensionIndex(Axes.CHANNEL))

# crop a channel
c0 = ops.transform().crop(
    img, Intervals.createMinMax(0, 0, 0, 0, xLen - 1, yLen - 1, 0, zLen - 1))
c0.setName("c0")

# crop both channels at z=12
z12 = ops.transform().crop(
    img, Intervals.createMinMax(0, 0, 0, 12, xLen - 1, yLen - 1, cLen - 1, 12))
z12.setName("z12")

# crop channel 0 at z=12
c0z12 = ops.transform().crop(
    img, Intervals.createMinMax(0, 0, 0, 12, xLen - 1, yLen - 1, 0, 12))
c0z12.setName("c0z12")

# crop an roi at channel 0, z=12
roiC0z12 = ops.transform().crop(
    img, Intervals.createMinMax(150, 150, 0, 12, 200, 200, 0, 12))
from net.imglib2.util import Intervals
from net.imagej.axis import Axes

# first take a look at the size and type of each dimension
for d in range(data.numDimensions()):
	print "axis d: type: "+str(data.axis(d).type())+" length: "+str(data.dimension(d))

img=data.getImgPlus()

xLen = data.dimension(data.dimensionIndex(Axes.X))
yLen = data.dimension(data.dimensionIndex(Axes.Y))
zLen = data.dimension(data.dimensionIndex(Axes.Z))
cLen = data.dimension(data.dimensionIndex(Axes.CHANNEL))

# crop a channel
c0=ops.transform().crop(img, Intervals.createMinMax(0, 0, 0,0,xLen-1, yLen-1, 0, zLen-1))
c0.setName("c0")

# crop both channels at z=12
z12=ops.transform().crop(img, Intervals.createMinMax(0,0,0,12, xLen-1, yLen-1, cLen-1, 12))
z12.setName("z12")

# crop channel 0 at z=12
c0z12=ops.transform().crop(img, Intervals.createMinMax(0,0,0,12, xLen-1, yLen-1, 0, 12))
c0z12.setName("c0z12")

# crop an roi at channel 0, z=12
roiC0z12=ops.transform().crop(img, Intervals.createMinMax(150,150,0,12, 200, 200, 0, 12))
roiC0z12.setName("roiC0z12")
Пример #8
0
from net.imglib2 import Point
from net.imglib2.algorithm.region.hypersphere import HyperSphere
from net.imglib2.util import Intervals

# crop PSF to desired size, this makes conv and decon run faster with little effect on final quality
croppedX = 64
croppedY = 64

kernelX = kernel.dimension(0)
kernelY = kernel.dimension(1)
kernelZ = kernel.dimension(2)

kernel_ = ops.transform().crop(
    kernel,
    Intervals.createMinMax(kernelX / 2 - croppedX / 2,
                           kernelY / 2 - croppedY / 2, 0,
                           kernelX / 2 + croppedX / 2 - 1,
                           kernelY / 2 + croppedY / 2 - 1, kernelZ - 1))

# convolve kernel with phantom
convolved = ops.filter().convolve(img, kernel)

# make convolved an ImgPlus
convolved = ops.create().imgPlus(convolved)
convolved.setName("convolved")

deconvolved = ops.create().img(convolved)
deconvolved = ops.deconvolve().richardsonLucy(deconvolved, convolved, kernel,
                                              [0, 0, 0], None, None, None,
                                              None, 30, True, True)
def smooth_temporal_gradient(ops, img, sigma_xy, sigma_t, frame_start,
                             frame_end, normalize_output):
    """
    Smooth input image (imglib2 array) xyt with Gaussian and build temporal gradient 
    from frame_start to frame_end
    """

    n = img.numDimensions()
    assert n == 3, "Input data needs to be 3-dimensional, 2D + time"

    dims = [img.dimension(d) for d in range(n)]
    dim_x, dim_y, dim_t = dims

    if frame_end == -1:
        frame_end = dim_t

    if frame_end > dim_t:
        frame_end = dim_t

    assert frame_start < frame_end, "'Frame start' must be smaller than 'Frame end'"

    ## crop image temporally by using a View
    # img_crop = Views.interval(img, [0, 0, frame_start], [dim_x-1, dim_y-1, frame_end-1])

    # or by cropping without view
    img_crop = ops.transform().crop(
        img,
        Intervals.createMinMax(0, 0, frame_start, dim_x - 1, dim_y - 1,
                               frame_end - 1))

    # create output for smoothing
    img_smooth = ArrayImgFactory(FloatType()).create(img_crop)

    # update dimensions (after cropping)
    dims = [img_crop.dimension(d) for d in range(n)]

    # smooth with Gaussian (use mirror as border treatment)
    Gauss3.gauss([sigma_xy, sigma_xy, sigma_t], Views.extendBorder(img_crop),
                 img_smooth)

    # compute gradient along (time) axis 2
    gradient = ArrayImgFactory(FloatType()).create(dims)
    PartialDerivative.gradientBackwardDifference(
        Views.extendBorder(img_smooth), gradient, 2)

    # separate response into growing and shrinking part by thresholding and multiplying that mask
    thresholded = ops.run("threshold.apply", gradient, FloatType(0.))
    mask = ops.convert().float32(thresholded)
    grow = ops.run("math.multiply", gradient, mask)

    # same for shrinking, but negate befor to obtain negative part
    gradient_neg = ops.run("math.multiply", gradient, -1)
    thresholded = ops.run("threshold.apply", gradient_neg, FloatType(0.))
    mask = ops.convert().float32(thresholded)
    shrink = ops.run("math.multiply", gradient, mask)

    # allign growth and shrink by offset of 1 in t dimesnion
    shrink = Views.interval(shrink, [0, 0, 0],
                            [dims[0] - 1, dims[1] - 1, dims[2] - 2])
    grow = Views.interval(grow, [0, 0, 1],
                          [dims[0] - 1, dims[1] - 1, dims[2] - 1])
    img_smooth = Views.interval(img_smooth, [0, 0, 0],
                                [dims[0] - 1, dims[1] - 1, dims[2] - 2])

    # concatenate output
    if normalize_output:
        shrink = rescale_uint8(ops, shrink)
        grow = rescale_uint8(ops, grow)
        img_smooth = rescale_uint8(ops, img_smooth)

    out = Views.stack([shrink, grow, img_smooth])

    # permute channel with time to make time last dim (for back-conversion)
    out = Views.permute(out, 2, 3)

    # crop view 1st time point result (empty since using backwardDifferences)
    out = Views.interval(out, [0, 0, 0, 1],
                         [out.dimension(d) - 1 for d in range(4)])

    # get ImagePlus back and set correct dimensions
    out_imp = IJF.wrap(out, "Grow and shrink")

    # resize IMP now one frame less (see crop view)
    out_imp.setDimensions(3, 1, frame_end - frame_start - 2)
    out_imp.setDisplayMode(IJ.COMPOSITE)
    #    IJ.save(out_imp, "H:/projects/032_loose_speckle/grow_and_shrink_ij.tif")

    return out_imp
Пример #10
0
# @Dataset data
# @UIService ui
# @DisplayService display

# to run this tutorial run 'file->Open Samples->Confocal Series' and make sure that
# confocal-series.tif is the active image

from net.imglib2.util import Intervals
from net.imagej.axis import Axes

# first take a look at the size and type of each dimension
for d in range(data.numDimensions()):
    print "axis d: type: " + str(data.axis(d).type()) + " length: " + str(
        data.dimension(d))

img = data.getImgPlus()

cropDims = []

for d in range(data.numDimensions()):
    print "axis d: type: " + str(data.axis(d).type()) + " length: " + str(
        data.dimension(d))

for d in range(data.numDimensions()):
    cropDims.append(data.dimension(d) / 4)
for d in range(data.numDimensions()):
    cropDims.append(3 * (data.dimension(d) / 4) - 1)

cropped = ops.image().crop(img, Intervals.createMinMax(cropDims))
ui.show(cropped)
Пример #11
0
from net.imglib2.util import Intervals
from net.imagej.axis import Axes

# first take a look at the size and type of each dimension
for d in range(data.numDimensions()):
	print "axis d: type: "+str(data.axis(d).type())+" length: "+str(data.dimension(d))

img=data.getImgPlus()

xLen = data.dimension(data.dimensionIndex(Axes.X))
yLen = data.dimension(data.dimensionIndex(Axes.Y))
zLen = data.dimension(data.dimensionIndex(Axes.Z))
cLen = data.dimension(data.dimensionIndex(Axes.CHANNEL))

# crop a channel
c0=ops.image().crop(img, Intervals.createMinMax(0, 0, 0,0,xLen-1, yLen-1, 0, zLen-1))
c0.setName("c0")

# crop both channels at z=12
z12=ops.image().crop(img, Intervals.createMinMax(0,0,0,12, xLen-1, yLen-1, cLen-1, 12))
z12.setName("z12")

# crop channel 0 at z=12
c0z12=ops.image().crop(img, Intervals.createMinMax(0,0,0,12, xLen-1, yLen-1, 0, 12))
c0z12.setName("c0z12")

# crop an roi at channel 0, z=12
roiC0z12=ops.image().crop(img, Intervals.createMinMax(150,150,0,12, 200, 200, 0, 12))
roiC0z12.setName("roiC0z12")
    for i, z in enumerate(range(z_start, z_end)):

        log.info(
            "Processing frame %i/%i for ROIs #%i and #%i (%i total detected ROIs)"
            % (z_start + i, z_end, j, j + 1, len(rois)))

        # Compute the translation
        dx = int(x_shift + total_x_shift) * -1
        dy = int(y_shift + total_y_shift) * -1

        total_x_shift += x_shift
        total_y_shift += y_shift

        # Get only the frame cooresponding to the actual Z/T position
        intervals = Intervals.createMinMax(0, 0, z,
                                           ds.getWidth() - 1,
                                           ds.getHeight() - 1, z)
        single_frame = ops.transform().crop(img, intervals)

        # Pad the frame so the outisde of the image dimensions values are set to 0
        padded_frame = ops.transform().extendZero(single_frame)

        # Do the translation
        translated_frame = ops.transform().translate(padded_frame, [dx, dy])

        # Cleanup
        interval2d = Intervals.createMinMax(0, 0,
                                            ds.getWidth() - 1,
                                            ds.getHeight() - 1)
        translated_frame = Views.interval(translated_frame, interval2d)
        translated_frame = ops.transform().dropSingletonDimensions(
# create an empty image
phantom = ops.create().img([xSize, ySize, zSize])

# make phantom an ImgPlus
phantom = ops.create().imgPlus(phantom)

location = Point(phantom.numDimensions())
location.setPosition([xSize / 2, ySize / 2, zSize / 2])

hyperSphere = HyperSphere(phantom, location, 10)

for value in hyperSphere:
    value.setReal(100)

phantom.setName("phantom")

affine = AffineTransform3D()
affine.scale(1, 1, 0.4)

interpolatedImg = Views.interpolate(Views.extendZero(phantom),
                                    NLinearInterpolatorFactory())

phantom = Views.interval(
    Views.raster(RealViews.affine(interpolatedImg, affine)),
    Intervals.createMinMax(0, 0, 18, 255, 255, 82))

# make phantom an ImgPlus
phantom = ops.create().imgPlus(ops.copy().iterableInterval(
    Views.zeroMin(phantom)))
phantom.setName('phantom')
converted = ops.convert().float32(data)

# Get the first frame (TODO: find a more convenient way !)
t_dim = data.dimensionIndex(Axes.TIME)
interval_start = []
interval_end = []
for d in range(0, data.numDimensions()):
    if d != t_dim:
        interval_start.append(0)
        interval_end.append(data.dimension(d) - 1)
    else:
        interval_start.append(0)
        interval_end.append(0)

intervals = interval_start + interval_end
intervals = Intervals.createMinMax(*intervals)

first_frame = ops.transform().crop(converted, intervals)

# Allocate output memory (wait for hybrid CF version of slice)
subtracted = ops.create().img(converted)

# Create the op
sub_op = ops.op("math.subtract", first_frame, first_frame)

# Setup the fixed axis
fixed_axis = [d for d in range(0, data.numDimensions()) if d != t_dim]

# Run the op
ops.slice(subtracted, converted, sub_op, fixed_axis)
converted = ops.convert().float32(data)
 
# Get the first frame (TODO: find a more convenient way !)
t_dim = data.dimensionIndex(Axes.TIME)
interval_start = []
interval_end = []
for d in range(0, data.numDimensions()):
    if d != t_dim:
        interval_start.append(0)
        interval_end.append(data.dimension(d) - 1)
    else:
        interval_start.append(0)
        interval_end.append(0)
         
intervals = interval_start + interval_end
intervals = Intervals.createMinMax(*intervals)
 
first_frame = ops.transform().crop(converted, intervals)
 
# Allocate output memory (wait for hybrid CF version of slice)
subtracted = ops.create().img(converted)
 
# Create the op
sub_op = ops.op("math.subtract", first_frame, first_frame)
 
# Setup the fixed axis
fixed_axis = [d for d in range(0, data.numDimensions()) if d != t_dim]
 
# Run the op
ops.slice(subtracted, converted, sub_op, fixed_axis)