-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathEasyXT.m
2882 lines (2431 loc) · 116 KB
/
EasyXT.m
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
classdef EasyXT < handle
%%EASYXT Simplified commands to access Imaris Interface
% We're wrapping up some common funtions of Imaris so as to simplify
% the creation of XTensions. Access to most functions is simplified
% using 'PropertyName', 'PropertyValue' pairs.
%
% It is of course a work in progress and will greatly benefit from
% any feedback from the community of users.
%
% Olivier Burri & Romain Guiet, EPFL BioImaging & Optics Platform
% March 2014
% olivier dot burri at epfl.ch
% romain dot guiet at epfl.ch
% The necessary disclaimer:
% We abide by the GNU General Public License (GPL) version 3:
% 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 3 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.co
%
% You should have received a copy of the GNU General Public License
% along with this program. If not, see <http://www.gnu.org/licenses/>.
properties
% The class contains the ImarisApplication instance as a property
% so it can be accessed by all functions.
ImarisApp;
% Path To Imaris
imarisPath = 'C:\Program Files\Bitplane\Imaris x64 7.6.5\';
% We can define here the path where ImarisLib.jar is
imarisLibPath = '';
end
methods
function eXT = EasyXT(varargin)
%% EASYXT builds a new EasyXT object
% * eXT = EASYXT(imarisApplication) builds a new EasyXT object
% and attaches it to the currently running Imaris Instance.
% * eXT = EASYXT('setup') prompts the user to define the foler
% where the imaris exectuable is. This only needs to be done
% once.
%
% Optional Arguments
% o imarisApplicationID - in case you'd like to start EasyXT from
% an XTension
% o 'setup' - string to announce that you wish to define the
% path to Imaris.
% Supress Java Duplicate Class warnings
warning('off','MATLAB:Java:DuplicateClass');
eXT.imarisPath = getSavedImarisPath();
eXT.imarisPath = eXT.imarisPath(1:(end-1));
eXT.imarisLibPath = [eXT.imarisPath, 'XT\matlab\ImarisLib.jar'];
if (nargin == 1 && strcmp(varargin{1}, 'setup')) || strcmp(eXT.imarisPath, '') || (exist(eXT.imarisLibPath,'file') ~= 2)
[~,PathName] = uigetfile('.exe','Location of Imaris executable');
eXT.imarisPath = PathName;
eXT.imarisLibPath = [PathName, 'XT\matlab\ImarisLib.jar'];
setSavedImarisPath(eXT.imarisPath);
end
% Start Imaris Connection
javaaddpath(eXT.imarisLibPath);
% Create an instance of ImarisLib
vImarisLib = ImarisLib;
% Re-enable Java Duplicate Class warnings
warning('on','MATLAB:Java:DuplicateClass');
% Attach to given ImarisApplication or create new one
if nargin == 1 && isa(varargin{1}, 'Imaris.IApplicationPrxHelper')
eXT.ImarisApp = varargin{1};
elseif nargin == 1 && ischar(varargin{1})
ID = round(str2double(varargin{1}));
else
% Grab the Imaris server
server = vImarisLib.GetServer();
try
ID = server.GetObjectID(0);
if ischar(ID)
ID = round(str2double(ID));
end
catch err
disp('Seems Imaris is not open: Could not get Imaris Application');
disp('Original error below:');
rethrow(err);
end
end
try
% Open the Imaris Application
eXT.ImarisApp = vImarisLib.GetApplication(ID);
catch err
disp('Could not get Imaris Application with ID');
disp('Original error below:');
rethrow(err);
end
end
end
%% Public methods.
methods
function ImarisObject = GetObject(eXT, varargin)
%% GETOBJECT recovers an object from the Surpass scene
% Imarisobject = GETOBJECT('Name', name, ...
% 'Parent', parent, ...
% 'Number', number, ...
% 'Type', type, ...
% 'Cast', isCast
% )
%
% Returns the imaris object defined by the Optional Name,
% Parent, Number, Type and Cast Parameters.
%
% The list of options and default values is:
% o Name - Name of the object as it appears on the Surpass
% Scene, like 'Volume', 'Surface 1', 'Spots 10', etc.
% Defaults to [ [] ]
%
% o Parent - The parent object where we should look for the
% object. Useful mostly when creating Groups.
% Defaults to Surpass Scene
%
% o Number - The nth object in the scene (Starting at 1). Can
% be combined with name and type to search for the nth object
% withe the same name or the nth surface, for example.
% Defaults to [ 1 ]
%
% o Type - Defines the type of object Choices are:
% 'All', 'Spots', 'Surfaces', 'Points' and 'Groups'
% Defaults to [ 'All' ]
%
% o Cast - Determines what kind of object you want in return.
% Any Surpass component is of class IDataItem. Surfaces for
% example are of a subclass of IDataItem, called ISurfaces.
% this property, if set to 'true', returns the object of
% the subclass if possible. Otherwise, it returns an IDataItem
% Object. Not too useful but good to have in case it's
% needed.
% Defaults to [ true ]
%
% Examples:
% %Return the active object in the imaris scene
% ImarisObj = GetObject();
% %Returns the first surface object
% spotsObj = GetObject('Type', 'Spots');
% %Returns fourth object in the scene.
% ImarisObj = GetObject('Number', 4);
% %Returns the first surface object with name 'My Surfaces'.
% surfObj = GetObject('Type', 'Surfaces', 'Name', 'My Surfaces');
% %Get the Second Surfaces object inside the folder 'Group 1'
% parentFolder = GetObject('Name', 'Group 1');
% surfObj = GetObject('Type', 'Surfaces', 'Number', 2, 'Parent', parentFolder);
% See also CreateGroup
% Define Defaults:
parent = eXT.ImarisApp.GetSurpassScene;
name = [];
number = 1;
type = 'All';
cast = true;
for i=1:2:length(varargin)
switch varargin{i}
case 'Parent'
parent = varargin{i+1};
case 'Name'
name = varargin{i+1};
case 'Number'
number = varargin{i+1};
case 'Type'
type = varargin{i+1};
case 'Cast'
cast = varargin{i+1};
otherwise
error(['Unrecognized Command:' varargin{i}]);
end
end
% here are the Existing Types
types = {'All', 'Spots', 'Surfaces', 'Points' , 'Groups'};
if ~any( contains(types, type) )
e = strcat ('Unrecognized Object Type (should be either: All, Spots, Surfaces, Points , Groups)' );
error( e );
end
object = []; %#ok<*NASGU>
ImarisObject = [];
if nargin==1
% Return the active selection
ImarisObject = GetImarisObject(eXT, eXT.ImarisApp.GetSurpassSelection, cast);
else
% Search for the object
specificTypeNum = 0;
nChildren = parent.GetNumberOfChildren;
for i = 1 : nChildren
object = parent.GetChild(i-1);
objName = eXT.GetName(object);
objType = GetImarisType(eXT, object);
if ~isempty(name) % If we're using the name
if strcmp(objName, name) % If the name matches
if strcmp(objType,type) || strcmp(type, 'All') % If the type matches
specificTypeNum = specificTypeNum+1;
if specificTypeNum == number
% GIVE THE OBJECT BACK
ImarisObject = GetImarisObject(eXT, object, cast);
break;
end
end
end
else % Use only the number and type
if strcmp(objType,type) || strcmp(type, 'All') % If the type matches
specificTypeNum = specificTypeNum+1;
if specificTypeNum == number
% GIVE THE OBJECT BACK
ImarisObject = GetImarisObject(eXT, object, cast);
break;
end
end
end
end
end
end
function SelectObject(eXT, varargin)
%% SELECTOBJECT Makes the selected object active on the Surpass Scene
% SELECTOBJECT('Name', name, ...)
% All arguments from GetObject can be applied here
% See also GetObject
obj = GetObject(eXT, varargin{:});
eXT.ImarisApp.SetSurpassSelection(obj);
end
function number = GetNumberOf(eXT, type, varargin)
%% GETNUMBEROF recovers the number of objects with a certain type
% number = GETNUMBEROF(type, 'Name', name, ... )
% Arguments
% o type - Type of Object: Spots, Surfaces, Group, Points,
% Cells, Filaments, DataSet, Light Source, Camera, Volume,
% Clipping Plane, Application
% Optional Key, Value Pairs:
% o All optional arguments from GetObject can be applied here
%
% This function is nice for iterating among objects of the same
% kind.
%
% Example:
% nSpots = GetNumberOf('Spots');
% for i=1:nSpots
% aSpot = GetObject('Type', 'Spots', 'Number', i);
% % Do something with spot number i here
% end
%
% See also GetObject
parent = eXT.ImarisApp.GetSurpassScene;
name = [];
for i=1:2:length(varargin)
switch varargin{i}
case 'Parent'
parent = varargin{i+1};
case 'Name'
name = varargin{i+1};
otherwise
error(['Unrecognized Command:' varargin{i}]);
end
end
% Search for the object
number = 0;
nChildren = parent.GetNumberOfChildren;
for i = 1 : nChildren
object = parent.GetChild(i-1);
objName = eXT.GetName(object);
objType = GetImarisType(eXT, object);
if ~isempty(name) % If we're using the name
if strcmp(objName, name) % If the name matches
if strcmp(objType,type) || strcmp(type, 'All') % If the type matches
number = number+1;
end
end
else % Use only the number and type
if strcmp(objType,type) || strcmp(type, 'All') % If the type matches
number = number+1;
end
end
end
end
function name = GetName(~, object)
%% GETNAME returns the name of the object if possible
% name = GETNAME(object)
% This is mainly for convenience so we never forget to cast the
% result of object.GetName to a char()
%
% Arguments
% o object - The Imaris object whose name you want
% See also SetName
if any(strcmp(methods(object), 'GetName'))
name = char(object.GetName);
else
name = [];
%disp(['Object of class ' class(object) ' has no "GetName" method.']);
end
end
function name = SetName(~, object, name)
%% SETNAME sets the name of the object if possible
% name = SETNAME(object, name)
%
% Arguments
% o object - The Imaris object whose name you want to set
% o name - The name you want to give to the object
% See also GetName
if any(strcmp(methods(object), 'SetName'))
object.SetName(name);
else
disp('This object has no "SetName" method.')
end
end
function SetColor(~, object, colors)
%% SETCOLOR sets the color of the object passed as argument
% SETCOLOR(object, colors) Takes an array that can be of the following forms
% - a 1x3 array with each element being an integer between
% [0-255].
% colors(1) is Red
% colors(2) is Green
% colors(3) is Blue
% - a 1x4 array where colors(1:3) are as above
% colors(4) is transparency (values [0-128])
if length(colors) < 4 % check if there is only 3 ,
colors(4) = 0; % means no transparecncy setted, so color(4) = 0
end
if length(colors) == 1 && colors < 255 % if only 1 value
colors(1:3) = colors(1); % set it for each column
end
if length(colors) == 2 || length(colors) > 4
disp('That is not a color, dear... I need a matrix with 1, 3 or 4 elements. Did you forget your medication?');
colors = [128, 128, 128, 0]; % set it to grey
end
if length(colors) == 1 && colors > 255
object.SetColorRGBA(double(colors));
else
col = colors(1) + colors(2)*256 + colors(3)*256^2 + colors(4)*256^3;
% Note that the A value (Transparency, or color(4))can only go to 128, this is a bug in
% Imaris...
object.SetColorRGBA(double(col));
end
end
function color = GetColor(~, object)
%% GETCOLOR returns the color of the current object as a 4 element vector
% color = GETCOLOR(object) checks if there is a getcolor method
% available and retuns a 4-element vector as follows:
% color(1) : RED
% color(2) : GREEN
% color(3) : BLUE
% color(4) : ALPHA
if any(strcmp(methods(object), 'GetColorRGBA'))
col = object.GetColorRGBA();
color = [];
e = 3;
for i = fliplr(0:e)
color(i+1) = floor(col ./ (256.^i)); %#ok<*AGROW>
col = col - color(i+1).*256.^i;
end
else
disp('This object has no "SetName" method.')
end
end
function chans = ColorCode(eXT, channel, varargin)
theLut = 'parula';
for i=1:2:length(varargin)
switch varargin{i}
case 'LUT'
theLut = varargin{i+1};
otherwise
error(['Unrecognized Command:' varargin{i}]);
end
end
% Lookup table, and linear space fitting
lut = eval(['colormap(' theLut ')']);
ds = eXT.ImarisApp.GetDataSet.Clone();
% Do this between the min and max Display range of the selected channel
mina = ds.GetChannelRangeMin(channel-1);
maxa = ds.GetChannelRangeMax(channel-1);
chans = (eXT.GetSize('C')+1):(eXT.GetSize('C')+3);
ds.SetSizeC(ds.GetSizeC()+3);
%eXT.SetSize('C', eXT.GetSize('C')+3);
% Prepare 3 new channels... RGB
ds.SetChannelColorRGBA(chans(1)-1, 255);
ds.SetChannelColorRGBA(chans(2)-1, 255*256);
ds.SetChannelColorRGBA(chans(3)-1, 255*255*256);
dimSize = eXT.GetSize('Z');
interpAxis = linspace(0,1,dimSize);
vq1 = interp1(linspace(0,1,size(lut,1)),lut,interpAxis);
theX = eXT.GetSize('X');
theY = eXT.GetSize('Y');
theZ = eXT.GetSize('Z');
for t=1:eXT.GetSize('T')
for z=1:eXT.GetSize('Z')
%Get The Data
vox = double(eXT.GetVoxels(channel, 'Time',t, 'Slice', z, '1D', true));
%Normalize the values
vox = ((vox-mina) ./ (maxa - mina)).*255;
vox(vox<0) = 0;
tmpChan = zeros(size(vox));
fprintf('Slice %d of %d, T%d', z, eXT.GetSize('Z'));
for c=1:3
tmpChan(:,:) = vox.*vq1(z,c);
ds.SetDataSubVolumeAs1DArrayBytes(uint8(round(tmpChan)),0,0,z-1, chans(c)-1, t-1, theX, theY, 1);
end
end
end
eXT.ImarisApp.SetDataSet(ds);
end
function spots = CreateSpots(eXT, PosXYZ, spotSizes, varargin)
%% CREATESPOTS creates and returns a new spot object
% spots = CREATESPOTS(posXYZ, spotSizes, ...,
% 'Name', name, ...
% 'Single Timepoint', isSingle, ...
% 'Time Indexes', t, ... )
% Creates a new spots object with the given xyz positions and
% sizes.
% Input Arguments:
% o posXYZ - an nx3 array where each row contains the X,
% Y and Z position of the spots, in the unit of the imaris
% dataset (Usually microns).
% o spotSizes - either an nx1 or nx3 array containing the radius
% of each spot, making the spot either spherical or defined by
% its X Y and Z semi-major axes lenghts.
% Optional Parameters:
% o Name - The name of the spots object, otherwise it uses
% Imaris's default naming scheme (Spots 1, Spots 2, ...)
% o Single Timepoint - Should the time indexes not be defined
% explicitely using the 'Time Indexes' option, are the spots
% to be created only for the currently selected timepoint or
% for each timepoint in the dataset? Defaults to [ true ]
% o Time Indexes - nx1 array with the time index
% corresponding to each spot. Must be the same length as
% posXYZ.
%
% NOTE: The spot is not added to the scene, use ADDTOSCENE for
% this.
t = zeros(size(PosXYZ,1),1);
isOnlyCurrent = true;
name = [];
% in order to complete for time
for i=1:2:length(varargin)
switch varargin{i}
case 'Single Timepoint'
isOnlyCurrent = varargin{i+1};
case 'Name'
name = varargin{i+1};
case 'Time Indexes'
t = varargin{i+1};
isOnlyCurrent = true;
otherwise
error(['Unrecognized Command:' varargin{i}]);
end
end
if (all(size(spotSizes) == [1 1]))
spotSizes = repmat(spotSizes, size(PosXYZ,1),3);
end
if size(spotSizes,2) == 1
spotSizes = repmat(spotSizes, 1,3);
end
if ~( size(spotSizes,2) == 3 || size(spotSizes,2) == 1 )&& ( size(spotSizes,1) == size(PosXYZ,1) )
error('Radii must be either an nx1 array or an nx3 array');
end
if size(PosXYZ,2) ~= 3
error('XYZ must be nx3 in size.');
end
% Create the spots
spots = eXT.ImarisApp.GetFactory().CreateSpots();
if isOnlyCurrent
spots.Set(PosXYZ, t, spotSizes(:,1));
spots.SetRadiiXYZ(spotSizes);
else
nT = eXT.GetSize('T');
spots.Set(repmat(PosXYZ,nT,1), repmat(t,nT,1), repmat(spotSizes, nT,1));
end
if ~isempty(name)
spots.SetName(name);
end
end
function OpenImage(eXT, filepath)
%% OPENIMAGE Opens the file in a new imaris scene
% OpenImage(filepath) is useful when batch processing data
CreateNewScene(eXT);
eXT.ImarisApp.FileOpen(filepath,'');
eXT.ImarisApp.GetSurpassCamera.Fit();
end
function analyzeImage(eXT, isAppend, analysis_function)
fileDir =[];
data = [];
[dir filename ext] = eXT.GetCurrentFileName();
nChan = eXT.GetSize('C');
%append name of analysis function to the filename
funcData = functions( analysis_function );
funcName = funcData.function;
fileDir = dir;
if isAppend
file = ['/' funcName '-analysis.csv'];
else
file = ['/' filename '-' funcName '.csv'];
end
filePath = [fileDir file];
if exist(filePath, 'file') == 2 && isAppend
% Read In the data
data = readtable(filePath, 'Delimiter' ,',');
else
data = table;
end
result = analysis_function( eXT );
result.Image = repmat({filename}, size(result,1),1);
% Append file Name here
data = [data; result];
writetable(data,filePath, 'Delimiter' ,',');
end
function RunBatch( eXT, batchPath, analysis_function, isAppend )
%Get the starting path for the directory
lastImgPath = eXT.GetCurrentFileName();
% Ask for the directory
files = dir( fullfile(batchPath) ); % List the files
files = {files.name}'; % Keep only the name as a Cell Array
saveDir = [batchPath,'/Results/'];
mkdir(saveDir);
% Check for files that already exist
processedFiles = dir(saveDir);
processedFileNames = {processedFiles.name}';
% Go through the files
for i = 1:numel(files)
file = files{i};
if eXT.isImage(file) && ~strcmp('.',file) && ~strcmp('..',file)
if ~ismember([file,'.ims'], processedFileNames)
eXT.OpenImage(fullfile(batchPath, file));
% Run the processing
analyzeImage(eXT, isAppend, analysis_function);
eXT.ImarisApp.FileSave([fullfile(saveDir, file) '.ims'],'');
pause(2);
else
fprintf('%s: already processed\n', file)
end
end
end
end
function CreateNewScene(eXT)
%% CREATENEWSCENE Creates a new Surpass scene
% CreateNewScene() is useful for clearning the current scene in
% the case that we are batch opening images, for examples.
vSurpassScene = eXT.ImarisApp.GetFactory.CreateDataContainer;
vSurpassScene.SetName('Scene');
%% Add a light source
vLightSource = eXT.ImarisApp.GetFactory.CreateLightSource;
vLightSource.SetName('Light source');
%% Add a frame (otherwise no 3D rendering)
vFrame = eXT.ImarisApp.GetFactory.CreateFrame;
vFrame.SetName(strcat('Frame'));
%% Add a Volume (otherwise no 3D rendering)
vVolume = eXT.ImarisApp.GetFactory.CreateVolume;
vVolume.SetName(strcat('Volume'));
%% Set up the surpass scene
eXT.ImarisApp.SetSurpassScene(vSurpassScene);
AddToScene(eXT, vLightSource);
AddToScene(eXT, vFrame);
AddToScene(eXT, vVolume);
end
function ResetScene(eXT)
%% ResetScene Creates a new Surpass scene
% RESETSCENE() is useful for clearning the current scene.
scene = eXT.ImarisApp.GetSurpassScene;
nChildren = scene.GetNumberOfChildren;
% objectsToRemove = [];
for i = nChildren:-1:1
object = scene.GetChild(i-1);
objectType = GetImarisType(eXT, object);
if (~( strcmp ( objectType , 'Light Source' ) || ...
strcmp ( objectType , 'Frame' ) || ...
strcmp ( objectType , 'Volume') ))
scene.RemoveChild(object) ;
end
end
end
function AddToScene(eXT, object, varargin)
%% ADDTOSCENE adds the object to the Imaris Scene
% ADDTOSCENE(object, 'Parent', parent) adds the object to the
% Imaris Scene or to the optional parent object.
% Optional Parameter/Value pairs
% o Parent - The parent object to add this new object to.
parent = eXT.ImarisApp.GetSurpassScene;
for i=1:2:length(varargin)
switch varargin{i}
case 'Parent'
parent = varargin{i+1};
otherwise
error(['Unrecognized Command:' varargin{i}]);
end
end
parent.AddChild(object, -1);
end
function RemoveFromScene(eXT, object, varargin)
%% REMOVEFROMSCENE removes the object from Imaris Scene
% Optional Parameter/Value pairs
% o Parent - The parent object inside which the object lives.
parent = eXT.ImarisApp.GetSurpassScene;
for i=1:2:length(varargin)
switch varargin{i}
case 'Parent'
parent = varargin{i+1};
otherwise
error(['Unrecognized Command:' varargin{i}]);
end
end
parent.RemoveChild(object);
end
function [newChannel, vDataset] = MaskChannelWithSurface( eXT, channel, surface, varargin )
outside = 0;
inside = 'same';
for i=1:2:length(varargin)
switch varargin{i}
case 'Outside'
outside = varargin{i+1};
case 'Inside'
inside = varargin{i+1};
otherwise
error(['Unrecognized Command:' varargin{i}]);
end
end
vDataSet = eXT.ImarisApp.GetDataSet.Clone;
% Get some info from the scene
[aData, aSize, aMin, aMax, ~] = GetDataSetData(vDataSet);
time = 1:eXT.GetSize('T');
slices = 1:eXT.GetSize('Z');
% Iterate through all selected timepoints and slices
for ind=1:size(time,2)
surfaceMask(ind) = surface.GetMask( aMin(1), aMin(2),aMin(3), ...
aMax(1), aMax(2), aMax(3), ...
aSize(1), aSize(2), aSize(3), ...
time(ind)-1);
end
newChannel = eXT.GetSize('C');
vDataSet.SetSizeC(newChannel+1);
dataType = eXT.GetDataType();
for z=slices
progress(z-1,eXT.GetSize('Z'));
for ind=1:size(surfaceMask,1)
switch dataType
case '8-bit'
maskSlice = surfaceMask(ind).GetDataSliceBytes(z-1, 0,0);
case '16-bit'
maskSlice = surfaceMask(ind).GetDataSliceShorts(z-1, 0,0);
case '32-bit'
maskSlice = surfaceMask(ind).GetDataSliceFloats(z-1, 0,0);
end
switch dataType
case '8-bit'
chanSlice = vDataSet.GetDataSliceBytes(z-1, channel-1,time(ind)-1);
case '16-bit'
chanSlice = vDataSet.GetDataSliceShorts(z-1, channel-1,time(ind)-1);
case '32-bit'
chanSlice = vDataSet.GetDataSliceFloats(z-1, channel-1,time(ind)-1);
end
newSlice = chanSlice;
% Here we have the mask, now we take the channel we
if ~strcmp( inside, 'same' )
% Modify the inside to be the given value
newSlice(maskSlice==1) = inside;
end
if ~strcmp( outside, 'same' )
newSlice(maskSlice==0) = outside;
end
switch dataType
case '8-bit'
vDataSet.SetDataSliceBytes(newSlice, z-1, newChannel, time(ind)-1);
case '16-bit'
vDataSet.SetDataSliceShorts(newSlice, z-1, newChannel, time(ind)-1);
case '32-bit'
vDataSet.SetDataSliceFloats(newSlice, z-1, newChannel, time(ind)-1);
end
end
end
% Name the channel
vDataSet.SetChannelName(newChannel, ['Masked Channel: ' char(vDataSet.GetChannelName(channel-1)) ' using "' char(surface.GetName) ' "']);
% Give the channel the same color as the Surface object
vRGBA = vDataSet.GetChannelColorRGBA(channel-1);
vDataSet.SetChannelColorRGBA(newChannel, vRGBA);
eXT.ImarisApp.SetDataSet(vDataSet);
newChannel = newChannel + 1;
end
function [newChannel, vDataSet] = MakeChannelFromSurfaces(eXT, surface, varargin)
%% MAKECHANNELFROMSURFACES builds a new channel from a surface mask.
% [newChannel vDataSet] = MAKECHANNELFROMSURFACES(surface, ...
% 'Mask Channel', maskChannel, ...
% 'IDs', surfIDs, ...
% 'Time Indexes', tInd, ...
% 'Inside', inVal, ... ...
% 'Outside', outVal, ...
% 'Add Channel', isAdd);
% Creates a new channel containing one of either:
% A mask based on the surface used OR
% An exsisting channel, masked by the surface object. All
% values outside the surface object are set to 0. Inside values
% are set to the value of the original channel.
% In all cases, this function creates a new channel and returns
% the index of that channel, as well as the new IDataSet object.
% Optional parameters are:
% o Mask Channel - If set, the number of the channel to mask
% with surface.
% o Time Indexes - The timepoints to consider. If not set,
% all timepoints will be treated. Index starts at 1.
% o IDs - IDs of the surfaces to use as masks. You cannot use
% this option in conjunction with time indexes, as surface
% IDs are unique for each surface, this includes timepoints.
% Masking based on track IDs could be implemented at a later
% date if needed.
% o Inside - The voxel value that you want the voxels inside
% the surface to have. Defaults to 255 or the value of the
% Mask Channel.
% o Outside - The voxel value that you want the voxels
% outside the surface to have. Defaults to 0;
% o Add Channel - Defines whether the function adds the
% channel to the current or not. You can grab the updated
% dataset through the second output argument. [ true ]
%
% Output Arguments
% newChannel returns the number of the new channel
% aDataSet returns the newly created IDataSet object.
%
% Example usage
% >> newChannel = MakeChannelFromSurfaces(surface, 'Mask Channel', 1);
% Creates a new channel based on channel 1 and masked using
% surface.
% >> newChannel = MakeChannelFromSurfaces(surface); Creates a
% mask of surface and puts it in a new channnel.
% >> [newChannel aDataSet] = MakeChannelFromSurfaces(surface, 'Add Channel', false);
% Creates a mask of surface and only returns the dataset without creating it.
time = [];
surfIDs = 'All';
maskChannel = 'None';
inVal = 255;
outVal = 0;
isAddChannel = true;
vDataSet = [];
for i=1:2:length(varargin)
switch varargin{i}
case 'IDs'
surfIDs = varargin{i+1};
case 'Time Indexes'
time = varargin{i+1};
case 'Mask Channel'
maskChannel = varargin{i+1};
case 'Inside'
inVal = varargin{i+1};
case 'Outside'
outVal = varargin{i+1};
case 'Add Channel'
isAddChannel = varargin{i+1};
case 'DataSet'
vDataSet = varargin{i+1};
otherwise
error(['Unrecognized Command:' varargin{i}]);
end
end
% Do all timepoints if nothing is specified
if isempty(time)
time = 1:eXT.GetSize('T');
end
if isempty(vDataSet)
vDataSet = eXT.ImarisApp.GetDataSet.Clone;
end
% Get some info from the scene
[aData, aSize, aMin, aMax, ~] = GetDataSetData(vDataSet);
% Make sure the object is a surface
if strcmp(eXT.GetImarisType(surface), 'Surfaces')
% Get the mask for the surface
if ~strcmp(surfIDs,'All')
for ind=1:size(surfIDs,1)
surfaceMask(ind) = surface.GetSingleMask( surfIDs(ind), ...
aMin(1), aMin(2),aMin(3), ...
aMax(1), aMax(2), aMax(3), ...
aSize(1), aSize(2), aSize(3));
% Get the time index
time(ind) = surface.GetTimeIndex(surfIDs(ind))+1;
end
else
% Iterate through all selected timepoints
for ind=1:size(time,2)
surfaceMask(ind) = surface.GetMask( aMin(1), aMin(2),aMin(3), ...
aMax(1), aMax(2), aMax(3), ...
aSize(1), aSize(2), aSize(3), ...
time(ind)-1);
end
end
% Now make the new channel for each timepoint
newChannel = eXT.GetSize('C');
vDataSet.SetSizeC(newChannel+1);
dataType = eXT.GetDataType();
% And now create the new volumes for each timepoint or for
% each subsurface
for z=1:eXT.GetSize('Z')
progress(z-1,eXT.GetSize('Z'));
for ind=1:size(surfaceMask,1)
switch dataType
case '8-bit'
maskSlice = surfaceMask(ind).GetDataSliceBytes(z-1, 0,0);
case '16-bit'
maskSlice = surfaceMask(ind).GetDataSliceShorts(z-1, 0,0);
case '32-bit'
maskSlice = surfaceMask(ind).GetDataSliceFloats(z-1, 0,0);
end
if strcmp(maskChannel, 'None')
chanSlice = aData(:,:,z) + inVal - 1;
else
switch dataType
case '8-bit'
chanSlice = vDataSet.GetDataSliceBytes(z-1, maskChannel-1,time(ind)-1);
case '16-bit'
chanSlice = vDataSet.GetDataSliceShorts(z-1, maskChannel-1,time(ind)-1);
case '32-bit'
chanSlice = vDataSet.GetDataSliceFloats(z-1, maskChannel-1,time(ind)-1);
end
end
% class(chanVol)
% class(maskVol)
% Write to the dataset.
% Set the outside value
outSlice = maskSlice;
outSlice(maskSlice==0) = outVal;
switch dataType
case '8-bit'
vDataSet.SetDataSliceBytes(chanSlice .* maskSlice + outSlice, z-1, newChannel, time(ind)-1);
case '16-bit'
vDataSet.SetDataSliceShorts(chanSlice .* maskSlice + outSlice, z-1, newChannel, time(ind)-1);
case '32-bit'
vDataSet.SetDataSliceFloats(chanSlice .* maskSlice + outSlice, z-1, newChannel, time(ind)-1);
end
end
end
% Name the channel
if ~strcmp(maskChannel, 'None')
vDataSet.SetChannelName(newChannel, ['Masked Channel: ' char(vDataSet.GetChannelName(maskChannel-1)) ' using "' char(surface.GetName) ' "']);
% Give the channel the same color as the Surface object
vRGBA = vDataSet.GetChannelColorRGBA(maskChannel-1);
vDataSet.SetChannelColorRGBA(newChannel, vRGBA);
else
vDataSet.SetChannelName(newChannel, ['Masked from Surface ' char(surface.GetName)]);
% Give the channel the same color as the Masked Channel
vRGBA = surface.GetColorRGBA;
vDataSet.SetChannelColorRGBA(newChannel, vRGBA);
end
if isAddChannel
disp('Adding');