Skip to content

Commit

Permalink
add code to put fake interest points between split tiles
Browse files Browse the repository at this point in the history
  • Loading branch information
StephanPreibisch committed May 16, 2024
1 parent 9ecd70c commit 6f58312
Show file tree
Hide file tree
Showing 3 changed files with 143 additions and 13 deletions.
37 changes: 31 additions & 6 deletions src/main/java/net/preibisch/mvrecon/fiji/plugin/Split_Views.java
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,11 @@ public class Split_Views implements PlugIn

public static boolean defaultOptimize = true;

public static boolean defaultAddIPs = true;
public static int defaultDensity = 100;

public static boolean defaultAssignIlluminations = true;

public static String defaultPath = null;

public static int defaultChoice = 0;
Expand All @@ -85,10 +90,12 @@ public static boolean split(
final long[] targetSize,
final long[] overlap,
final long[] minStepSize,
final boolean assingIlluminationsFromTileIds,
final boolean optimize,
final int pointDensity,
final boolean display )
{
final SpimData2 newSD = SplittingTools.splitImages( data, overlap, targetSize, minStepSize, optimize );
final SpimData2 newSD = SplittingTools.splitImages( data, overlap, targetSize, minStepSize, assingIlluminationsFromTileIds, optimize, pointDensity );

if ( display )
{
Expand Down Expand Up @@ -140,8 +147,13 @@ public static boolean split( final SpimData2 data, final String fileName )
gd.addMessage( "Note: overlap will be adjusted to be divisible by " + Arrays.toString( minStepSize ), GUIHelper.mediumstatusfont, Color.RED );
gd.addMessage( "Minimal image sizes per dimension: " + Util.printCoordinates( imgSizes.getB() ), GUIHelper.mediumstatusfont, Color.DARK_GRAY );

gd.addCheckbox( "Add_fake_interest_points", defaultAddIPs );
gd.addNumericField( "Density (# per 100x100x100 px)", defaultDensity, 0 );
gd.addMessage( "" );

if ( data.getSequenceDescription().getAllIlluminationsOrdered().size() == 1 )
gd.addCheckbox( "Assign_old_tiles_as_illuminations (great for visualization)", defaultAssignIlluminations );

IOFunctions.println( fileName );
if ( defaultPath == null || defaultPath.trim().length() == 0 )
{
Expand All @@ -152,7 +164,7 @@ public static boolean split( final SpimData2 data, final String fileName )
defaultPath = fileName + ".split.xml";
}

gd.addFileField("New_XML_File", defaultPath);
gd.addFileField("New_XML_File", defaultPath, 30);
gd.addChoice( "Split_Result", resultChoice, resultChoice[ defaultChoice ] );

gd.showDialog();
Expand All @@ -170,6 +182,19 @@ public static boolean split( final SpimData2 data, final String fileName )
final long oy = defaultOverlapY = closestLargerLongDivisableBy( Math.round( gd.getNextNumber() ), minStepSize[ 1 ] );
final long oz = defaultOverlapZ = closestLargerLongDivisableBy( Math.round( gd.getNextNumber() ), minStepSize[ 2 ] );

final boolean addIPs = defaultAddIPs = gd.getNextBoolean();
final int density = defaultDensity = addIPs ? (int)Math.round( gd.getNextNumber() ) : 0;
final boolean assignIllum;
if ( data.getSequenceDescription().getAllIlluminationsOrdered().size() == 1 )
assignIllum = defaultAssignIlluminations = gd.getNextBoolean();
else
assignIllum = false;

gd.addMessage( "" );

if ( data.getSequenceDescription().getAllIlluminationsOrdered().size() == 1 )
gd.addCheckbox( "Assign_old_tiles_as_illuminations (great for visualization)", defaultAssignIlluminations );

final String saveAs = defaultPath = gd.getNextString();
final int choice = defaultChoice = gd.getNextChoiceIndex();

Expand All @@ -182,7 +207,7 @@ public static boolean split( final SpimData2 data, final String fileName )
return false;
}

return split( data, saveAs, new long[]{ sx, sy, sz }, new long[]{ ox, oy, oz }, minStepSize, optimize, choice == 0 );
return split( data, saveAs, new long[]{ sx, sy, sz }, new long[]{ ox, oy, oz }, minStepSize, assignIllum, optimize, density, choice == 0 );
}

public static Pair< HashMap< String, Integer >, long[] > collectImageSizes( final AbstractSpimData< ? > data )
Expand Down Expand Up @@ -285,9 +310,9 @@ public static long[] findMinStepSize( final AbstractSpimData< ? > data )

for ( int d = 0; d < minStepSize.length; ++d )
{
if ( lowestResolution[ d ] % 1 != 0.0 )
if ( Math.abs( lowestResolution[ d ] % 1 ) > 0.001 )
if ( !roundMipmapResolutions )
throw new RuntimeException( "Downsampling has a fraction, cannot split dataset" );
throw new RuntimeException( "Downsampling has a fraction > 0.001, cannot split dataset since it does not seem to be a rounding error." );

minStepSize[ d ] = lowestCommonMultiplier( minStepSize[ d ], Math.round( lowestResolution[ d ] ) );
}
Expand All @@ -307,7 +332,7 @@ public static void main( String[] args ) throws SpimDataException
{
new ImageJ();

GenericLoadParseQueryXML.defaultXMLfilename = "/Users/preibischs/SparkTest/IP/dataset.xml";
GenericLoadParseQueryXML.defaultXMLfilename = "/Users/preibischs/SparkTest/Stitching/dataset.xml";

new Split_Views().run( null );
//SpimData2 data = new XmlIoSpimData2("").load( GenericLoadParseQueryXML.defaultXMLfilename );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,9 @@ public static void main( String[] args ) throws SpimDataException
Split_Views.closestLongDivisableBy( 200, minStepSize[ 1 ] ),
Split_Views.closestLongDivisableBy( 40, minStepSize[ 2 ] ) },
minStepSize,
true );
true,
true,
100 );
// drosophila with 1000 views

final ViewSetupExplorer< SpimData2 > explorer = new ViewSetupExplorer<>( newSD, fileOut, new XmlIoSpimData2( "" ) );
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
package net.preibisch.mvrecon.process.splitting;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
Expand Down Expand Up @@ -54,6 +55,7 @@
import net.imglib2.Interval;
import net.imglib2.iterator.LocalizingZeroMinIntervalIterator;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.util.Intervals;
import net.imglib2.util.Pair;
import net.imglib2.util.Util;
import net.imglib2.util.ValuePair;
Expand All @@ -74,10 +76,29 @@

public class SplittingTools
{
public static boolean assingIlluminationsFromTileIds = false;
//public static boolean assingIlluminationsFromTileIds = false;
public static double error = 0.5;
public static int minPoints = 20;

public static SpimData2 splitImages( final SpimData2 spimData, final long[] overlapPx, final long[] targetSize, final long[] minStepSize, final boolean optimize )
{
/**
*
* @param spimData - the spimdata object to split up
* @param overlapPx - the expected overlap
* @param targetSize - roughly the expected size of each subdivided tile
* @param minStepSize - step size or multipleOf for coordinates and sizes (except size of last tile), e.g. 8 - defined by the lowest downsampling step
* @param assingIlluminationsFromTileIds - use illumination attribute to remember former tiles
* @param optimize - whether to optimize overlap size
* @param pointDensity - how many points per 100x100x100 volume
* @return
*/
public static SpimData2 splitImages(
final SpimData2 spimData,
final long[] overlapPx,
final long[] targetSize,
final long[] minStepSize,
final boolean assingIlluminationsFromTileIds,
final boolean optimize,
final int pointDensity) {
final TimePoints timepoints = spimData.getSequenceDescription().getTimePoints();

final List< ViewSetup > oldSetups = new ArrayList<>();
Expand Down Expand Up @@ -108,6 +129,8 @@ public static SpimData2 splitImages( final SpimData2 spimData, final long[] over
if ( spimData.getSequenceDescription().getAllIlluminationsOrdered().size() > 1 )
throw new IllegalArgumentException( "Cannot SplittingTools.assingIlluminationsFromTileIds because more than one Illumination exists." );

final Random rnd = new Random( 23424459 );

for ( final ViewSetup oldSetup : oldSetups )
{
final int oldID = oldSetup.getId();
Expand All @@ -125,6 +148,8 @@ public static SpimData2 splitImages( final SpimData2 spimData, final long[] over

final ArrayList< Interval > intervals = distributeIntervalsFixedOverlap( input, overlapPx, targetSize, minStepSize, optimize );

final HashMap< Interval, ViewSetup > interval2ViewSetup = new HashMap<>();

for ( int i = 0; i < intervals.size(); ++i )
{
final Interval interval = intervals.get( i );
Expand All @@ -150,6 +175,8 @@ public static SpimData2 splitImages( final SpimData2 spimData, final long[] over
final ViewSetup newSetup = new ViewSetup( newId, null, newDim, voxDim, newTile, channel, angle, newIllum );
newSetups.add( newSetup );

interval2ViewSetup.put( interval, newSetup );

// update registrations and interest points for all timepoints
for ( final TimePoint t : timepoints.getTimePointsOrdered() )
{
Expand Down Expand Up @@ -179,11 +206,12 @@ public static SpimData2 splitImages( final SpimData2 spimData, final long[] over
{
for ( final String label : oldVipl.getHashMap().keySet() )
{
int id = 0;

final ArrayList< InterestPoint > newIp = new ArrayList<>();
final InterestPoints oldIpl = oldVipl.getInterestPointList( label );
final List< InterestPoint > oldIp = oldIpl.getInterestPointsCopy();
final ArrayList< InterestPoint > newIp = new ArrayList<>();

int id = 0;

for ( final InterestPoint ip : oldIp )
{
if ( contains( ip.getL(), interval ) )
Expand All @@ -196,6 +224,81 @@ public static SpimData2 splitImages( final SpimData2 spimData, final long[] over
}
}

// adding random corresponding interest points
if ( Double.isFinite( pointDensity ) && pointDensity > 0 )
{
// for each overlapping tile that has not been processed yet
for ( int j = 0; j < i; ++j )
{
final Interval otherInterval = intervals.get( j );
final Interval intersection = Intervals.intersect( interval, otherInterval );

// find the overlap
if ( !Intervals.isEmpty( intersection ) )
{
final ViewSetup otherSetup = interval2ViewSetup.get( otherInterval );
final ViewId otherViewId = new ViewId( t.getId(), otherSetup.getId() );
final ViewInterestPointLists otherIPs = newInterestpoints.get( otherViewId );

// TODO: maybe find the area for both that do not contain interest points yet

// add points as function of the area
final int n = intersection.numDimensions();
long numPixels = 1;
for ( int d = 0; d < n; ++d )
numPixels *= intersection.dimension( d );

final int numPoints = Math.max( minPoints, (int)Math.round( Math.ceil( pointDensity * numPixels / (100.0*100.0*100.0) ) ) );
System.out.println(numPixels / (100.0*100.0*100.0) + " " + numPoints );

final List< InterestPoint > otherPoints;
int otherId;
if ( otherIPs.getInterestPointList( label + "_split" ) == null )
{
otherPoints = new ArrayList<>( numPoints );
otherId = 0;
}
else
{
otherPoints = otherIPs.getInterestPointList( label + "_split" ).getInterestPointsCopy();
otherId = otherPoints.size() > 0 ? otherPoints.get( otherPoints.size() - 1 ).getId() + 1 : 0;
}

for ( int k = 0; k < numPoints; ++k )
{
final double[] p = new double[ n ];
final double[] op = new double[ n ];

for ( int d = 0; d < n; ++d )
{
final double l = rnd.nextDouble() * intersection.dimension( d ) + intersection.min( d );
p[ d ] = (l + (rnd.nextDouble()-0.5)*error ) - interval.min( d );
op[ d ] = (l - (rnd.nextDouble()-0.5)*error ) - otherInterval.min( d );
}

newIp.add( new InterestPoint( id++, p ) );
otherPoints.add( new InterestPoint( otherId++, op ) );
}

// store the interest points for the overlapping interval
if ( otherIPs.getInterestPointList( label + "_split" ) == null )
{
// this should only happens for the first pair
final InterestPoints newOtherIpl = InterestPoints.newInstance( oldIpl.getBaseDir(), otherViewId, label + "_split" );
newOtherIpl.setInterestPoints( newIp );
newOtherIpl.setParameters( oldIpl.getParameters() );
newOtherIpl.setCorrespondingInterestPoints( new ArrayList<>() );
otherIPs.addInterestPointList( label + "_split", newOtherIpl ); // still add

}
else
{
otherIPs.getInterestPointList( label + "_split" ).setInterestPoints( otherPoints );
}
}
}
}

final InterestPoints newIpl = InterestPoints.newInstance( oldIpl.getBaseDir(), newViewId, label + "_split" );
newIpl.setInterestPoints( newIp );
newIpl.setParameters( oldIpl.getParameters() );
Expand Down

0 comments on commit 6f58312

Please sign in to comment.