Last active
April 5, 2019 14:11
-
-
Save bogovicj/3b9672e11430e8ed7f8f176673412f02 to your computer and use it in GitHub Desktop.
An ImageJ2 script for combining binary images with specified physical bounding boxes
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
// @UIService ui | |
// @LogService log | |
// @Dataset base | |
// @String baseMinArg | |
// @String baseMaxArg | |
// @Dataset add | |
// @String addMinArg | |
// @String addMaxArg | |
// @Integer value | |
// see this forum thread: | |
// https://forum.image.sc/t/how-to-merge-3d-raw-files-into-imagej-without-loosing-their-original-locations/24047/12 | |
import java.util.ArrayList; | |
import net.imagej.axis.Axes | |
import net.imglib2.interpolation.randomaccess.*; | |
import net.imglib2.realtransform.*; | |
import net.imglib2.view.*; | |
import net.imglib2.util.*; | |
def toDoubleArray( s ){ | |
l = [] | |
for( x in s.trim().split("\\s+")){ l.add( Double.parseDouble( x )); } | |
return l as double[] | |
} | |
def getResolution( min, max, size ){ | |
res = [] | |
for( i in (0..2) ){ | |
res.add( (max[i] - min[i]) / size[i]) | |
} | |
return res as double[] | |
} | |
def toPhysical( c, min, res ) | |
{ | |
pt = [] | |
for( i in (0..2) ){ | |
pt.add( (c.getDoublePosition( i ) * res[i]) + min[i] ) | |
} | |
return pt as double[] | |
} | |
baseMin = toDoubleArray( baseMinArg ) | |
baseMax = toDoubleArray( baseMaxArg ) | |
addMin = toDoubleArray( addMinArg ) | |
addMax = toDoubleArray( addMaxArg ) | |
baseSz = Intervals.dimensionsAsLongArray( base ) | |
addSz = Intervals.dimensionsAsLongArray( add ) | |
baseRes = getResolution( baseMin, baseMax, baseSz ) | |
addRes = getResolution( addMin, addMax, addSz ) | |
// build transform | |
totalTransform = new AffineTransform3D() | |
add2Phys = new ScaleAndTranslation( addRes, addMin ) | |
base2Phys = new ScaleAndTranslation( baseRes, baseMin ) | |
totalTransform.concatenate( add2Phys ) // pixel to phys | |
totalTransform.preConcatenate( base2Phys.inverse()) // back to pixel space of base img | |
addXfm = RealViews.transformReal( | |
Views.interpolate( Views.extendZero( add ), new NearestNeighborInterpolatorFactory()), | |
totalTransform ).realRandomAccess(); | |
result = base.duplicate() | |
c = Views.flatIterable( result ).cursor() | |
nvoxSet = 0 | |
while( c.hasNext() ) | |
{ | |
c.fwd(); | |
addXfm.setPosition( c ); | |
if( addXfm.get().getRealDouble() > 128 ) | |
{ | |
c.get().set( value ) | |
nvoxSet++ | |
} | |
} | |
println( "modified " + nvoxSet + " voxels") | |
ui.show( result ) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment