-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMosaicExplorerJ_Latest.ijm
1494 lines (1402 loc) · 50 KB
/
MosaicExplorerJ_Latest.ijm
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
/////////////////////////////////////////////////////////////////////////////////////////////
//
// Name: MosaicExplorer
// Author: Sébastien Tosi (IRB/ADMCF)
// Version: 1.8
// Date: 07-02-2022
//
// Description: An ImageJ script to align and stitch three-dimensional tiles and quickly
// explore terabyte-sized microscopy datasets.
//
// Usage: See documentation at https://github.com/SebastienTs/MosaicExplorerJ
//
/////////////////////////////////////////////////////////////////////////////////////////////
macro "MosaicExplorerJ [F2]"
{
// Color Blind Mode
ColorBlind = false;
// Default channel long names
ChanStr = newArray("--C00","--C01","XXX--XXX","XXX--XXX","XXX--XXX","XXX--XXX","XXX--XXX","XXX--XXX");
// Tiles: 3D Images / subfolders / 2D Images naming convention
XString = "--X";XDigits = 2; // Tile grid X coordinate
YString = "--Y";YDigits = 2; // Tile grid Y coordinate
CString = "--C";CDigits = 2; // Channel short name (starts at 0)
RLString = "RL";RLDigits = 2; // Illumination side (0/1)
// Additional file naming convention for 2D images
ZString = "Zb00--Z";ZDigits = 4; // Z coordinate
CAMString = "CAM"; // Camera side (1/2)
// Close all images
run("Close All");
// Root folder of the scan
RootFolder = getDirectory("Select scan root folder");
// Dialog box: scan configuration
Dialog.create("Scan configuration");
Dialog.addNumber("Tile width (pix)",2048);
Dialog.addNumber("Tile height (pix)",2048);
Dialog.addNumber("Side margins (pix)",512);
for(c=0;c<8;c++)Dialog.addString("Channel "+d2s(c,0),ChanStr[c]);
Dialog.addCheckbox("Dual side",true);
Dialog.addCheckbox("Dual camera",true);
Dialog.addCheckbox("Color mode",true);
Dialog.addCheckbox("Auto Detect X,Y,Z,C",true);
Dialog.show();
ImageWidth = Dialog.getNumber();
ImageHeight = Dialog.getNumber();
SideMargins = Dialog.getNumber();
for(c=0;c<8;c++)ChanStr[c] = Dialog.getString();
DualSide = Dialog.getCheckbox();
DualCAM = Dialog.getCheckbox();
EnableColorMode = Dialog.getCheckbox();
AutoXYZC = Dialog.getCheckbox();
// Check if the 3D tiles are subfolders of 2D tif files or multi-tiff files
FileList = getFileList(RootFolder);
FolderMode = false;
for(i=0;i<lengthOf(FileList);i++)if(endsWith(FileList[i],'/'))FolderMode = true;
// Check if a configuration file is already saved in the root folder
tst = File.exists(RootFolder+"ScanStitch.csv");
if(tst)UseFileParams = getBoolean("Import existing tile grid settings?");
else UseFileParams = 0;
// Tile grid alignment parameters
OverlapX = newArray(0,0,0,0);
OverlapY = newArray(0,0,0,0);
CorrX = newArray(0,0,0,0);
CorrY = newArray(0,0,0,0);
CorrZX = newArray(0,0,0,0);
CorrZY = newArray(0,0,0,0);
MaxInt = newArray(2047,2047,2047,2047,2047,2047,2047,2047,2047,2047,2047,2047,2047,2047,2047,2047);
CAMAng = newArray(0,0);
CAMSca = newArray(1,1);
CAMXCor = newArray(0,0);
CAMYCor = newArray(0,0);
CorrRLX = newArray(0,0);
CorrRLY = newArray(0,0);
CropFracWidth = 0; // Fractional crop width of left side last column / right side first column
CorrRLZ = newArray(0,0);
Steps = 5;
BigSteps = 5;
RegRight = false;
OverlayCAM1 = false;
CAMOverlay = false;
NudgeMode = false;
SpaceMode = false;
FreeXYCorr = false;
DisplayLUT = "Grays";
// Load tile grid alignment parameters from configuration file (if present)
if(UseFileParams)
{
RawParams = File.openAsString(RootFolder+"ScanStitch.csv");
RawParams = split(RawParams,"\n");
// Main parameters are on first line
FirstLine = RawParams[0];
Params = split(FirstLine,",");
FloatParams = newArray(lengthOf(Params));
for(i=0;i<lengthOf(Params);i++)FloatParams[i] = parseFloat(Params[i]);
if(DualSide == false)
{
Nprm = 24;
if(lengthOf(Params)!=48)exit("Saved parameters are not for single side experiment");
}
else
{
Nprm = 28;
if(lengthOf(Params)!=56)exit("Saved parameters are not for dual side experiment");
}
// Loop over sides (tile grid alignment is independent for both sides, and bot cameras)
for(s=0;s<=1;s++)
{
OverlapX[0+2*s] = FloatParams[0+Nprm*s]; // Camera 1
OverlapX[1+2*s] = FloatParams[1+Nprm*s]; // Camera 2
OverlapY[0+2*s] = FloatParams[2+Nprm*s]; // Camera 1
OverlapY[1+2*s] = FloatParams[3+Nprm*s]; // Camera 2
CorrX[0+2*s] = FloatParams[4+Nprm*s]; // ...
CorrX[1+2*s] = FloatParams[5+Nprm*s];
CorrY[0+2*s] = FloatParams[6+Nprm*s];
CorrY[1+2*s] = FloatParams[7+Nprm*s];
CorrZX[0+2*s] = FloatParams[8+Nprm*s];
CorrZX[1+2*s] = FloatParams[9+Nprm*s];
CorrZY[0+2*s] = FloatParams[10+Nprm*s];
CorrZY[1+2*s] = FloatParams[11+Nprm*s];
for(c=0;c<8;c++)MaxInt[c+8*s] = FloatParams[12+c+Nprm*s];
CAMAng[s] = FloatParams[20+Nprm*s];
CAMSca[s] = FloatParams[21+Nprm*s];
CAMXCor[s] = FloatParams[22+Nprm*s];
CAMYCor[s] = FloatParams[23+Nprm*s];
if(DualSide==1)
{
c = s; // Here, loop is over cameras, not sides
CorrRLX[c] = FloatParams[24+Nprm*c];
CorrRLY[c] = FloatParams[25+Nprm*c];
CorrRLZ[c] = FloatParams[26+Nprm*c];
CropFracWidth = FloatParams[27+Nprm*c];
}
}
// Free Zxy correction is on second line (if used)
if(lengthOf(RawParams)>1)
{
SecondLine = RawParams[1];
Params2 = split(SecondLine,",");
ZManOffs = newArray(lengthOf(Params2));
for(i=0;i<lengthOf(Params2);i++)ZManOffs[i] = parseFloat(Params2[i]);
FreeZxyCorr = true;
}
else
{
ZManOffs = newArray(1);
FreeZxyCorr = false;
}
}
else
{
ZManOffs = newArray(1);
FreeZxyCorr = false;
}
// Init
if(isOpen("ROI Manager"))
{
selectWindow("ROI Manager");
run("Close");
}
ColorMode = EnableColorMode; // Color-code adjacent fields of view
ReDraw = true; // Redraw current slice
StitchMode = "Add"; // Tile copy mode
IntCorr = false; // Apply intensity correction (from files or linear)
CCur = 0; // Current channel
CamCur = 1; // Current camera
SidCur = 1; // Current illumination side
ShowDual = DualSide; // Display both illumination sides
ExportStack = 0; // Export tile grid to folder
Exit = false; // Exit macro
ZMax = NaN; // Last slice
// Parse filenames to retrieve grid configuration
FileList = getFileList(RootFolder);
// Find tile grid minimum and maximum X/Y/Z/C
XMin = 1/0;YMin = 1/0;XMax = 0;YMax = 0;CMin = 1/0; CMax = 0;Test = false;
if(FolderMode)FilterStr = "/";
else FilterStr = ".tif";
for(i=0;i<lengthOf(FileList);i++)
{
if(endsWith(FileList[i],FilterStr))
{
// Only for first tile / folder
if(Test == false)
{
if(FolderMode)
{
FolderNameTemplate = FileList[i];
FileList2 = getFileList(RootFolder+FileList[i]);
FileNameTemplate = FileList2[0];
FirstFileName = FileList2[0];
LastFileName = FileList2[lengthOf(FileList2)-1];
ZMax = parseInt(substring(LastFileName,indexOf(LastFileName,ZString)+lengthOf(ZString),indexOf(LastFileName,ZString)+lengthOf(ZString)+ZDigits));
ZMin = parseInt(substring(FirstFileName,indexOf(FirstFileName,ZString)+lengthOf(ZString),indexOf(FirstFileName,ZString)+lengthOf(ZString)+ZDigits));
}
else
{
FolderNameTemplate = "";
FileNameTemplate = FileList[i];
ZMin = 0;
setBatchMode(true);
run("TIFF Virtual Stack...", "open=["+RootFolder+FileList[i]+"]");
ZMax = nSlices/(DualCAM+1)-1;
close();
setBatchMode("exit & display");
}
Test = true;
}
Name = FileList[i];
Idx = indexOf(Name,XString);
XInd = parseInt(substring(Name,Idx+lengthOf(XString),Idx+lengthOf(XString)+XDigits));
Idx = indexOf(Name,YString);
YInd = parseInt(substring(Name,Idx+lengthOf(YString),Idx+lengthOf(YString)+YDigits));
Idx = indexOf(Name,CString);
CInd = parseInt(substring(Name,Idx+lengthOf(CString),Idx+lengthOf(CString)+CDigits));
XMin = minOf(XMin,XInd);
YMin = minOf(YMin,YInd);
CMin = minOf(CMin,CInd);
XMax = maxOf(XMax,XInd);
YMax = maxOf(YMax,YInd);
CMax = maxOf(CMax,CInd);
}
}
// Overide detected X,Y,Z,C bounds
if(!AutoXYZC)
{
Dialog.create("Set dimensions manually");
Dialog.addNumber("XMin",0);
Dialog.addNumber("XMax",1);
Dialog.addNumber("YMin",0);
Dialog.addNumber("YMax",1);
Dialog.addNumber("ZMin",0);
Dialog.addNumber("ZMax",1);
Dialog.addNumber("CMin",0);
Dialog.addNumber("CMax",0);
Dialog.show();
XMin = Dialog.getNumber();
XMax = Dialog.getNumber();
YMin = Dialog.getNumber();
YMax = Dialog.getNumber();
ZMin = Dialog.getNumber();
ZMax = Dialog.getNumber();
CMin = Dialog.getNumber();
CMax = Dialog.getNumber();
Test = true;
}
// Bounds of currently displayed tile grid
XdMin = XMin;
XdMax = XMax;
YdMin = YMin;
YdMax = YMax;
NCol = (XMax-XMin+1);
NRow = (YMax-YMin+1);
NFields = NRow*NCol;
// Free XY correction (optional, only used to correct for residual error)
FreeCorrX = newArray(NFields*4); // up to 2 cameras + 2 sides
FreeCorrY = newArray(NFields*4);
// Current ZSlice
ZCur = round((ZMax+ZMin)/2);
// Check that a tile grid has been detected
if((Test==false)||(isNaN(ZMax)))exit("Invalid root folder");
// Last diaplayed tile center tables
XTile = newArray((XMax-XMin+1)*(YMax-YMin+1)*2);
YTile = newArray((XMax-XMin+1)*(YMax-YMin+1)*2);
XiTile = newArray((XMax-XMin+1)*(YMax-YMin+1)*2);
YiTile = newArray((XMax-XMin+1)*(YMax-YMin+1)*2);
// Check that number of channels does not exceed 8
if(CMax>8)
{
showMessage("Only up to 8 channels supported");
exit;
}
// Initialize manual Zxy correction if not loaded from file
if(lengthOf(ZManOffs)==1)ZManOffs = newArray((XMax+1-XMin)*(YMax+1-YMin)*4);
// Assign default alignment for right side tile grids when no configuration file is loaded (no overlap)
if(!UseFileParams)
{
CorrRLX[0] = ImageWidth*(XMax-XMin+1)*DualSide;
CorrRLX[1] = ImageWidth*(XMax-XMin+1)*DualSide;
}
// Create board
if(EnableColorMode == true)newImage("Board", "RGB black", ImageWidth*(XMax-XMin+1)*(DualSide+1)+2*SideMargins, ImageHeight*(YMax-YMin+1)+2*SideMargins, 1);
else newImage("Board", "16-bit black", ImageWidth*(XMax-XMin+1)*(DualSide+1)+2*SideMargins, ImageHeight*(YMax-YMin+1)+2*SideMargins, 1);
BoardID = getImageID();
// Main loop
ShowHelp = true;
while(isOpen(BoardID))
{
// Help message
if((isOpen("CAM1"))&&(CAMOverlay==false))showStatus("Draw two pairs of matching points first, next a pair of matching points");
wait(50);
if(NudgeMode == true)showStatus("Tile Z correction mode: (Shift) Add Zstep (Space) Scroll Zstep (Alt) Stop");
if(ShowHelp == true)showStatus("Press F1 for command help (requires to install macro)");
// Color blind mode LUTs
if(ColorBlind == true)
{
Col1 = "Cyan";
Col2 = "Yellow";
}
else
{
Col1 = "Red";
Col2 = "Green";
}
if(ReDraw == true)
{
// Cleanup and initialize
selectImage(BoardID);
if(FreeZxyCorr==false)rename("Board (Z="+d2s(ZCur,0)+" CorrZx="+d2s(CorrZX[CamCur-1+2*(SidCur-1)],0)+" CorrZy="+d2s(CorrZY[CamCur-1+2*(SidCur-1)],0)+")");
else
{
ZdMax = ZManOffs[YdMax+XdMax*(YMax+1-YMin)+(SidCur-1)*(YMax+1-YMin)*(XMax+1-XMin)];
rename("Board (Z="+d2s(ZCur,0)+" CorZxy="+d2s(ZdMax,0)+" @LowerRight)");
}
run("Select All");
run("Clear");
run("Select None");
setTool("zoom");
setBatchMode(true);
// Ramp blending should use "add" copy/paste
StitchModeCopy = StitchMode;
if(StitchModeCopy=="Ramp")StitchModeCopy = "Add";
setPasteMode(StitchModeCopy);
// Additional loop for dual side mode
SidMin=SidCur;SidMax=SidCur;
if(ShowDual==true)
{
SidMin = 1;
SidMax = 2;
}
// Compute Ramp Masks if needed
if((StitchMode=="Ramp")||(IntCorr==true))
{
BlendMask(OverlapX[CamCur-1]/100,OverlapY[CamCur-1]/100,0,CCur,CamCur,IntCorr);
if(DualSide == true)BlendMask(OverlapX[CamCur-1+2]/100,OverlapY[CamCur-1+2]/100,1,CCur,CamCur,IntCorr);
selectImage(BoardID);
}
// Read free XY correction if needed
if(FreeXYCorr==true)
{
FileNameX = "ScanXCorr.csv";
FileNameY = "ScanYCorr.csv";
tstX = File.exists(RootFolder+FileNameX);
tstY = File.exists(RootFolder+FileNameY);
if(tstX&&tstY)
{
RawParams = File.openAsString(RootFolder+FileNameX);
RawParams = split(RawParams,"\n");
cntRow = 0;
for(n=0;n<4;n++)
{
for(j=0;j<NRow;j++)
{
CurrentRow = split(RawParams[cntRow]," ");
for(i=0;i<NCol;i++)
{
FreeCorrX[i+j*NCol+n*NFields] = CurrentRow[i];
}
cntRow = cntRow+1;
}
cntRow = cntRow+2;
}
RawParams = File.openAsString(RootFolder+FileNameY);
RawParams = split(RawParams,"\n");
cntRow = 0;
for(n=0;n<4;n++)
{
for(j=0;j<NRow;j++)
{
CurrentRow = split(RawParams[cntRow]," ");
for(i=0;i<NCol;i++)
{
FreeCorrY[i+j*NCol+n*NFields] = CurrentRow[i];
}
cntRow = cntRow+1;
}
cntRow = cntRow+2;
}
}
else
{
XYStr = "";
for(n=0;n<4;n++)
{
for(j=0;j<NRow;j++)
{
for(i=0;i<NCol;i++)
{
XYStr = XYStr + "0 ";
}
XYStr = XYStr + "\n";
}
XYStr = XYStr + "\n\n";
}
File.saveString(XYStr,RootFolder+FileNameX);
File.saveString(XYStr,RootFolder+FileNameY);
}
}
// Initialize optimal crop bounding box
if(CamCur == 1)
{
BBStartX = 1/0;BBStartY = 1/0;BBEndX = 0;BBEndY = 0;
}
// Paste all images from tile grid
for(SidCur2=SidMin;SidCur2<=SidMax;SidCur2++)
{
// Effective XY size of a tile without overlap
CropWidth = round(ImageWidth*(100-OverlapX[CamCur-1+2*(SidCur2-1)])/100);
CropHeight = round(ImageHeight*(100-OverlapY[CamCur-1+2*(SidCur2-1)])/100);
for(j=YdMin;j<=YdMax;j++)
{
for(i=XdMin;i<=XdMax;i++)
{
// Update filename to load correct file
ImageName = replace(FileNameTemplate, XString+IJ.pad(XMin,XDigits), XString+IJ.pad(i,XDigits));
ImageName = replace(ImageName, YString+IJ.pad(YMin,YDigits), YString+IJ.pad(j,YDigits));
ImageName = replace(ImageName, RLString+IJ.pad(0,RLDigits), RLString+IJ.pad(SidCur2-1,RLDigits));
// Acount for Z correction
if(FreeZxyCorr)ZCorrected = ZCur+CorrRLZ[CamCur-1]*(SidCur2-1)+ZManOffs[j+i*(YMax+1-YMin)+(SidCur2-1)*(YMax+1-YMin)*(XMax+1-XMin)]+CorrRLZ[CamCur-1]*(SidCur2-1); // Manual ZOffs
else ZCorrected = ZCur+CorrZX[CamCur-1+2*(SidCur2-1)]*i+CorrZY[CamCur-1+2*(SidCur2-1)]*j+CorrRLZ[CamCur-1]*(SidCur2-1); // Linear ZOffs
// Tiles are stored as folders of images
if(FolderMode)
{
ImageName = replace(ImageName, ChanStr[0], ChanStr[CCur]);
ImageName = replace(ImageName, CAMString+"1", CAMString+d2s(CamCur,0));
ImageName = replace(ImageName, ZString+IJ.pad(ZMin,ZDigits), ZString+IJ.pad(ZCorrected,ZDigits));
FolderName = replace(FolderNameTemplate, XString+IJ.pad(XMin,XDigits), XString+IJ.pad(i,XDigits));
FolderName = replace(FolderName, YString+IJ.pad(YMin,YDigits), YString+IJ.pad(j,YDigits));
FolderName = replace(FolderName, CString+IJ.pad(0,CDigits), CString+IJ.pad(CCur,CDigits));
FolderName = replace(FolderName, RLString+IJ.pad(0,RLDigits), RLString+IJ.pad(SidCur2-1,RLDigits));
// If an image is not found display a black image of same size instead
if(File.exists(RootFolder+FolderName+ImageName))open(RootFolder+FolderName+ImageName);
else newImage("Black", "16-bit black", ImageWidth, ImageHeight, 1);
}
else // Tiles are stored as 3D multi-TIFF files
{
ImageName = replace(ImageName, CString+IJ.pad(0,CDigits), CString+IJ.pad(CCur,CDigits));
// If an image is not found display a black image of same size instead
if((ZCorrected>=0)&&(ZCorrected<=ZMax)&&File.exists(RootFolder+ImageName))open(RootFolder+ImageName,1+ZCorrected+ZMax*(CamCur-1)*DualCAM);
else newImage("Black", "16-bit black", ImageWidth, ImageHeight, 1);
}
// Apply intensity correction to right side for dual side mode and different saturation intensity settings
if((DualSide)&&(MaxInt[CCur+8]/MaxInt[CCur]!=1)&&(SidCur2==2))run("Multiply...", "value="+d2s(MaxInt[CCur]/MaxInt[CCur+8],4));
// Apply tile blending ramp
if((StitchMode=="Ramp")||(IntCorr==true))
{
rename("Img");
run("32-bit");
if(SidCur2==1)imageCalculator("Multiply","Img","Mask1");
else imageCalculator("Multiply","Img","Mask2");
setMinAndMax(0,65535);
run("16-bit");
}
// Color mode: color-code adjacent fields of view with alternating red/green or cyan/yellow
if(EnableColorMode==true)
{
if((ColorMode==true)&&(isOpen("CAM1")==false))
{
if((((j-YdMin)%2)==0)^(((i-XdMin)%2)==0)^(((SidCur2-1)==0)&&((XMax+1-XMin)%2==1))^(CamCur==1))run(Col2);
else run(Col1);
}
else
{
if(isOpen("CAM1")==true)run(Col2);
else run(DisplayLUT);
}
if(DualSide)setMinAndMax(0,MaxInt[CCur]);
else setMinAndMax(0,MaxInt[CCur+8*(SidCur2-1)]);
run("RGB Color");
}
// Flip image horizontally for second camera
if(CamCur == 2)run("Flip Horizontally");
// Optionally apply cropping to right side tile grid to reduce overlap
if((ShowDual)&&(CropFracWidth!=0))
{
if((i==XdMax)&&(SidCur2==1))
{
makeRectangle(ImageWidth-round(ImageWidth*CropFracWidth),0,ImageWidth,ImageHeight);
run("Clear", "slice");
run("Select None");
}
if((i==XdMin)&&(SidCur2==2))
{
makeRectangle(0,0,round(ImageWidth*CropFracWidth),ImageHeight);
run("Clear", "slice");
run("Select None");
}
}
// Copy the image
run("Select None");
run("Copy");
close();
// Paste the image at the correct location in the tile grid
makeRectangle(CropWidth*(i-XMin)-CorrX[CamCur-1+2*(SidCur2-1)]*j+SideMargins+CorrRLX[CamCur-1]*(SidCur2-1)+FreeCorrX[i+j*NCol+(CamCur-1+(SidCur2-1)*2)*NFields],CropHeight*(j-YMin)-CorrY[CamCur-1+2*(SidCur2-1)]*i+SideMargins+CorrRLY[CamCur-1]*(SidCur2-1)+FreeCorrY[i+j*NCol+(CamCur-1+(SidCur2-1)*2)*NFields],ImageWidth,ImageHeight);
if(CamCur == 1) // Compute bounding box for CAM1
{
getSelectionBounds(CXs, CYs, width, height);
CXe = CXs + width;
CYe = CYs + height;
if(CXs < BBStartX)BBStartX = CXs;
if(CYs < BBStartY)BBStartY = CYs;
if(CXe > BBEndX)BBEndX = CXe;
if(CYe > BBEndY)BBEndY = CYe;
}
run("Paste");
}
}
// Ensure no selection is active
run("Select None");
// Intensity saturation adjustment for grayscale mode
if(EnableColorMode==false)
{
if(DualSide)setMinAndMax(0,MaxInt[CCur]);
else setMinAndMax(0,MaxInt[CCur+8*(SidCur2-1)]);
}
} // End of tile pasting loop
// Compute all possible tile positions for both sides
cnt = 0;
for(SidCur2=1;SidCur2<=2;SidCur2++)
{
for(j=0;j<=YMax;j++)
{
for(i=0;i<=XMax;i++)
{
XTile[cnt] = CropWidth*(i-XMin)-CorrX[CamCur-1+2*(SidCur2-1)]*j+SideMargins+CorrRLX[CamCur-1]*(SidCur2-1)+ImageWidth/2;
YTile[cnt] = CropHeight*(j-YMin)-CorrY[CamCur-1+2*(SidCur2-1)]*i+SideMargins+CorrRLY[CamCur-1]*(SidCur2-1)+ImageHeight/2;
XiTile[cnt] = i;
YiTile[cnt] = j;
cnt++;
}
}
}
// Apply CAM2 tilt and scaling if CAM1 overlay is active
if((isOpen("CAM1"))&&(CamCur==2))
{
if(CAMAng[SidCur-1]!=0)run("Rotate... ", "angle="+d2s(CAMAng[SidCur-1],2)+" grid=1 interpolation=None");
if(CAMSca[SidCur-1]!=1)run("Scale...", "x="+d2s(CAMSca[SidCur-1],4)+" y="+d2s(CAMSca[SidCur-1],4)+" interpolation=Bilinear average");
if((CAMXCor[SidCur-1]!=0)||(CAMYCor[SidCur-1]!=0))run("Translate...", "x="+d2s(CAMXCor[SidCur-1],0)+" y="+d2s(CAMYCor[SidCur-1],0)+" interpolation=None");
}
if((isOpen("CAM1"))&&(CAMOverlay==true))
{
run("Remove Overlay");
run("Add Image...", "image=CAM1 x=0 y=0 opacity=50");
}
// Close Ramp Masks if opened
if((StitchMode=="Ramp")||(IntCorr==true))
{
selectImage("Mask1");
close();
if(DualSide)
{
selectImage("Mask2");
close();
}
selectImage(BoardID);
}
// Exit batch mode
setBatchMode("exit & display");
ReDraw = false;
}
// Adjust Z adjustment direction + switch between 1 and BigSteps
if(isKeyDown("space"))
{
// If space is pressed twice in a row, swicth between 1 and BigSteps
if(SpaceMode == true)
{
if(abs(Steps) == 1)Steps = -Steps*BigSteps;
else Steps = -Steps/abs(Steps);
SpaceMode = false;
}
else // Invert step direction
{
SpaceMode = true;
Steps = -Steps;
}
showStatus("ZStep: "+d2s(Steps,0));
// Wait space to be released
while(isKeyDown("space"))wait(50);
}
// Shortcut to nudge Z slice and Z correction
if(isKeyDown("shift"))
{
// Exit "Space mode" (adjusting step)
SpaceMode = false;
// Use mouse coordinates to locate active tile
getCursorLoc(x, y, z, flags);
// Use to avoid the nasty behaviour of IJ to "lose" cursor coordinates after pressing space
if(x == -1)
{
x = LastX;
y = LastY;
}
else
{
LastX = x;
LastY = y;
}
mindst = 1/0;
minidx = 0;
N = lengthOf(XTile)/2;
for(i=(SidCur-1)*N;i<SidCur*N;i++)
{
dst = sqrt(pow(x-XTile[i],2)+pow(y-YTile[i],2));
if(dst<mindst)
{
mindst = dst;
minidx = i;
}
}
// Nudge CorrZx (global or column)
if(XiTile[minidx]>XdMin)
{
//if(x<XTile[minidx])
//{
XdMin = XiTile[minidx]-1;
YdMin = YiTile[minidx];
XdMax = XiTile[minidx];
YdMax = YiTile[minidx];
if(NudgeMode == true)
{
if(FreeZxyCorr==false)CorrZX[CamCur-1+2*(SidCur-1)] = CorrZX[CamCur-1+2*(SidCur-1)] + Steps;
else for(i=XiTile[minidx];i<=(XMax-XMin);i++)for(j=YiTile[minidx];j<=(YMax-YMin);j++)ZManOffs[j+i*(YMax+1-YMin)+(SidCur-1)*(YMax+1-YMin)*(XMax+1-XMin)] = ZManOffs[j+i*(YMax+1-YMin)+(SidCur-1)*(YMax+1-YMin)*(XMax+1-XMin)] + Steps;
}
else showStatus("Tile adjustment mode");
ShowDual = false;
ReDraw = true;
NudgeMode = true;
//}
}
// Nudge CorrZy (global or row)
if(YiTile[minidx]>YdMin)
{
//if(y<YTile[minidx])
//{
XdMin = XiTile[minidx];
YdMin = YiTile[minidx]-1;
XdMax = XiTile[minidx];
YdMax = YiTile[minidx];
if(NudgeMode == true)
{
if(FreeZxyCorr==false)CorrZY[CamCur-1+2*(SidCur-1)] = CorrZY[CamCur-1+2*(SidCur-1)] + Steps;
else for(j=YiTile[minidx];j<=(YMax-YMin);j++)for(i=XiTile[minidx];i<=(XMax-XMin);i++)ZManOffs[j+i*(YMax+1-YMin)+(SidCur-1)*(YMax+1-YMin)*(XMax+1-XMin)] = ZManOffs[j+i*(YMax+1-YMin)+(SidCur-1)*(YMax+1-YMin)*(XMax+1-XMin)] + Steps;
}
ShowDual = false;
ReDraw = true;
NudgeMode = true;
//}
}
// Actions from left uppermost visible tile
if((XiTile[minidx]==XdMin)&&(YiTile[minidx]==YdMin))
{
// Nudge current Z slice
//ZCur = ZCur + (-1+2*(x>XTile[minidx]))*Steps;
ZCur = ZCur + Steps;
ReDraw = true;
}
// Wait shift to be released
while(isKeyDown("shift"))wait(50);
}
// Open configuration panel
if(isKeyDown("alt"))
{
// We assume user read the message
ShowHelp = false;
//Exit nudge mode and restore complete grid
if(NudgeMode==true)
{
XdMin = XMin;
YdMin = YMin;
XdMax = XMax;
YdMax = YMax;
ReDraw = true;
NudgeMode = false;
}
else
{
// Dialog box: control panel
OldColorMode = ColorMode;
CamCurOld = CamCur;
CCurOld = CCur;
SidCurOld = SidCur;
ZCurOld = ZCur;
CCurOld = CCur;
StitchModeOld = StitchMode;
ColorModeOld = ColorMode;
Dialog.create("Control Panel");
Dialog.addSlider("ZPos", ZMin, ZMax, ZCur);
Dialog.addSlider("CPos", 0, CMax, CCur);
Dialog.addSlider("MaxInt", 1, 65535, MaxInt[CCur+8*(SidCur-1)]);
if(!isOpen("CAM1"))Dialog.addSlider("CAM", 1, DualCAM+1, CamCur);
Dialog.addSlider("Side", 1, DualSide+1, SidCur);
Dialog.addNumber("OvlX (%)", OverlapX[CamCur-1+2*(SidCur-1)]);
Dialog.addNumber("OvlY (%)", OverlapY[CamCur-1+2*(SidCur-1)]);
Dialog.addNumber("XCor", CorrX[CamCur-1+2*(SidCur-1)]);
Dialog.addNumber("YCor", CorrY[CamCur-1+2*(SidCur-1)]);
Dialog.addMessage("ZCor");
if(FreeZxyCorr==false)
{
Dialog.addNumber("ZxCor", CorrZX[CamCur-1+2*(SidCur-1)]);
Dialog.addNumber("ZyCor", CorrZY[CamCur-1+2*(SidCur-1)]);
}
else
{
cnt=(SidCur-1)*(XMax+1-XMin)*(YMax+1-YMin);
for(i=XMin;i<=XMax;i++)
{
Str="";
for(j=YMin;j<=YMax;j++)
{
Str=Str+ZManOffs[cnt];
if(j<YMax)Str=Str+",";
cnt++;
}
Dialog.addString("Col"+IJ.pad(i,2),Str,24);
}
}
if(DualSide)Dialog.addNumber("ZRLCor", CorrRLZ[CamCur-1]);
Dialog.addCheckbox("Free Zxy correction", FreeZxyCorr);
Dialog.addCheckbox("Free XY correction", FreeXYCorr);
Dialog.addCheckbox("Intensity correction", IntCorr);
Dialog.addChoice("Stitch Mode", newArray("Add","Copy","Max","Ramp"), StitchMode);
if(DualSide == true)Dialog.addCheckbox("Dual side mode", ShowDual);
if(EnableColorMode == true)Dialog.addCheckbox("Color mode", ColorMode);
if(EnableColorMode == true)Dialog.addCheckbox("Color blind", ColorBlind);
if(EnableColorMode == true)Dialog.addChoice("LUT:", newArray("Grays", "16 Colors"), DisplayLUT);
if(selectionType()==5)Dialog.addCheckbox("Register OvlX & YCor --> OvlY & XCorr", false);
if(selectionType()==5)Dialog.addCheckbox("Register OvlY & XCor only", false);
Dialog.addCheckbox("Grid+CAM+LR", false);
Dialog.addCheckbox("Export stack", false);
Dialog.addCheckbox("Exit", false);
Dialog.show();
ZCur = Dialog.getNumber();
CCur = Dialog.getNumber();
MaxInt[CCurOld+8*(SidCurOld-1)] = Dialog.getNumber();
if(!isOpen("CAM1"))CamCur = Dialog.getNumber();
SidCur = Dialog.getNumber();
OverlapX[CamCurOld-1+2*(SidCurOld-1)] = Dialog.getNumber();
OverlapY[CamCurOld-1+2*(SidCurOld-1)] = Dialog.getNumber();
CorrX[CamCurOld-1+2*(SidCurOld-1)] = Dialog.getNumber();
CorrY[CamCurOld-1+2*(SidCurOld-1)] = Dialog.getNumber();
if(FreeZxyCorr==false)
{
CorrZX[CamCurOld-1+2*(SidCurOld-1)] = Dialog.getNumber();
CorrZY[CamCurOld-1+2*(SidCurOld-1)] = Dialog.getNumber();
}
else
{
cnt=(SidCurOld-1)*(XMax+1-XMin)*(YMax+1-YMin);
for(i=XMin;i<=XMax;i++)
{
Str = Dialog.getString;
Str = split(Str,",");
if(lengthOf(Str)==(YMax+1-YMin))
{
for(j=0;j<=YMax-YMin;j++)
{
ZManOffs[cnt] = parseInt(Str[j]);
cnt++;
}
}
else
{
for(j=0;j<=YMax-YMin;j++)
{
ZManOffs[cnt] = 0;
cnt++;
}
}
}
}
if(DualSide == true)CorrRLZ[CamCurOld-1] = Dialog.getNumber();
FreeZxyCorr = Dialog.getCheckbox();
FreeXYCorr = Dialog.getCheckbox();
IntCorr = Dialog.getCheckbox();
StitchMode = Dialog.getChoice();
if(DualSide == true)ShowDual = Dialog.getCheckbox();
if(EnableColorMode == true)ColorMode = Dialog.getCheckbox();
if(EnableColorMode == true)ColorBlind = Dialog.getCheckbox();
if(EnableColorMode == true)DisplayLUT = Dialog.getChoice();
if(selectionType()==5)RegOvlX = Dialog.getCheckbox();
if(selectionType()==5)RegOvlY = Dialog.getCheckbox();
ShowExtra = Dialog.getCheckbox();
ExportStack = Dialog.getCheckbox();
Exit = Dialog.getCheckbox();
// Check if intensity calibration should be triggered
if((DualSide == true)&&(EnableColorMode == true))
{
if((StitchMode=="Ramp")&&(StitchModeOld!="Ramp"))
{
waitForUser("Ramp blending","- For dual-side illumination, OvlX MUST match LR overlap\n- Color mode will be disabled");
ColorMode = false;
}
}
// Wait alt to be released
while(isKeyDown("alt"))wait(50);
// Trigger board redraw
ReDraw = true;
// Make sure that current view is the camera and side marked by the sliders (avoid register CAM2 to itself!)
if((ShowExtra)&&((CamCurOld!=CamCur)||(SidCurOld!=SidCur)||(ZCurOld!=ZCur)||(CCurOld!=CCur)||(StitchModeOld!=StitchMode)||(ColorModeOld!=ColorMode)))
{
waitForUser("Sliders or Color mode updated\nwithout refreshing display\nGrid+CAM+LR panel wont't open!");
ShowExtra = false;
}
// Grid+CAM+LR Panel dialog box
if(ShowExtra)
{
BigStepsOld = BigSteps;
Dialog.create("Grid+CAM+LR Panel");
Dialog.addSlider("XdMin", XMin, XMax, XdMin);
Dialog.addSlider("XdMax", XMin, XMax, XdMax);
Dialog.addSlider("YdMin", YMin, YMax, YdMin);
Dialog.addSlider("YdMax", YMin, YMax, YdMax);
if(DualCAM==true)
{
Dialog.addMessage("CAM2 registration");
Dialog.addNumber("CAM2XCor", CAMXCor[SidCurOld-1]);
Dialog.addNumber("CAM2YCor", CAMYCor[SidCurOld-1]);
Dialog.addNumber("CAM2Angle", CAMAng[SidCurOld-1]);
Dialog.addNumber("CAM2Scale", CAMSca[SidCurOld-1]);
if(!isOpen("CAM1"))
{
if((ColorMode==true)||(CamCur==2))Dialog.addMessage("To overlay cameras or register them\ndisable Color mode and set view to CAM1");
}
else
{
if(CAMOverlay==false)Dialog.addMessage("Mark matching points in both windows\n\nStep 1: Two distant points (Tilt & Scaling)\nStep 2: One central point (XY position)");
}
}
if((CamCur==1)&&(!isOpen("CAM1"))&&(ColorMode==false)&&(DualCAM==true))Dialog.addCheckbox("Overlay CAM1", OverlayCAM1);
if((CamCur==1)&&(!isOpen("CAM1"))&&(ColorMode==false)&&(DualCAM==true))Dialog.addCheckbox("Register CAM2", false);
if(CamCur==2)
{
if(selectionType()==10)
{
getSelectionCoordinates(XCAM1,YCAM1);
if(lengthOf(XCAM1)==2)Dialog.addCheckbox("Register CAM2 Tilt & Scaling", false);
if(lengthOf(XCAM1)==1)Dialog.addCheckbox("Register CAM2 XY Position", false);
}
else
{
if(isOpen("CAM1"))Dialog.addCheckbox("Close CAM1 / Stop CAM2 registration", false);
}
}
if(DualSide)
{
Dialog.addMessage("Right mosaic registration");
Dialog.addNumber("CorrRLX", CorrRLX[CamCur-1]);
Dialog.addNumber("CorrRLY", CorrRLY[CamCur-1]);
Dialog.addNumber("CropFracX", CropFracWidth);
if(selectionType()==5)Dialog.addCheckbox("Register RL", false);
}
Dialog.addMessage("Big step used for Z adjustments");
Dialog.addNumber("Big step", BigSteps);
Dialog.show();
XdMin = Dialog.getNumber();
XdMax = Dialog.getNumber();
YdMin = Dialog.getNumber();
YdMax = Dialog.getNumber();
if(DualCAM==true)
{
CAMXCor[SidCurOld-1] = Dialog.getNumber();
CAMYCor[SidCurOld-1] = Dialog.getNumber();
CAMAng[SidCurOld-1] = Dialog.getNumber();
CAMSca[SidCurOld-1] = Dialog.getNumber();
}
if((CamCur==1)&&(!isOpen("CAM1"))&&(ColorMode==false)&&(DualCAM==true))OverlayCAM1 = Dialog.getCheckbox;
else OverlayCAM1 = false;
if(((CamCur==1)&&(!isOpen("CAM1"))&&(ColorMode==false)&&(DualCAM==true))||((CamCur==2)&&(isOpen("CAM1"))&&(ColorMode==false)&&(DualCAM==true)))RegCAM = Dialog.getCheckbox;
else RegCAM = false;
if(DualSide==1)
{
CorrRLX[CamCur-1] = Dialog.getNumber();
CorrRLY[CamCur-1] = Dialog.getNumber();
CropFracWidth = Dialog.getNumber();
CropFracWidth = maxOf(CropFracWidth,0);
CropFracWidth = minOf(CropFracWidth,0.5);
if(selectionType()==5)RegRight = Dialog.getCheckbox;
}
BigSteps = Dialog.getNumber();
// Update BigSteps
if(BigSteps != BigStepsOld)
{
Steps = Steps/abs(Steps)*BigSteps;
showStatus("Steps ="+d2s(Steps,0));
}
// Overlay CAM1 in CAM2 window
if(OverlayCAM1)
{
if(!isOpen("CAM1"))
{
// Display CAM1 window
run("Select None");
run("Duplicate...", "title=CAM1");
run("8-bit");
run(Col1);
CAMOverlay = true;
CamCur = 2; // Automatically switch to CAM2
selectImage(BoardID);
}
}
// Register CAM2
if(RegCAM == true)
{
if(CamCur==1)
{
// Starting point: Display CAM1 window + reset CAM2 registration
if(!isOpen("CAM1"))
{
tst = getBoolean("CAM2 registration will be reset, confirm?");
if(tst)
{
CAMXCor[SidCur-1] = 0;
CAMYCor[SidCur-1] = 0;
CAMAng[SidCur-1] = 0;
CAMSca[SidCur-1] = 1;
run("Select None");
run("Duplicate...", "title=CAM1");
run("8-bit");
run(Col1);
CAMOverlay = false;
CamCur = 2; // Automatically switch to CAM2
selectImage(BoardID);
}
else RegCAM = false;
}
}
else // Two step registration: Tilt + scaling for two active points, XY alignment for one active point
{
if(isOpen("CAM1"))
{
Do = 0;
selectImage("CAM1");