-
Notifications
You must be signed in to change notification settings - Fork 0
/
ResaveImaris.bsh
134 lines (109 loc) · 3.94 KB
/
ResaveImaris.bsh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
// written by Stephan Preibisch & Friedrich Preusser
// Berlin, 11 Apr 2019
// We ran into the problem that Fiji/BioFormats is not able to open our .ims files acquired with the Andor Fusion software version 2.0 (i.e. our Dragonfly spinning disk).
// It looks like the problem occurs only if files are bigger than ca. 3GB.
// This script enables re-saving of individual timepoints from the .ims as tiffs, which can then be handled in Fiji again.
// You run it as follows:
// 1) Go to GitHub > click on Raw > select & copy everything (or check the project out)
// 2) Run Fiji > New > Script > Beanshell > Paste > Run
// 3) Select .ims file > Select output directory & time point range > Start
import java.io.File;
import java.io.IOException;
import java.util.List;
import bdv.img.imaris.Imaris;
import bdv.spimdata.SpimDataMinimal;
import bdv.spimdata.XmlIoSpimDataMinimal;
import fiji.util.gui.GenericDialogPlus;
import ij.IJ;
import ij.ImageJ;
import ij.ImagePlus;
import ij.io.FileSaver;
import mpicbg.spim.data.SpimDataException;
import mpicbg.spim.data.generic.sequence.BasicImgLoader;
import mpicbg.spim.data.generic.sequence.BasicViewSetup;
import mpicbg.spim.data.sequence.TimePoint;
import mpicbg.spim.data.sequence.ViewId;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.img.display.imagej.ImageJFunctions;
GenericDialogPlus gd = new GenericDialogPlus( "Save options" );
gd.addFileField( "Select Imaris file", "", 100 );
gd.showDialog();
if ( gd.wasCanceled() )
return;
File file = new File( gd.getNextString() );
if ( !file.exists() )
{
IJ.log( "File " + file + " does not exist." );
return;
}
else
{
IJ.log( "Opening " + file );
}
SpimDataMinimal spimData = null;
try
{
spimData = Imaris.openIms( file.getAbsolutePath() );
}
catch ( Exception e ) { e.printStackTrace(); }
if ( spimData == null )
{
IJ.log( "File " + file + " could not be opened." );
return;
}
// display
//BigDataViewer.open( spimData, "test", new ProgressWriterIJ(), ViewerOptions.options() );
List viewSetups = spimData.getSequenceDescription().getViewSetupsOrdered();
List timepoints = spimData.getSequenceDescription().getTimePoints().getTimePointsOrdered();
IJ.log( "Found " + viewSetups.size() + " viewsetups." );
IJ.log( "Found " + timepoints.size() + " timepoints." );
gd = new GenericDialogPlus( "Save options" );
gd.addDirectoryField( "Directory", file.getParent(), 50 );
if ( timepoints.size() > 1 )
{
gd.addSlider( "Start timepoint", timepoints.get( 0 ).getId(), timepoints.get( timepoints.size() - 1 ).getId(), 0 );
gd.addSlider( "End timepoint", timepoints.get( 0 ).getId(), timepoints.get( timepoints.size() - 1 ).getId(), timepoints.get( timepoints.size() - 1 ).getId() );
}
gd.showDialog();
if ( gd.wasCanceled() )
return;
String saveDir = defaultDir = gd.getNextString();
int startTp, endTp;
if ( timepoints.size() > 1 )
{
startTp = (int)Math.round( gd.getNextNumber() );
endTp = (int)Math.round( gd.getNextNumber() );
}
else
{
startTp = endTp = timepoints.get( 0 ).getId();
}
BasicImgLoader imgLoader = spimData.getSequenceDescription().getImgLoader();
for ( TimePoint tp : timepoints )
{
if ( tp.getId() >= startTp && tp.getId() <= endTp )
{
IJ.log( "Exporting timepoint " + tp.getId() );
for ( BasicViewSetup vs : viewSetups )
{
IJ.log( "Exporting viewsetup " + vs.getId() );
ViewId v = new ViewId( tp.getId(), vs.getId() );
if ( spimData.getSequenceDescription().getViewDescriptions().get( v ).isPresent() )
{
RandomAccessibleInterval img = imgLoader.getSetupImgLoader( vs.getId() ).getImage( tp.getId(), 0, null );
ImagePlus imp = ImageJFunctions.wrap( img, "tp_" + tp.getId() + "_vs_" + vs.getId() );
String saveFile = new File( saveDir, imp.getTitle() ).getAbsolutePath() + ".tif";
new FileSaver( imp ).saveAsTiffStack( saveFile );
IJ.log( "Saved file " + saveFile );
}
else
{
IJ.log( "Exporting viewsetup " + vs.getName() + " IS NOT PRESENT." );
}
}
}
else
{
IJ.log( "Skipping timepoint " + tp.getId() );
}
}