Exemplo n.º 1
0
def rotatedView(img, angle, enlarge=True, extend=Views.extendBorder):
  """ Return a rotated view of the image, around the Z axis,
      with an expanded (or reduced) interval view so that all pixels are exactly included.

      img: a RandomAccessibleInterval
      angle: in degrees
  """
  cx = img.dimension(0) / 2.0
  cy = img.dimension(1) / 2.0
  toCenter = AffineTransform2D()
  toCenter.translate(-cx, -cy)
  rotation = AffineTransform2D()
  # Step 1: place origin of rotation at the center of the image
  rotation.preConcatenate(toCenter)
  # Step 2: rotate around the Z axis
  rotation.rotate(radians(angle))
  # Step 3: undo translation to the center
  rotation.preConcatenate(toCenter.inverse())
  rotated = RV.transform(Views.interpolate(extend(img),
                                           NLinearInterpolatorFactory()), rotation)
  if enlarge:
    # Bounds:
    bounds = repeat((sys.maxint, 0)) # initial upper- and lower-bound values  
                                     # for min, max to compare against  
    transformed = zeros(2, 'f')
    for corner in product(*zip(repeat(0), Intervals.maxAsLongArray(img))):
      rotation.apply(corner, transformed)
      bounds = [(min(vmin, int(floor(v))), max(vmax, int(ceil(v))))
                for (vmin, vmax), v in zip(bounds, transformed)]
    minC, maxC = map(list, zip(*bounds)) # transpose list of 2 pairs
                                         # into 2 lists of 2 values
    imgRot = Views.zeroMin(Views.interval(rotated, minC, maxC))
  else:
    imgRot = Views.interval(rotated, img)
  return imgRot
Exemplo n.º 2
0
    def asNormalizedUnsignedByteArrayImg(interval, invert, blockRadius, n_bins,
                                         slope, matrices, copy_threads, index,
                                         imp):
        sp = imp.getProcessor()  # ShortProcessor
        sp.setRoi(interval.min(0), interval.min(1),
                  interval.max(0) - interval.min(0) + 1,
                  interval.max(1) - interval.min(1) + 1)
        sp = sp.crop()
        if invert:
            sp.invert()
        CLAHE.run(
            ImagePlus("", sp), blockRadius, n_bins, slope, None
        )  # far less memory requirements than NormalizeLocalContrast, and faster.
        minimum, maximum = autoAdjust(sp)

        # Transform and convert image to 8-bit, mapping to display range
        img = ArrayImgs.unsignedShorts(
            sp.getPixels(), [sp.getWidth(), sp.getHeight()])
        sp = None
        affine = AffineTransform2D()
        affine.set(matrices[index])
        imgI = Views.interpolate(Views.extendZero(img),
                                 NLinearInterpolatorFactory())
        imgA = RealViews.transform(imgI, affine)
        imgT = Views.zeroMin(Views.interval(imgA, img))
        imgMinMax = convert(imgT, RealUnsignedByteConverter(minimum, maximum),
                            UnsignedByteType)
        aimg = ArrayImgs.unsignedBytes(Intervals.dimensionsAsLongArray(img))
        ImgUtil.copy(ImgView.wrap(imgMinMax, aimg.factory()), aimg,
                     copy_threads)
        img = imgI = imgA = imgT = imgMinMax = None
        return aimg
def getViewFromImp(imp, r=None):
    # r is a java.awt.rectangle
    im = IL.wrapByte(imp)
    if r is None:
        r = Rectangle(0, 0, imp.getWidth(), imp.getHeight())
    v = Views.zeroMin(
        Views.interval(im, [r.x, r.y],
                       [r.x + r.width - 1, r.y + r.height - 1]))
    return v
Exemplo n.º 4
0
def prepareImgForDeconvolution(img, affine3D, interval):
  """
  Transform the img for deconvolution, taking care of pixels with zero value within the image
  and setting the appropriate values for outside the image, and cropping to the interval.
  """
  return Views.zeroMin(TransformView.transformView(img, affine3D, interval,
                                                   MultiViewDeconvolution.minValueImg,
                                                   MultiViewDeconvolution.outsideValueImg,
                                                   1)) # 1: linear interpolation
  """
Exemplo n.º 5
0
 def get(self, path):
   transform = self.transformsDict[path]
   img = self.loader.get(path)
   imgE = Views.extendZero(img)
   imgI = Views.interpolate(imgE, NLinearInterpolatorFactory())
   imgT = RealViews.transform(imgI, transform)
   minC = self.roi[0] if self.roi else [0] * img.numDimensions()
   maxC = self.roi[1] if self.roi else [img.dimension(d) -1 for d in xrange(img.numDimensions())]
   imgO = Views.zeroMin(Views.interval(imgT, minC, maxC))
   return ImgView.wrap(imgO, img.factory()) if self.asImg else imgO
Exemplo n.º 6
0
 def makeCell(self, index):
     self.preloadCells(index)  # preload others in the background
     img = self.loadImg(self.filepaths[index])
     affine = AffineTransform2D()
     affine.set(self.matrices[index])
     imgI = Views.interpolate(Views.extendZero(img),
                              NLinearInterpolatorFactory())
     imgA = RealViews.transform(imgI, affine)
     imgT = Views.zeroMin(Views.interval(imgA, self.interval))
     aimg = img.factory().create(self.interval)
     ImgUtil.copy(ImgView.wrap(imgT, aimg.factory()), aimg)
     return Cell(self.cell_dimensions, [0, 0, index], aimg.update(None))
Exemplo n.º 7
0
 def keyPressed(self, ke):
   keyCode = ke.getKeyCode()
   if ke.isControlDown() and keyCode in Navigator.moves:
     d, sign = Navigator.moves[keyCode]
     inc = 200 if ke.isShiftDown() else 20
     mins[d] += sign * inc
     maxs[d] += sign * inc
     # Replace source with shifted cropped volume
     fsource.set(stack, Views.zeroMin(Views.interval(imgE, FinalInterval(mins, maxs))))
     imp.updateVirtualSlice()
     return
   # Else, pass the event onto other listeners
   for kl in kls:
     kl.keyPressed(ke)
Exemplo n.º 8
0
 def crop(event):
   global cropped, cropped_imp
   coords = [int(float(tf.getText())) for tf in textfields]
   minC = [max(0, c) for c in coords[0:3]]
   maxC = [min(d -1, c) for d, c in izip(Intervals.dimensionsAsLongArray(images[0]), coords[3:6])]
   storeRoi(minC, maxC)
   print "ROI min and max coordinates"
   print minC
   print maxC
   cropped = [Views.zeroMin(Views.interval(img, minC, maxC)) for img in images]
   cropped_imp = showAsStack(cropped, title="cropped")
   cropped_imp.setDisplayRange(imp.getDisplayRangeMin(), imp.getDisplayRangeMax())
   if cropContinuationFn:
     cropContinuationFn(images, minC, maxC, cropped, cropped_imp)
Exemplo n.º 9
0
    def asNormalizedUnsignedByteArrayImg(interval, invert, blockRadius, n_bins,
                                         slope, matrices, index, imp):
        sp = imp.getProcessor()  # ShortProcessor
        # Crop to interval if needed
        x = interval.min(0)
        y = interval.min(1)
        width = interval.max(0) - interval.min(0) + 1
        height = interval.max(1) - interval.min(1) + 1
        if 0 != x or 0 != y or sp.getWidth() != width or sp.getHeight(
        ) != height:
            sp.setRoi(x, y, width, height)
            sp = sp.crop()

        if invert:
            sp.invert()

        CLAHE.run(
            ImagePlus("", sp), blockRadius, n_bins, slope, None
        )  # far less memory requirements than NormalizeLocalContrast, and faster.
        minimum, maximum = autoAdjust(sp)

        # Transform and convert image to 8-bit, mapping to display range
        img = ArrayImgs.unsignedShorts(
            sp.getPixels(), [sp.getWidth(), sp.getHeight()])
        sp = None
        imp = None
        # Must use linear interpolation for subpixel precision
        affine = AffineTransform2D()
        affine.set(matrices[index])
        imgI = Views.interpolate(Views.extendZero(img),
                                 NLinearInterpolatorFactory())
        imgA = RealViews.transform(imgI, affine)
        imgT = Views.zeroMin(Views.interval(imgA, img))
        # Convert to 8-bit
        imgMinMax = convert2(imgT,
                             RealUnsignedByteConverter(minimum, maximum),
                             UnsignedByteType,
                             randomAccessible=False)  # use IterableInterval
        aimg = ArrayImgs.unsignedBytes(Intervals.dimensionsAsLongArray(img))
        # ImgUtil copies multi-threaded, which is not appropriate here as there are many other images being copied too
        #ImgUtil.copy(ImgView.wrap(imgMinMax, aimg.factory()), aimg)

        # Single-threaded copy
        copier = createBiConsumerTypeSet(UnsignedByteType)
        LoopBuilder.setImages(imgMinMax, aimg).forEachPixel(copier)

        img = imgI = imgA = imgMinMax = imgT = None
        return aimg
def twoStep(index=0):
    # The current way:
    img = klb.readFull(filepaths[index])  # klb_loader.get(filepaths[index])
    imgE = Views.extendZero(img)
    imgI = Views.interpolate(imgE, NLinearInterpolatorFactory())
    imgT = RealViews.transform(imgI, cmIsotropicTransforms[index])
    imgB = Views.zeroMin(Views.interval(imgT, roi[0],
                                        roi[1]))  # bounded: crop with ROI
    imgBA = ArrayImgs.unsignedShorts(Intervals.dimensionsAsLongArray(imgB))
    ImgUtil.copy(ImgView.wrap(imgB, imgBA.factory()), imgBA)
    imgP = prepareImgForDeconvolution(
        imgBA,
        affine3D(fineTransformsPostROICrop[index]).inverse(),
        FinalInterval([0, 0, 0], [imgB.dimension(d) - 1 for d in xrange(3)]))
    # Copy transformed view into ArrayImg for best performance in deconvolution
    imgA = ArrayImgs.floats(Intervals.dimensionsAsLongArray(imgP))
    ImgUtil.copy(ImgView.wrap(imgP, imgA.factory()), imgA)
    IL.wrap(imgA, "two step").show()
Exemplo n.º 11
0
def filterBankRotations(img,
                        angles=xrange(0, 46, 9), # sequence, in degrees
                        filterBankFn=filterBank, # function that takes an img as sole positional argument
                        outputType=FloatType()):
  """ img: a RandomAccessibleInterval.
      filterBankFn: the function from which to obtain a sequence of ImgMath ops.
      angles: a sequence of angles in degrees.
      outputType: for materializing rotated operations and rotating them back.

      For every angle, will prepare a rotated view of the image,
      then create a list of ops on the basis of that rotated view,
      then materialize each op into an image so that an unrotated view
      can be returned back.

      returns a list of unrotated views, each containing the values of applying
      each op to the rotated view. 
  """
  ops_rotations = []
  
  for angle in angles:
    imgRot = img if 0 == angle else rotatedView(img, angle)
    ops = filterBankFn(imgRot)

    # Materialize these two combination ops and rotate them back (rather, a rotated view)
    interval = Intervals.translate(img, [(imgRot.dimension(d) - img.dimension(d)) / 2
                                         for d in xrange(img.numDimensions())])
    for op in ops:
      imgOpRot = compute(op).intoArrayImg(outputType)
      if 0 == angle:
        ops_rotations.append(imgOpRot)
        continue
      # Rotate them back and crop view
      imgOpUnrot = rotatedView(imgOpRot, -angle, enlarge=False)
      imgOp = Views.zeroMin(Views.interval(imgOpUnrot, interval))
      #if angle == 0 or angle == 45:
      #  IL.wrap(imgOpRot, "imgOpRot angle=%i" % angle).show()
      #  IL.wrap(imgOpUnrot, "imgOpUnrot angle=%i" % angle).show()
      #  IL.wrap(imgOp, "imgOp angle=%i" % angle).show()
      ops_rotations.append(imgOp)
  
  return ops_rotations
Exemplo n.º 12
0
# by using the 'transformed' float array.

# We compute the bounds by, for every corner, checking if the floor of each dimension
# of a corner coordinate is smaller than the previously found minimum value,
# and by checking if the ceil of each corner coordinate is larger than the
# previously found value, packing the new pair of minimum and maximum values
# into the list of pairs that is 'bounds'.

# Notice the min coordinates can have negative values, as the rotated image
# has pixels now somewhere to the left and up from the top-left 0,0,0 origin
# of coordinates. That's why we use Views.zeroMin, to ensure that downstream
# uses of the transformed image see it as fitting within bounds that start at 0,0,0.

bounds = repeat(
    (sys.maxint, 0)
)  # initial upper- and lower-bound values for min, max to compare against
transformed = zeros(img.numDimensions(), 'f')

for corner in product(*zip(repeat(0), Intervals.maxAsLongArray(img))):
    rotation.apply(corner, transformed)
    bounds = [(min(vmin, int(floor(v))), max(vmax, int(ceil(v))))
              for (vmin, vmax), v in zip(bounds, transformed)]

minC, maxC = map(list, zip(*bounds))  # transpose list of lists
imgRot2dFit = IL.wrap(Views.zeroMin(Views.interval(rotated, minC, maxC)),
                      imp.getTitle() + " - rot2dFit")
imgRot2dFit.show()

matrix = rotation.getRowPackedCopy()
pprint([list(matrix[i:i + 4]) for i in xrange(0, 12, 4)])
Exemplo n.º 13
0
from itertools import imap, izip, product, combinations
from jarray import array, zeros
import os, csv
from java.util.concurrent import Executors, Callable
from java.util import ArrayList
from ij import IJ
from net.imglib2.algorithm.math import ImgMath, ImgSource
from net.imglib2.img.array import ArrayImgs
from net.imglib2.realtransform import RealViews, AffineTransform3D
from net.imglib2.interpolation.randomaccess import NLinearInterpolatorFactory
from net.imglib2.util import Intervals

img = IL.wrap(IJ.getImage())

# Cut out a cube
img1 = Views.zeroMin(
    Views.interval(img, [39, 49, 0], [39 + 378 - 1, 49 + 378 - 1, 378 - 1]))
print[img1.dimension(d) for d in xrange(img1.numDimensions())]

# Rotate the cube on the Y axis to the left
img2 = Views.rotate(img1, 2, 0)

# copy into ArrayImg
img1a = ArrayImgs.unsignedShorts([378, 378, 378])
ImgMath.compute(ImgSource(img1)).into(img1a)

img2a = ArrayImgs.unsignedShorts([378, 378, 378])
ImgMath.compute(ImgSource(img2)).into(img2a)

img1 = img1a
img2 = img2a
Exemplo n.º 14
0
from net.imglib2.img.display.imagej import ImageJFunctions as IL
from ij import IJ

# Open Nile Bend sample image
imp = IJ.getImage()
#imp = IJ.openImage("https://imagej.nih.gov/ij/images/NileBend.jpg")
img = IL.wrapRGBA(imp)

# Extract red channel: alpha:0, red:1, green:2, blue:3
red = Converters.argbChannel(img, 1)

# Cut out two overlapping ROIs
r1 = Rectangle(1708, 680, 1792, 1760)
r2 = Rectangle(520, 248, 1660, 1652)
cut1 = Views.zeroMin(
    Views.interval(red, [r1.x, r1.y],
                   [r1.x + r1.width - 1, r1.y + r1.height - 1]))
cut2 = Views.zeroMin(
    Views.interval(red, [r2.x, r2.y],
                   [r2.x + r2.width - 1, r2.y + r2.height - 1]))

# Thread pool
exe = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors())

try:
    # PCM: phase correlation matrix
    pcm = PhaseCorrelation2.calculatePCM(cut1, cut2,
                                         ArrayImgFactory(FloatType()),
                                         FloatType(),
                                         ArrayImgFactory(ComplexFloatType()),
                                         ComplexFloatType(), exe)
# Test

# Prepare test data
def dropSlices(img, nth):
  """ Drop every nth slice. Calibration is to be multipled by nth for Z.
      Counts slices 1-based so as to preserve the first slice (index zero).
  """
  return Views.stack([Views.hyperSlice(img, 2, i)
                      for i in xrange(img.dimension(2)) if 0 == (i+1) % nth])

# Grap the current image
img = IL.wrap(IJ.getImage())

# Cut out a cube
img1 = Views.zeroMin(Views.interval(img, [39, 49, 0],
                                         [39 + 378 -1, 49 + 378 -1, 378 -1]))

# Rotate the cube on the Y axis to the left
img2 = Views.zeroMin(Views.rotate(img1, 2, 0)) # zeroMin is CRITICAL

# Rotate the cube on the X axis to the top
img3 = Views.zeroMin(Views.rotate(img1, 2, 1))

# Reduce Z resolution: make them anisotropic but in a different direction
nth = 2
img1 = dropSlices(img1, nth)
img2 = dropSlices(img2, nth)
img3 = dropSlices(img3, nth)

# The sequence of images to transform, each relative to the previous
images = [img1, img2, img3]
imp.setRoi(roi)

# Now, iterate each peak, defining a small interval centered at each peak,
# and measure the sum of total pixel intensity,
# and display the results in an ImageJ ResultTable.
table = ResultsTable()

for peak in peaks:
    # Read peak coordinates into an array of integers
    peak.localize(p)
    # Define limits of the interval around the peak:
    # (sigmaSmaller is half the radius of the embryo)
    minC = [int(p[i] - sigmaSmaller) for i in xrange(img.numDimensions())]
    maxC = [int(p[i] + sigmaSmaller) for i in xrange(img.numDimensions())]
    # View the interval around the peak, as a flat iterable (like an array)
    square = Views.flatIterable(Views.zeroMin(Views.interval(img, minC, maxC)))
    s1 = sum(t.getInteger() for t in square)
    area1 = Intervals.numElements(square)
    # Use a sphere instead
    radius = sqrt(area1 / pi)  # same area for both
    print sigmaSmaller, radius
    circle = Masks.toIterableRegion(GeomMasks.closedSphere(p, radius))
    s2 = sum(t.getInteger() for t in Regions.sample(circle, imgE))
    area2 = Intervals.numElements(circle)

    print area1, area2

    # Compute sum of pixel intensity values of the interval
    # (The t is the Type that mediates access to the pixels, via its get* methods)

    # Add to results table
Exemplo n.º 17
0
def cropView(img):
  return Views.zeroMin(Views.interval(img, [1, 228, 0], [1 + 406 -1, 228 + 465 -1, 325 -1]))
def getViewFromImglib2Im(img, r):
    v = Views.zeroMin(
        Views.interval(img, [r.x, r.y],
                       [r.x + r.width - 1, r.y + r.height - 1]))
    return v
# 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')
Exemplo n.º 20
0
def cropView(img, minCoords, maxCoords):
    return Views.zeroMin(Views.interval(img, minCoords, maxCoords))