forked from bpisaacoff/RoboScope
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRecordMovie.m
135 lines (114 loc) · 3.89 KB
/
RecordMovie.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
function mov=RecordMovie(filename,numframes)
% RecordMovie is a function written by BPI to record a movie using
% micromanager.
%
% outputs a movie mov. *Note if you don't want mov stored in the workspace
% (potentially taking up a lot of memory) as ans, then call this function
% as [~]=RecordMovie(mm.core,filename,numframes)
%
% filename is the filename of the movie to be saved, if no movie is to be
% saved then put a [] there instead
%
% numframes is the number of frames to record
%% initializing
%the global micromanager structure
global mm
% file info
try
[pathstr,fname] = fileparts(filename);
catch
fname='frames';
end
mm.core.logMessage(['Running RecordMovie, saving ',filename])
%ROI with ROI.width & ROI.height
ROI=mm.core.getROI;
% checking live mode
if mm.slm.getIsLiveModeOn
livewason=true;
mm.slm.setLiveMode(false);
else
livewason=false;
end
%intialize matrix
mov=zeros(ROI.height,ROI.width,numframes);
%change to correct image type
try
%see if there is a last image available
lastimg=mm.core.getLastImage;
imtype=class(lastimg);
mov=eval([imtype,'(mov)']);
catch
try
%if not take a new image
mm.core.snapImage;
lastimg=mm.core.getImage;
imtype=class(lastimg);
mov=eval([imtype,'(mov)']);
catch
%guess that it's a uint16 type
mov=uint16(mov);
end
end
%% acquiring
%initialize waitbar
h1=waitbar(0);
set(h1,'Position',[481.5000 507 270 56.2500])
set(findall(h1,'type','text'),'Interpreter','none');
waitbar(0,h1,['Recording ',fname]);
% start the acquisition (and importantly the circular buffer)
% inputs ar: number of frames, interval between frames, Boolean re stopping
% on overflow
mm.core.startSequenceAcquisition(numframes, 0, false);
tic;%for debugging purposes
%initialize the frame counter
ii = 1;
%loop through until the the number of sequence is done
while mm.core.isSequenceRunning || mm.core.getRemainingImageCount>0
if mm.core.getRemainingImageCount>0
%grab & remove the next image from the circular buffer
img = mm.core.popNextImage;
%get the time for debugging purposes
times(ii)=toc;
%NOTE This line mm.slm.displayImage(img); is causing the error
% [IFO,App] Error: MetaData did not contain ElapsedTime-ms field
% to appear in the corelog 4 times everytime it's called as-is. ATM
% I'm not sure how to fix this.
%update the image in the Snap/Live Window
mm.slm.displayImage(img);
%put the image into the array
mov(:,:,ii)=reshape(img,[ROI.width,ROI.height])';
%update the counter
ii=ii+1;
%update the waitbar
try;waitbar(ii/numframes,h1);end
end
end
%stop the acquisition
mm.core.stopSequenceAcquisition;
%closing the waitbar
try; close(h1); end
%turn live mode back on if it was on initially
if livewason
mm.slm.setLiveMode(true);
end
%debugging
% diff(times)
%% save it
%if there is no file name given don't do this
if ~isempty(filename)
% check if either file exists already and if so rename to append _2nd
while exist([pathstr,filesep,fname,'.tif'],'file')==2 || exist([pathstr,filesep,fname,'.mat'],'file')==2
fname=[fname,'_2nd'];
end
%save the log file
Ben_LogFile([pathstr,filesep,fname],numframes);
%save it as a .mat file, using version 7.3 to ensure partial loading is
%available
save([pathstr,filesep,fname],'mov','-v7.3');
%save as a tiff stack
options.message=false;
% *note there might be a datatype problem here, if the movie isn't being
% saved as a tiff stack check that it's an appropriate datatype
% saveastiff(mov,[pathstr,filesep,fname,'.tif'],options);
end
end