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] IL.wrap(Views.stack(images), "unregistered").show()
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 IL.wrap(img1, "cube").show() IL.wrap(img2, "cube rotated").show() # Now register them
fi = FileInfo() fi.offset = 1024 # header size in bytes fi.width = 128 fi.height = 128 fi.nImages = 128 baseDir = "/home/albert/lab/scripts/data/cim.mcgill.ca-shape-benchmark/" bird = IL.wrap(Raw.open(baseDir + "/birdsIm/b21.im", fi)) airplane = IL.wrap(Raw.open(baseDir + "/airplanesIm/b14.im", fi)) # Rotate bird # Starts with posterior view # Rotate 180 degrees around Y axis # Set to dorsal up: 180 degrees birdY90 = Views.rotate(bird, 2, 0) # 90 birdY180 = Views.rotate(birdY90, 2, 0) # 90 again: 180 c1 = Views.iterable(birdY180).cursor() img1 = ArrayImgs.unsignedBytes(Intervals.dimensionsAsLongArray(birdY180)) c2 = img1.cursor() while c2.hasNext(): c2.next().set(c1.next()) # Rotate airplane # Starts with dorsal view, anterior down # Set to: coronal view, but dorsal is down airplaneC = Views.rotate(airplane, 2, 1) # Set to dorsal up: 180 degrees airplaneC90 = Views.rotate(airplaneC, 0, 1) # 90 airplaneC180 = Views.rotate(airplaneC90, 0, 1) # 90 again: 180