-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathApplyBigwarpPlugin.java
More file actions
1235 lines (1084 loc) · 38.1 KB
/
Copy pathApplyBigwarpPlugin.java
File metadata and controls
1235 lines (1084 loc) · 38.1 KB
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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*-
* #%L
* BigWarp plugin for Fiji.
* %%
* Copyright (C) 2015 - 2021 Howard Hughes Medical Institute.
* %%
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as
* published by the Free Software Foundation, either version 2 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program. If not, see
* <http://www.gnu.org/licenses/gpl-2.0.html>.
* #L%
*/
package bdv.ij;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import org.janelia.saalfeldlab.n5.Compression;
import org.janelia.saalfeldlab.n5.GzipCompression;
import org.janelia.saalfeldlab.n5.Lz4Compression;
import org.janelia.saalfeldlab.n5.N5Writer;
import org.janelia.saalfeldlab.n5.RawCompression;
import org.janelia.saalfeldlab.n5.XzCompression;
import org.janelia.saalfeldlab.n5.blosc.BloscCompression;
import org.janelia.saalfeldlab.n5.ij.N5Exporter;
import org.janelia.saalfeldlab.n5.ij.N5Factory;
import org.janelia.saalfeldlab.n5.imglib2.N5Utils;
import org.janelia.saalfeldlab.n5.metadata.N5CosemMetadata;
import org.janelia.saalfeldlab.n5.metadata.N5CosemMetadataParser;
import org.scijava.command.Command;
import org.scijava.plugin.Plugin;
import bdv.export.ProgressWriter;
import bdv.gui.TransformTypeSelectDialog;
import bdv.ij.util.ProgressWriterIJ;
import bdv.img.WarpedSource;
import bdv.viewer.Interpolation;
import bdv.viewer.Source;
import bdv.viewer.SourceAndConverter;
import bigwarp.BigWarp.BigWarpData;
import bigwarp.BigWarp;
import bigwarp.BigWarpExporter;
import bigwarp.BigWarpInit;
import bigwarp.landmarks.LandmarkTableModel;
import fiji.util.gui.GenericDialogPlus;
import bigwarp.transforms.BigWarpTransform;
import ij.IJ;
import ij.ImageJ;
import ij.ImagePlus;
import ij.WindowManager;
import mpicbg.spim.data.sequence.VoxelDimensions;
import net.imglib2.FinalInterval;
import net.imglib2.Interval;
import net.imglib2.RandomAccessibleInterval;
import net.imglib2.RealRandomAccessible;
import net.imglib2.realtransform.AffineGet;
import net.imglib2.realtransform.AffineRandomAccessible;
import net.imglib2.realtransform.AffineTransform;
import net.imglib2.realtransform.AffineTransform3D;
import net.imglib2.realtransform.InvertibleRealTransform;
import net.imglib2.realtransform.RealTransformSequence;
import net.imglib2.realtransform.RealViews;
import net.imglib2.type.NativeType;
import net.imglib2.type.numeric.NumericType;
import net.imglib2.util.Intervals;
import net.imglib2.view.IntervalView;
import net.imglib2.view.Views;
/**
*
* Apply a bigwarp transform to a 2d or 3d ImagePlus
*
*/
@Plugin(type= Command.class,
menuPath = "Plugins>BigDataViewer>Big Warp Apply"
)
public class ApplyBigwarpPlugin implements Command
{
public static final String TARGET = "Target";
public static final String MOVING = "Moving";
public static final String MOVING_WARPED = "Moving (warped)";
public static final String UNION_TARGET_MOVING = "Union of Target and warped moving image";
public static final String SPECIFIED = "Specified";
public static final String SPECIFIED_PHYSICAL = "Specified (physical units)";
public static final String SPECIFIED_PIXEL = "Specified (pixel units)";
public static final String LANDMARK_POINTS = "Landmark points";
public static final String LANDMARK_POINT_CUBE_PHYSICAL = "Landmark point cube (physical units)";
public static final String LANDMARK_POINT_CUBE_PIXEL = "Landmark point cube (pixel units)";
public static void main( String[] args ) throws IOException
{
new ImageJ();
new ApplyBigwarpPlugin().run();
}
public static boolean validateInput(
final ImagePlus movingIp,
final ImagePlus targetIp,
final LandmarkTableModel landmarks,
final String fieldOfViewOption,
final String resolutionOption,
final double[] resolutionSpec,
final double[] fovSpec,
final double[] offsetSpec,
final Interpolation interp,
final boolean isVirtual,
final int nThreads )
{
// there's no target image
if( targetIp == movingIp )
{
if( fieldOfViewOption.equals( TARGET ) )
{
return false;
}
if( resolutionOption.equals( TARGET ) )
{
return false;
}
}
return true;
}
public static double[] getResolution(
final Source< ? > source,
final String resolutionOption,
final double[] resolutionSpec )
{
if( ( source == null ) )
{
System.err.println("Requested target resolution but target image is missing.");
return null;
}
final double[] res = resolutionFromSource( source );
/*
* Maybe the below will work in the future, but it does not right now
* ( April 2021) in part because theres no way to set the VoxelDimensions for a
* bdv.util.RandomAccessibleIntervalSource
*/
// VoxelDimensions voxdims = source.getVoxelDimensions();
// if( voxdims == null )
// Arrays.fill( res, 1.0 );
// else
// voxdims.dimensions( res );
return res;
}
public static String getUnit( final BigWarpData<?> bwData,
final String resolutionOption )
{
String unit = "pix";
boolean doTargetImagesExist = bwData.targetSourceIndices != null && bwData.targetSourceIndices.length > 0;
if( resolutionOption.equals( MOVING ) || resolutionOption.equals( MOVING_WARPED ) || !doTargetImagesExist )
{
VoxelDimensions mvgVoxDims = bwData.sources.get( bwData.movingSourceIndices[0] ).getSpimSource().getVoxelDimensions();
if( mvgVoxDims != null )
unit = mvgVoxDims.unit();
}
else
{
// use target units even if
VoxelDimensions tgtVoxDims = bwData.sources.get( bwData.targetSourceIndices[0] ).getSpimSource().getVoxelDimensions();
if( tgtVoxDims != null )
unit = tgtVoxDims.unit();
}
return unit;
}
public static double[] getResolution(
final BigWarpData<?> bwData,
final String resolutionOption,
final double[] resolutionSpec )
{
// TODO it may be necessary to generalize this to an arbitrary
// moving or target index rather than grabbing the first
if( resolutionOption.equals( TARGET ))
{
if( bwData.targetSourceIndices.length <= 0 )
return null;
Source< ? > spimSource = bwData.sources.get(
bwData.targetSourceIndices[ 0 ]).getSpimSource();
return getResolution( spimSource, resolutionOption, resolutionSpec );
}
else if( resolutionOption.equals( MOVING ))
{
if( bwData.targetSourceIndices.length <= 0 )
return null;
Source< ? > spimSource = bwData.sources.get(
bwData.movingSourceIndices[ 0 ]).getSpimSource();
return getResolution( spimSource, resolutionOption, resolutionSpec );
}
else if( resolutionOption.equals( SPECIFIED ))
{
if( ( resolutionSpec == null ) )
{
System.err.println("Requested moving resolution but moving image is missing.");
return null;
}
double[] res = new double[ 3 ];
System.arraycopy( resolutionSpec, 0, res, 0, resolutionSpec.length );
return res;
}
return null;
}
/**
* Returns the resolution of the output given input options
*
* @param movingIp the moving ImagePlus
* @param targetIp the target ImagePlus
* @param resolutionOption the resolution option
* @param resolutionSpec the resolution (if applicable)
* @return the output image resolution
*/
public static double[] getResolution(
final ImagePlus movingIp,
final ImagePlus targetIp,
final String resolutionOption,
final double[] resolutionSpec )
{
if( resolutionOption.equals( TARGET ))
{
if( ( targetIp == null ) )
{
System.err.println("Requested target resolution but target image is missing.");
return null;
}
double[] res = new double[ 3 ];
res[ 0 ] = targetIp.getCalibration().pixelWidth;
res[ 1 ] = targetIp.getCalibration().pixelHeight;
res[ 2 ] = targetIp.getCalibration().pixelDepth;
return res;
}
else if( resolutionOption.equals( MOVING ))
{
if( ( movingIp == null ) )
{
System.err.println("Requested moving resolution but moving image is missing.");
return null;
}
double[] res = new double[ 3 ];
res[ 0 ] = movingIp.getCalibration().pixelWidth;
res[ 1 ] = movingIp.getCalibration().pixelHeight;
res[ 2 ] = movingIp.getCalibration().pixelDepth;
return res;
}
else if( resolutionOption.equals( SPECIFIED ))
{
if( ( resolutionSpec == null ) )
{
System.err.println("Requested moving resolution but moving image is missing.");
return null;
}
double[] res = new double[ 3 ];
System.arraycopy( resolutionSpec, 0, res, 0, resolutionSpec.length );
return res;
}
System.err.println("Invalid resolution option: " + resolutionOption );
return null;
}
public static List<Interval> getPixelInterval(
final ImagePlus movingIp,
final ImagePlus targetIp,
final LandmarkTableModel landmarks,
final InvertibleRealTransform transform,
final String fieldOfViewOption,
final String fieldOfViewPointFilter,
final double[] fovSpec,
final double[] offsetSpec,
final double[] outputResolution )
{
BigWarpData<?> bwData = BigWarpInit.createBigWarpDataFromImages( movingIp, targetIp );
return getPixelInterval( bwData, landmarks, transform, fieldOfViewOption,
fieldOfViewPointFilter, fovSpec, offsetSpec, outputResolution );
}
public static Interval getPixelInterval(
final Source< ? > source,
final LandmarkTableModel landmarks,
final InvertibleRealTransform transform,
final String fieldOfViewOption,
final double[] outputResolution )
{
RandomAccessibleInterval< ? > rai = source.getSource( 0, 0 );
if( fieldOfViewOption.equals( TARGET ))
{
double[] inputres = resolutionFromSource( source );
long[] max = new long[ rai.numDimensions() ];
for( int d = 0; d < rai.numDimensions(); d++ )
{
max[ d ] = (long)Math.ceil( ( inputres[ d ] * rai.dimension( d )) / outputResolution[ d ]);
}
return new FinalInterval( max );
}
else if( fieldOfViewOption.equals( MOVING_WARPED ))
{
double[] movingRes = resolutionFromSource( source );
int ndims = transform.numSourceDimensions();
AffineTransform movingPixelToPhysical = new AffineTransform( ndims );
movingPixelToPhysical.set( movingRes[ 0 ], 0, 0 );
movingPixelToPhysical.set( movingRes[ 1 ], 1, 1 );
if( ndims > 2 )
movingPixelToPhysical.set( movingRes[ 2 ], 2, 2 );
AffineTransform outputResolution2Pixel = new AffineTransform( ndims );
outputResolution2Pixel.set( outputResolution[ 0 ], 0, 0 );
outputResolution2Pixel.set( outputResolution[ 1 ], 1, 1 );
if( ndims > 2 )
outputResolution2Pixel.set( outputResolution[ 2 ], 2, 2 );
RealTransformSequence seq = new RealTransformSequence();
seq.add( movingPixelToPhysical );
seq.add( transform.inverse() );
seq.add( outputResolution2Pixel.inverse() );
FinalInterval interval = new FinalInterval(
Intervals.minAsLongArray( rai ),
Intervals.maxAsLongArray( rai ));
return BigWarpExporter.estimateBounds( seq, interval );
}
else if( fieldOfViewOption.equals( UNION_TARGET_MOVING ))
{
final Interval movingWarpedInterval = getPixelInterval( source, landmarks, transform, MOVING_WARPED, outputResolution );
final Interval targetInterval = getPixelInterval( source, landmarks, transform, TARGET, outputResolution );
return Intervals.union( movingWarpedInterval, targetInterval );
}
return null;
}
public static double[] resolutionFromSource( Source< ? > src )
{
double[] res = new double[ 3 ];
final AffineTransform3D xfm = new AffineTransform3D();
src.getSourceTransform( 0, 0, xfm );
res[ 0 ] = xfm.get( 0, 0 );
res[ 1 ] = xfm.get( 1, 1 );
res[ 2 ] = xfm.get( 2, 2 );
return res;
}
/**
* Returns the interval in pixels of the output given input options
*
* @param bwData the BigWarpData
* @param landmarks the landmarks
* @param fieldOfViewOption the field of view option
* @param fieldOfViewPointFilter the regexp for filtering landmarks points by name
* @param fovSpec the field of view specification
* @param offsetSpec the offset specification
* @param outputResolution the resolution of the output image
* @return the output interval
*/
public static List<Interval> getPixelInterval(
final BigWarpData<?> bwData,
final LandmarkTableModel landmarks,
final InvertibleRealTransform transform,
final String fieldOfViewOption,
final String fieldOfViewPointFilter,
final double[] fovSpec,
final double[] offsetSpec,
final double[] outputResolution )
{
if( fieldOfViewOption.equals( TARGET ))
{
if ( bwData.targetSourceIndices.length <= 0 )
{
System.err.println("Requested target fov but target image is missing.");
return null;
}
return Stream.of(
getPixelInterval(
bwData.sources.get( bwData.targetSourceIndices[ 0 ]).getSpimSource(),
landmarks, transform, fieldOfViewOption, outputResolution )
).collect(Collectors.toList());
}
else if( fieldOfViewOption.equals( MOVING_WARPED ))
{
return Stream.of(
getPixelInterval(
bwData.sources.get( bwData.movingSourceIndices[ 0 ]).getSpimSource(),
landmarks, transform, fieldOfViewOption, outputResolution )
).collect(Collectors.toList());
}
else if( fieldOfViewOption.equals( UNION_TARGET_MOVING ))
{
return Stream.of(
getPixelInterval(
bwData.sources.get( bwData.movingSourceIndices[ 0 ]).getSpimSource(),
landmarks, transform, fieldOfViewOption, outputResolution )
).collect(Collectors.toList());
}
else if( fieldOfViewOption.equals( SPECIFIED_PIXEL ) )
{
if( fovSpec.length == 2 )
{
long[] min = new long[]{
(long)Math.floor( offsetSpec[ 0 ] ),
(long)Math.floor( offsetSpec[ 1 ] ) };
long[] max = new long[]{
(long)Math.ceil( offsetSpec[ 0 ] + fovSpec[ 0 ] ),
(long)Math.ceil( offsetSpec[ 0 ] + fovSpec[ 1 ] ) };
ArrayList<Interval> out = new ArrayList<>();
out.add( new FinalInterval( min, max ));
return out;
}
else if( fovSpec.length == 3 )
{
long[] min = new long[]{
(long)Math.floor( offsetSpec[ 0 ] ),
(long)Math.floor( offsetSpec[ 1 ] ),
(long)Math.floor( offsetSpec[ 2 ] ) };
long[] max = new long[]{
(long) Math.ceil(offsetSpec[0] + fovSpec[0]),
(long) Math.ceil(offsetSpec[1] + fovSpec[1]),
(long) Math.ceil(offsetSpec[2] + fovSpec[2]) };
ArrayList<Interval> out = new ArrayList<>();
out.add( new FinalInterval( min, max ));
return out;
}
else
{
System.out.println("Invalid fov spec, length : " + fovSpec.length );
return null;
}
}
else if( fieldOfViewOption.equals( SPECIFIED_PHYSICAL ) )
{
if( fovSpec.length == 2 )
{
long[] min = new long[]{
(long)Math.floor( offsetSpec[ 0 ] / outputResolution[ 0 ]),
(long)Math.floor( offsetSpec[ 1 ] / outputResolution[ 1 ]) };
long[] max = new long[]{
(long)Math.floor( (offsetSpec[ 0 ] + fovSpec[ 0 ] ) / outputResolution[ 0 ]),
(long)Math.floor( (offsetSpec[ 1 ] + fovSpec[ 1 ] ) / outputResolution[ 1 ]) };
ArrayList<Interval> out = new ArrayList<>();
out.add( new FinalInterval( min, max ));
return out;
}
else if( fovSpec.length == 3 )
{
long[] min = new long[]{
(long)Math.floor( offsetSpec[ 0 ] / outputResolution[ 0 ]),
(long)Math.floor( offsetSpec[ 1 ] / outputResolution[ 1 ]),
(long)Math.floor( offsetSpec[ 2 ] / outputResolution[ 2 ]) };
long[] max = new long[]{
(long)Math.floor( (offsetSpec[ 0 ] + fovSpec[ 0 ] ) / outputResolution[ 0 ]),
(long)Math.floor( (offsetSpec[ 0 ] + fovSpec[ 1 ] ) / outputResolution[ 1 ]),
(long)Math.floor( (offsetSpec[ 2 ] + fovSpec[ 2 ] ) / outputResolution[ 2 ]) };
ArrayList<Interval> out = new ArrayList<>();
out.add( new FinalInterval( min, max ));
return out;
}
else
{
System.out.println("Invalid fov spec, length : " + fovSpec.length );
return null;
}
}
else if( fieldOfViewOption.equals( LANDMARK_POINTS ) )
{
List< Double[] > matchedLandmarks = getMatchedPoints( landmarks, fieldOfViewPointFilter );
long[] min = new long[ landmarks.getNumdims() ];
long[] max = new long[ landmarks.getNumdims() ];
Arrays.fill( min, Long.MAX_VALUE );
Arrays.fill( max, Long.MIN_VALUE );
int numPoints = 0;
for ( int i = 0; i < matchedLandmarks.size(); i++ )
{
Double[] pt = matchedLandmarks.get( i );
for ( int d = 0; d < pt.length; d++ )
{
long lo = (long) (Math.floor( pt[ d ] / outputResolution[ d ] ));
long hi = (long) (Math.ceil( pt[ d ] / outputResolution[ d ] ));
if ( lo < min[ d ] )
min[ d ] = lo;
if ( hi > max[ d ] )
max[ d ] = hi;
}
numPoints++;
}
System.out.println( "Estimated field of view using " + numPoints + " landmarks." );
// Make sure something naughty didn't happen
for ( int d = 0; d < min.length; d++ )
{
if ( min[ d ] == Long.MAX_VALUE )
{
System.err.println( "Problem generating field of view from landmarks" );
return null;
}
if ( max[ d ] == Long.MIN_VALUE )
{
System.err.println( "Problem generating field of view from landmarks" );
return null;
}
}
ArrayList<Interval> out = new ArrayList<>();
out.add( new FinalInterval( min, max ));
return out;
}
else if( fieldOfViewOption.equals( LANDMARK_POINT_CUBE_PHYSICAL )
|| fieldOfViewOption.equals( LANDMARK_POINT_CUBE_PIXEL ) )
{
List< Double[] > matchedLandmarks = getMatchedPoints( landmarks, fieldOfViewPointFilter );
if( matchedLandmarks.isEmpty() )
{
System.err.println( "No matching point found" );
return null;
}
final int nd = landmarks.getNumdims();
ArrayList<Interval> out = new ArrayList<>();
for( int i = 0; i < matchedLandmarks.size(); i++ )
{
final Double[] pt = matchedLandmarks.get( i );
long[] min = new long[ nd ];
long[] max = new long[ nd ];
if( fieldOfViewOption.equals( LANDMARK_POINT_CUBE_PHYSICAL ) )
{
for ( int d = 0; d < nd; d++ )
{
min[ d ] = ( long ) Math.floor( (pt[ d ] / outputResolution[ d ]) - ( fovSpec[ d ] / outputResolution[ d ] ) );
max[ d ] = ( long ) Math.ceil( (pt[ d ] / outputResolution[ d ]) + ( fovSpec[ d ] / outputResolution[ d ] ) );
}
}
else
{
for ( int d = 0; d < nd; d++ )
{
min[ d ] = ( long ) Math.floor( (pt[ d ] / outputResolution[ d ]) - ( fovSpec[ d ] ) ) + 1;
max[ d ] = ( long ) Math.ceil( (pt[ d ] / outputResolution[ d ]) + ( fovSpec[ d ] ) ) - 1;
}
}
out.add( new FinalInterval( min, max ) );
}
return out;
}
System.err.println("Invalid field of view option: ( " + fieldOfViewOption + " )" );
return null;
}
public static void fillMatchedPointNames(
final List<String> ptList,
final LandmarkTableModel landmarks,
final String fieldOfViewPointFilter )
{
Pattern r = null;
if ( !fieldOfViewPointFilter.isEmpty() )
r = Pattern.compile( fieldOfViewPointFilter );
for ( int i = 0; i < landmarks.getRowCount(); i++ )
{
if ( r != null && !r.matcher( landmarks.getNames().get( i ) ).matches() )
{
continue;
}
Double[] pt = landmarks.getFixedPoint( i );
if( Double.isInfinite( pt[ 0 ].doubleValue()) )
{
continue;
}
ptList.add( landmarks.getNames().get( i ) );
}
}
public static List<Double[]> getMatchedPoints(
final LandmarkTableModel landmarks,
final String fieldOfViewPointFilter )
{
ArrayList<Double[]> ptList = new ArrayList<>();
Pattern r = null;
if ( !fieldOfViewPointFilter.isEmpty() )
r = Pattern.compile( fieldOfViewPointFilter );
for ( int i = 0; i < landmarks.getRowCount(); i++ )
{
if ( r != null && !r.matcher( landmarks.getNames().get( i ) ).matches() )
{
continue;
}
Double[] pt = landmarks.getFixedPoint( i );
if( Double.isInfinite( pt[ 0 ].doubleValue()) )
{
continue;
}
ptList.add( pt );
System.out.println( "Using point with name : "
+ landmarks.getNames().get( i ) );
}
return ptList;
}
/**
* Get the offset in pixels given the output resolution and interval
*
* @param fieldOfViewOption the field of view option
* @param offsetSpec the offset specification
* @param outputResolution the resolution of the output image
* @param outputInterval the output interval
* @return the offset
*/
public static double[] getPixelOffset(
final String fieldOfViewOption,
final double[] offsetSpec,
final double[] outputResolution,
final Interval outputInterval )
{
double[] offset = new double[ 3 ];
if( fieldOfViewOption.equals( SPECIFIED_PIXEL ) )
{
System.arraycopy( offsetSpec, 0, offset, 0, offset.length );
return offset;
}
else if( fieldOfViewOption.equals( SPECIFIED_PHYSICAL ) )
{
for( int d = 0; d < outputInterval.numDimensions(); d++ )
{
offset[ d ] = offsetSpec[ d ] / outputResolution[ d ];
}
return offset;
}
else
{
for( int d = 0; d < outputInterval.numDimensions(); d++ )
{
offset[ d ] = outputInterval.realMin( d );
}
return offset;
}
}
public static List<ImagePlus> apply(
final ImagePlus movingIp,
final ImagePlus targetIp,
final LandmarkTableModel landmarks,
final String tranformTypeOption,
final String fieldOfViewOption,
final String fieldOfViewPointFilter,
final String resolutionOption,
final double[] resolutionSpec,
final double[] fovSpec,
final double[] offsetSpec,
final Interpolation interp,
final boolean isVirtual,
final boolean wait,
final int nThreads )
{
return apply( movingIp, targetIp, landmarks, tranformTypeOption, fieldOfViewOption,
fieldOfViewPointFilter, resolutionOption,
resolutionSpec, fovSpec, offsetSpec,
interp, isVirtual, nThreads, wait, null );
}
public static List<ImagePlus> apply(
final ImagePlus movingIp,
final ImagePlus targetIp,
final LandmarkTableModel landmarks,
final String tranformTypeOption,
final String fieldOfViewOption,
final String fieldOfViewPointFilter,
final String resolutionOption,
final double[] resolutionSpec,
final double[] fovSpec,
final double[] offsetSpec,
final Interpolation interp,
final boolean isVirtual,
final int nThreads,
final boolean wait,
final WriteDestinationOptions writeOpts )
{
BigWarpData<?> bwData = BigWarpInit.createBigWarpDataFromImages( movingIp, targetIp );
return apply( bwData, landmarks, tranformTypeOption, fieldOfViewOption, fieldOfViewPointFilter,
resolutionOption, resolutionSpec, fovSpec, offsetSpec,
interp, isVirtual, nThreads, wait, writeOpts );
}
public static <T> List<ImagePlus> apply(
final BigWarpData<T> bwData,
final LandmarkTableModel landmarks,
final String tranformTypeOption,
final String fieldOfViewOption,
final String fieldOfViewPointFilter,
final String resolutionOption,
final double[] resolutionSpec,
final double[] fovSpec,
final double[] offsetSpec,
final Interpolation interp,
final boolean isVirtual,
final int nThreads,
final boolean wait,
final WriteDestinationOptions writeOpts )
{
int numChannels = bwData.movingSourceIndices.length;
int[] movingSourceIndexList = bwData.movingSourceIndices;
List< SourceAndConverter< T >> sourcesxfm = BigWarp.wrapSourcesAsTransformed(
bwData.sources,
landmarks.getNumdims(),
bwData );
InvertibleRealTransform invXfm = new BigWarpTransform( landmarks, tranformTypeOption ).getTransformation();
for ( int i = 0; i < numChannels; i++ )
{
((WarpedSource< ? >) (sourcesxfm.get( movingSourceIndexList[ i ]).getSpimSource())).updateTransform( invXfm );
((WarpedSource< ? >) (sourcesxfm.get( movingSourceIndexList[ i ]).getSpimSource())).setIsTransformed( true );
}
ProgressWriter progressWriter = new ProgressWriterIJ();
// Generate the properties needed to generate the transform from output pixel space
// to physical space
final double[] res = getResolution( bwData, resolutionOption, resolutionSpec );
List<Interval> outputIntervalList = getPixelInterval( bwData, landmarks, invXfm, fieldOfViewOption,
fieldOfViewPointFilter, fovSpec, offsetSpec, res );
final List<String> matchedPtNames = new ArrayList<>();
if( outputIntervalList.size() > 1 )
ApplyBigwarpPlugin.fillMatchedPointNames( matchedPtNames, landmarks, fieldOfViewPointFilter );
double[] offset = getPixelOffset( fieldOfViewOption, offsetSpec, res, outputIntervalList.get( 0 ) );
if( writeOpts != null && writeOpts.n5Dataset != null && !writeOpts.n5Dataset.isEmpty())
{
final String unit = ApplyBigwarpPlugin.getUnit( bwData, resolutionOption );
ApplyBigwarpPlugin.runN5Export( bwData, sourcesxfm, fieldOfViewOption,
outputIntervalList.get( 0 ), interp,
offset, res, unit,
progressWriter, writeOpts,
Executors.newFixedThreadPool( nThreads ) );
return null;
}
else
{
final boolean show = (writeOpts.pathOrN5Root == null || writeOpts.pathOrN5Root.isEmpty());
return runExport( bwData, sourcesxfm, fieldOfViewOption,
outputIntervalList, matchedPtNames, interp,
offset, res, isVirtual, nThreads,
progressWriter, show, wait, writeOpts );
}
}
public static <T> List<ImagePlus> runExport(
final BigWarpData<T> data,
final List< SourceAndConverter< T >> sources,
final String fieldOfViewOption,
final List<Interval> outputIntervalList,
final List<String> matchedPtNames,
final Interpolation interp,
final double[] offsetIn,
final double[] resolution,
final boolean isVirtual,
final int nThreads,
final ProgressWriter progressWriter,
final boolean show,
final boolean wait,
final WriteDestinationOptions writeOpts )
{
ArrayList<ImagePlus> ipList = new ArrayList<>();
int i = 0;
for( Interval outputInterval : outputIntervalList )
{
double[] offset = ApplyBigwarpPlugin.getPixelOffset( fieldOfViewOption, offsetIn, resolution, outputIntervalList.get( i ) );
// need to declare the exporter in the loop since the actual work
// is done asynchronously, and changing variables in the loop would mess it up
BigWarpExporter<?> exporter = BigWarpExporter.getExporter( data, sources, interp, progressWriter );
exporter.setOutputList( ipList );
exporter.setRenderResolution( resolution );
exporter.setOffset( offset );
exporter.setVirtual( isVirtual );
exporter.setNumThreads( nThreads );
if( writeOpts!= null && writeOpts.pathOrN5Root != null )
exporter.setExportPath( writeOpts.pathOrN5Root );
exporter.setInterval( outputInterval );
if( matchedPtNames.size() > 0 )
exporter.setNameSuffix( matchedPtNames.get( i ));
// never wait
exporter.exportAsynch( wait, show );
i++;
}
return ipList;
}
public static <S, T extends NativeType<T> & NumericType<T>> void runN5Export(
final BigWarpData<S> data,
final List< SourceAndConverter< S >> sources,
final String fieldOfViewOption,
final Interval outputInterval,
final Interpolation interp,
final double[] offset,
final double[] resolution,
final String unit,
final ProgressWriter progressWriter,
final WriteDestinationOptions writeOpts,
final ExecutorService exec )
{
final int nd = BigWarp.detectNumDims( data.sources );
// setup n5 parameters
final String dataset = writeOpts.n5Dataset;
final int[] blockSize = writeOpts.blockSize;
final Compression compression = writeOpts.compression;
if( dataset == null || dataset.isEmpty() )
{
return;
}
N5Writer n5;
try
{
n5 = new N5Factory().openWriter( writeOpts.pathOrN5Root );
}
catch ( IOException e1 )
{
e1.printStackTrace();
return;
}
// build metadata
final String[] axes = nd == 2 ? new String[] { "y", "x" } :new String[]{ "z", "y", "x" } ;
final String[] units = nd == 2 ? new String[]{ unit, unit } : new String[] { unit, unit, unit };
final N5CosemMetadata metadata = new N5CosemMetadata( "", new N5CosemMetadata.CosemTransform( axes, resolution, offset, units ), null );
N5CosemMetadataParser parser = new N5CosemMetadataParser();
// setup physical to pixel transform
final AffineTransform3D resolutionTransform = new AffineTransform3D();
resolutionTransform.set( resolution[ 0 ], 0, 0 );
resolutionTransform.set( resolution[ 1 ], 1, 1 );
if( resolution.length > 2 )
resolutionTransform.set( resolution[ 2 ], 2, 2 );
final AffineTransform3D offsetTransform = new AffineTransform3D();
offsetTransform.set( offset[ 0 ], 0, 3 );
offsetTransform.set( offset[ 1 ], 1, 3 );
if( resolution.length > 2 )
offsetTransform.set( offset[ 2 ], 2, 3 );
final AffineTransform3D pixelRenderToPhysical = new AffineTransform3D();
pixelRenderToPhysical.concatenate( resolutionTransform );
pixelRenderToPhysical.concatenate( offsetTransform );
// render and write
final int N = data.movingSourceIndices.length;
for ( int i = 0; i < N; i++ )
{
final int movingSourceIndex = data.movingSourceIndices[ i ];
@SuppressWarnings( "unchecked" )
final RealRandomAccessible< T > raiRaw = ( RealRandomAccessible< T > )sources.get( movingSourceIndex ).getSpimSource().getInterpolatedSource( 0, 0, interp );
// to pixel space
final AffineRandomAccessible< T, AffineGet > rai = RealViews.affine( raiRaw, pixelRenderToPhysical.inverse() );
final IntervalView< T > img = Views.interval( Views.raster( rai ),
Intervals.zeroMin( outputInterval ) );
final String srcName = data.sources.get( data.movingSourceIndices[ i ]).getSpimSource().getName();
String destDataset = dataset;
if( N > 1 )
destDataset = dataset + String.format( "/%s", srcName.replace( " " , "_" ));
RandomAccessibleInterval<T> imgToWrite;
if( nd == 2 )
imgToWrite = Views.hyperSlice( img, 2, 0 );
else
imgToWrite = img;
try
{
N5Utils.save( imgToWrite, n5, destDataset, blockSize, compression, exec );
if( parser != null && metadata != null )
parser.writeMetadata( metadata, n5, destDataset );
}
catch ( Exception e )
{
e.printStackTrace();
}
progressWriter.setProgress( (i+1) / ((double)N) );
}
progressWriter.setProgress( 1.0 );
}
@Override
public void run()
{
if ( IJ.versionLessThan( "1.40" ) )
return;
// Find any open images
final int[] ids = WindowManager.getIDList();
final int N = ids == null ? 0 : ids.length;
final String[] titles = new String[ N + 1 ];
for ( int i = 0; i < N; ++i )
{
titles[ i ] = ( WindowManager.getImage( ids[ i ] ) ).getTitle();
}
titles[ N ] = "<None>";
final GenericDialogPlus gd = new GenericDialogPlus( "Apply Big Warp transform" );
gd.addMessage( "File Selection:" );
gd.addFileField( "landmarks_image_file", "" );
ImagePlus currimg = WindowManager.getCurrentImage();
String current = titles[ N ];
if ( currimg != null )
{
current = currimg.getTitle();
}
gd.addChoice( "moving_image", titles, current );
if ( titles.length > 1 )
gd.addChoice( "target_image", titles, current.equals( titles[ 0 ] ) ? titles[ 1 ] : titles[ 0 ] );
else
gd.addChoice( "target_image", titles, titles[ 0 ] );
gd.addMessage( "\nN5/Zarr/HDF5/BDV-XML" );
gd.addDirectoryOrFileField( "Moving", "" );
gd.addStringField( "Moving_dataset", "" );
gd.addDirectoryOrFileField( "Target", "" );
gd.addStringField( "Target_dataset", "" );
gd.addChoice( "Transform type",