Last active
August 19, 2021 23:12
-
-
Save bogovicj/fbbf75fa1dd09a9a6a4ee6f2792ca200 to your computer and use it in GitHub Desktop.
Pads/crops the source image such that its field of view matches that target image.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#@ Dataset source | |
#@ Dataset target | |
/** | |
* Pads/crops the source image such that its field of view matches that target image, | |
* respecting the target image origin. | |
* | |
* Currently only supports pixel-length shifts, and ignores source origin. | |
* | |
* see: | |
* https://forum.image.sc/t/bigwarp-for-overlaying-medical-images/56591 | |
* | |
* John Bogovic | |
*/ | |
import net.imglib2.view.*; | |
import net.imglib2.util.*; | |
import net.imglib2.loops.*; | |
import net.imglib2.img.imageplus.*; | |
import java.util.function.BiConsumer; | |
// setup | |
nx = target.dimension(0); | |
ny = target.dimension(1); | |
minx = target.axis(0).origin() as long; | |
miny = target.axis(1).origin() as long; | |
nz = target.dimension(2); | |
is2d = (nz <= 1); | |
if( is2d ) { // 2d | |
targetInterval = Intervals.createMinSize(minx, miny, nx, ny ); | |
} | |
else { // 3d | |
minz = target.axis(2).origin() as long; | |
targetInterval = Intervals.createMinSize(minx, miny, minz, nx, ny, nz ); | |
} | |
// transform and pad the source image | |
resampledSource = Views.interval(Views.extendZero(source), targetInterval); | |
resampledSourceDataset = new ImagePlusImgFactory( Util.getTypeFromInterval(source)).create(targetInterval); | |
def setget = {x,y -> y.set(x.get())} as BiConsumer; | |
LoopBuilder.setImages(resampledSource,resampledSourceDataset).forEachPixel( setget ); | |
// copy metadata and show | |
imp = resampledSourceDataset.getImagePlus(); | |
imp.getCalibration().pixelWidth = target.axis(0).scale(); | |
imp.getCalibration().pixelHeight = target.axis(1).scale(); | |
imp.getCalibration().xOrigin = target.axis(0).origin(); | |
imp.getCalibration().yOrigin = target.axis(1).origin(); | |
if( !is2d ) { | |
imp.getCalibration().pixelDepth = target.axis(2).scale(); | |
imp.getCalibration().zOrigin = target.axis(2).origin(); | |
} | |
imp.show(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment