|
| 1 | +#@ File (label="Landmark file") landmarksPath |
| 2 | +#@ ImagePlus (label="Moving image") movingIp |
| 3 | +#@ ImagePlus (label="Target image") targetIp |
| 4 | +#@ String (label="Transform type", choices={"Thin Plate Spline", "Affine", "Similarity", "Rotation", "Translation" }) transformType |
| 5 | +#@ String (label="Interpolation", choices={"Linear", "Nearest Neighbor"}) interpType |
| 6 | +#@ Integer (label="Number of threads", min=1, max=64, value=1) nThreads |
| 7 | +#@ UIService ui |
| 8 | + |
| 9 | + |
| 10 | +int nd = 2; |
| 11 | +ltm = new LandmarkTableModel( nd ); |
| 12 | +try |
| 13 | +{ |
| 14 | + ltm.load( landmarksPath ); |
| 15 | +} catch ( IOException e ) { |
| 16 | + ui.showMessage("Could not load landmarks from:\n" + landmarksPath) |
| 17 | + e.printStackTrace(); |
| 18 | + return; |
| 19 | +} |
| 20 | + |
| 21 | +// apply transformation to every slice / frame |
| 22 | +sz = movingIp.getImageStackSize(); |
| 23 | +sliceResults = new ImageProcessor[sz]; |
| 24 | +for( int i = 0; i < sz; i++) { |
| 25 | + sliceIp = new ImagePlus("slice"+i, movingIp.getStack().getProcessor(i+1)); |
| 26 | + sliceIp.setCalibration(movingIp.getCalibration()); |
| 27 | + sliceResults[i] = transform(sliceIp).getProcessor(); |
| 28 | +} |
| 29 | + |
| 30 | + |
| 31 | +// combine slices and show |
| 32 | +nx = sliceResults[0].getWidth(); |
| 33 | +ny = sliceResults[0].getHeight(); |
| 34 | +stack = new ImageStack(nx, ny); |
| 35 | +for( int i = 0; i < sz; i++) { |
| 36 | + stack.addSlice(sliceResults[i]); |
| 37 | +} |
| 38 | +result = new ImagePlus(movingIp.getTitle() + "-transformed", stack); |
| 39 | +result.setDimensions(movingIp.getNChannels(), movingIp.getNSlices(), movingIp.getNFrames()); |
| 40 | +result.show(); |
| 41 | + |
| 42 | + |
| 43 | +// Applies transform to one slice |
| 44 | +def transform( ImagePlus mvg ) { |
| 45 | + |
| 46 | + bwData = BigWarpInit.createBigWarpDataFromImages(mvg, targetIp); |
| 47 | + bwData.wrapMovingSources(); |
| 48 | + |
| 49 | + bboxEst = new BoundingBoxEstimation(BoundingBoxEstimation.Method.CORNERS); |
| 50 | + emptyWriteOpts = new ApplyBigwarpPlugin.WriteDestinationOptions( "", "", null, null ); |
| 51 | + |
| 52 | + Interpolation interp = Interpolation.NLINEAR; |
| 53 | + if (interpType.equals("Nearest Neighbor")) |
| 54 | + interp = Interpolation.NEARESTNEIGHBOR; |
| 55 | + |
| 56 | + invXfm = new BigWarpTransform( ltm, transformType ).getTransformation(); |
| 57 | + show = false; |
| 58 | + isVirtual = false; |
| 59 | + |
| 60 | + warpedIpList = ApplyBigwarpPlugin.apply( |
| 61 | + bwData, ltm, invXfm, transformType, "Target", "", bboxEst, |
| 62 | + "Target", null, null, null, |
| 63 | + interp, isVirtual, nThreads, true, emptyWriteOpts, show); |
| 64 | + |
| 65 | + return warpedIpList.get(0); |
| 66 | +} |
| 67 | + |
| 68 | +import java.io.File; |
| 69 | +import java.io.IOException; |
| 70 | +import java.util.ArrayList; |
| 71 | +import java.util.List; |
| 72 | +import java.util.concurrent.ExecutionException; |
| 73 | +import java.util.concurrent.ExecutorService; |
| 74 | +import java.util.concurrent.Executors; |
| 75 | +import java.util.concurrent.Future; |
| 76 | + |
| 77 | +import bdv.ij.ApplyBigwarpPlugin; |
| 78 | +import bdv.ij.ApplyBigwarpPlugin.WriteDestinationOptions; |
| 79 | +import bdv.viewer.Interpolation; |
| 80 | +import bigwarp.BigWarpData; |
| 81 | +import bigwarp.BigWarpInit; |
| 82 | +import bigwarp.landmarks.LandmarkTableModel; |
| 83 | +import bigwarp.transforms.BigWarpTransform; |
| 84 | +import ij.IJ; |
| 85 | +import ij.ImagePlus; |
| 86 | +import ij.ImageStack; |
| 87 | +import ij.process.ImageProcessor; |
| 88 | +import net.imglib2.realtransform.BoundingBoxEstimation; |
| 89 | +import net.imglib2.realtransform.InvertibleRealTransform; |
0 commit comments