-
Notifications
You must be signed in to change notification settings - Fork 1
/
ISI_writeMovie.m
executable file
·103 lines (89 loc) · 3.27 KB
/
ISI_writeMovie.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
function ISIdata = ISI_writeMovie(ISIdata,prmts)
% Generate movie of averaged frames across trials
% Assumes that ISIdata.deltaSignal exists, with each frame as the averaged
% intrinsic signal across trials. We use the already calculated
% ISIdata.climAll to scale the image appropriately.
%
% This is in place of ISI_averageTrialsMovingAverage()
% and ISI_trialsMovingAverage(), which repeat analysis and do so in a
% different manner than what is done in ISI_calc_dRR
%
% Changelog:
% 11/11/11 Adapted from original code by Patrick Drew & Pablo Blinder. MJP
% 03/23/12 Fixed saving AVIs, which was broken. PMK
% 03/23/12 Added option to smooth frames. PMK
% 03/23/12 Frames are now saved in true size. PMK
if ~isfield(ISIdata,'deltaSignal')
error('deltaSignal does not exist');
end
hmovie = figure('color',[.7 .7 .7],'name',['Trial averaged moving average ' prmts.name]);
set(hmovie,'DoubleBuffer','on');
set(gca,'xlim',[1 ISIdata.frameSizeYX(2)],'ylim',[1 ISIdata.frameSizeYX(1)],...
'NextPlot','replace','Visible','off');
colormap(gray);
mvi = 0; % frame counter for movie
clear MOVIE ;
secperbin=ISIdata.bin_duration/ISIdata.frame_rate;
% mjp 2010.12.16 fixes crash on filename
filename = fullfile(prmts.path2dir,prmts.name);
[path,name,ext] = fileparts(filename);
if ~strcmpi(ext,'.avi')
k=strfind(filename,ext);
if ~isempty(k)
filename=strcat(filename(1:k(end)-1),'.avi');
end
end
if isempty(ext)
filename = strcat(filename,'.avi');
end
mSize = [];
for fi = 1:ISIdata.nFramesPerTrial
img = ISIdata.deltaSignal(:,:,fi);
% Smooth image
if ~isnan(prmts.smoothSigma)
mWin = fspecial('gaussian', prmts.smoothSigma*3, prmts.smoothSigma);
img = single(filter2(mWin, img, 'same'));
end
% Mask frame
if prmts.useManualMask
tMask = prmts.manualMask;
if ~isempty(tMask)
img(~tMask.mROI) = NaN;
% Crop image
[vI, vJ] = find(~isnan(img));
vI = [min(vI) max(vI)];
vJ = [min(vJ) max(vJ)];
img = imcrop(img, [vJ(1) vI(1) diff(vJ) diff(vI)]);
end
end
% Replace NaN's with zeros
img(isnan(img)) = 0;
if ~ishandle(hmovie), return, end
figure(hmovie)
imagesc(img);
title(sprintf('Frame # %d', fi));
text(4,ISIdata.frameSizeYX(1)-10, sprintf('%5.2f s', fi*secperbin),'color','k','backgroundcolor','w','fontweight','bold')
set(gca,'clim',ISIdata.climAll); colorbar ('horizontal');
if fi > ISIdata.nPreStimFrames && fi <= (ISIdata.nPreStimFrames+ISIdata.nStimFrames)
w = floor(ISIdata.frameSizeYX(1)*0.02); % make patch width 2% of frame
patch([10 10 10+w 10+w],[10 10+w 10+w 10], 'red');
end
axis image; axis off
truesize(gcf, size(img))
mvi = mvi + 1;
MOVIE(mvi) = getframe(gca);
mSize(end+1, :) = size(MOVIE(mvi).cdata);
drawnow
end
% Crop all frames in MOVIE to smallest frame (may vary due to buggy matlab plotting)
vMinSize = min(mSize, [], 1);
for f = 1:length(MOVIE)
MOVIE(f).cdata = MOVIE(f).cdata(1:vMinSize(1), 1:vMinSize(2), 1:1:vMinSize(3));
end
try
movie2avi(MOVIE, filename)
catch ME
avimov = close(avimov);
error(ME.message);
end
return